github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/pkg/pinning/unpin_pin.go (about)

     1  package pinning
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"path/filepath"
     7  	"time"
     8  
     9  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
    10  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/debug"
    11  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/file"
    12  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger"
    13  )
    14  
    15  var unpinPin = "https://api.pinata.cloud/pinning/unpin/%s"
    16  
    17  // unpinOne unpins a pin
    18  func unpinOne(chain string, i, total int, hash base.IpfsHash) error {
    19  	url := fmt.Sprintf(unpinPin, hash.String())
    20  
    21  	debug.DebugCurlStr(url)
    22  	if req, err := http.NewRequest("DELETE", url, nil); err != nil {
    23  		return err
    24  	} else {
    25  		s, _ := NewService(chain, Pinata)
    26  		if s.HeaderFunc != nil {
    27  			headers := s.HeaderFunc(&s, "application/json")
    28  			for key, value := range headers {
    29  				req.Header.Add(key, value)
    30  			}
    31  		}
    32  		if res, err := http.DefaultClient.Do(req); err != nil {
    33  			return err
    34  		} else {
    35  			if res.StatusCode != 200 {
    36  				logger.Error("Error deleting pin", hash, res.StatusCode)
    37  			} else {
    38  				logger.Info("Unpinned", i, "of", total, hash.String())
    39  			}
    40  			return nil
    41  		}
    42  	}
    43  }
    44  
    45  func Unpin(chain string, count bool, sleep float64) error {
    46  	lines := file.AsciiFileToLines(filepath.Join(".", "unpins"))
    47  	if count {
    48  		logger.Info("There are", len(lines), "pins to unpin.")
    49  	} else {
    50  		for i, line := range lines {
    51  			if IsValid(line) {
    52  				if err := unpinOne(chain, i, len(lines), base.IpfsHash(line)); err != nil {
    53  					return err
    54  				}
    55  				if sleep > 0 {
    56  					ms := time.Duration(sleep*1000) * time.Millisecond
    57  					// logger.Info(fmt.Sprintf("Sleeping for %g seconds", sleep))
    58  					time.Sleep(ms)
    59  				}
    60  			}
    61  		}
    62  	}
    63  	return nil
    64  }