github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/pkg/pinning/pin_file.go (about) 1 package pinning 2 3 import ( 4 "fmt" 5 "path/filepath" 6 7 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base" 8 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/colors" 9 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/config" 10 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/file" 11 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/index" 12 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger" 13 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types" 14 ) 15 16 var ErrNoPinningService = fmt.Errorf("no pinning service available") 17 18 // PinOneFile pins the named database given a path to the local and/or remote pinning 19 // services if they are available. If the local service is not available, the remote 20 // service is used. 21 func PinOneFile(chain, dbName, fileName string, remote bool) (base.IpfsHash, base.IpfsHash, error) { 22 var localHash base.IpfsHash 23 var remoteHash base.IpfsHash 24 var err error 25 26 if !file.FileExists(fileName) { 27 return localHash, remoteHash, fmt.Errorf(dbName+" file (%s) does not exist", fileName) 28 } 29 30 local := config.IpfsRunning() 31 if !local && !remote { 32 return localHash, remoteHash, ErrNoPinningService 33 } 34 35 toShow := filepath.Base(fileName) 36 if local { 37 localService, _ := NewService(chain, Local) 38 if localHash, err = localService.pinFileLocally(chain, fileName); err != nil { 39 return localHash, remoteHash, fmt.Errorf("error pinning locally: %s %s", fileName, err) 40 } 41 } 42 43 if remote { 44 logger.Progress(true, colors.Magenta+"Pinning", dbName, "file", toShow, "remotely...", colors.Off) 45 remoteService, _ := NewService(chain, Pinata) 46 if remoteHash, err = remoteService.pinFileRemotely(chain, fileName); err != nil { 47 return localHash, remoteHash, fmt.Errorf("error pinning remotely: %s %s", fileName, err) 48 } 49 if localHash == "" { 50 localHash = remoteHash 51 } 52 } 53 54 logger.Info(colors.Magenta+"Pinned", dbName, "file", toShow, "to", localHash, colors.Off) 55 return localHash, remoteHash, nil 56 } 57 58 // PinOneChunk pins the named chunk given a path to the local and/or remote pinning service 59 func PinOneChunk(chain, path string, remote bool) (types.ChunkRecord, types.ChunkRecord, error) { 60 bloomFile := index.ToBloomPath(path) 61 indexFile := index.ToIndexPath(path) 62 local := config.IpfsRunning() 63 64 rng := base.RangeFromFilename(bloomFile) 65 localPin := types.ChunkRecord{Range: rng.String()} 66 remotePin := types.ChunkRecord{Range: rng.String()} 67 var err error 68 69 if !local && !remote { 70 return localPin, remotePin, ErrNoPinningService 71 } 72 73 if local { 74 localService, _ := NewService(chain, Local) 75 if localPin.BloomHash, err = localService.pinFileLocally(chain, bloomFile); err != nil { 76 return localPin, remotePin, err 77 } 78 localPin.BloomSize = file.FileSize(bloomFile) 79 if localPin.IndexHash, err = localService.pinFileLocally(chain, indexFile); err != nil { 80 return localPin, remotePin, err 81 } 82 localPin.IndexSize = file.FileSize(indexFile) 83 logger.Info(colors.Magenta+"Pinned", rng, "local to ", localPin.BloomHash, localPin.IndexHash, colors.Off) 84 } 85 86 if remote { 87 logger.Progress(true, colors.Magenta+"Pinning file", rng.String(), "remotely...", colors.Off) 88 remoteService, _ := NewService(chain, Pinata) 89 if remotePin.BloomHash, err = remoteService.pinFileRemotely(chain, bloomFile); err != nil { 90 return localPin, remotePin, err 91 } 92 remotePin.BloomSize = file.FileSize(bloomFile) 93 if remotePin.IndexHash, err = remoteService.pinFileRemotely(chain, indexFile); err != nil { 94 return localPin, remotePin, err 95 } 96 remotePin.IndexSize = file.FileSize(indexFile) 97 logger.Info(colors.Magenta+"Pinned", rng, "remote to", remotePin.BloomHash, remotePin.IndexHash, colors.Off) 98 } 99 100 return localPin, remotePin, err 101 }