github.com/0chain/gosdk@v1.17.11/zboxcore/sdk/downloader.go (about) 1 package sdk 2 3 import ( 4 "path" 5 6 "errors" 7 8 "github.com/0chain/gosdk/core/sys" 9 "github.com/0chain/gosdk/zboxcore/fileref" 10 ) 11 12 // Downloader downloader for file, blocks and thumbnail 13 type Downloader interface { 14 GetAllocation() *Allocation 15 GetFileName() string 16 Start(status StatusCallback, isFinal bool) error 17 } 18 19 // DownloadOptions download options 20 type DownloadOptions struct { 21 allocationObj *Allocation 22 fileName string 23 24 localPath string 25 remotePath string 26 27 isViewer bool 28 authTicket string 29 lookupHash string 30 31 isBlockDownload bool 32 blocksPerMarker int 33 startBlock int64 34 endBlock int64 35 36 isThumbnailDownload bool 37 verifyDownload bool 38 39 isFileHandlerDownload bool 40 fileHandler sys.File 41 reqOpts []DownloadRequestOption 42 } 43 44 // CreateDownloader create a downloander instance with options 45 // - allocationID: allocation id 46 // - localPath: local path to save the downloaded file 47 // - remotePath: remote path of the file to download 48 // - opts: download options as option functions 49 func CreateDownloader(allocationID, localPath, remotePath string, opts ...DownloadOption) (Downloader, error) { 50 do := DownloadOptions{ 51 localPath: localPath, 52 remotePath: remotePath, 53 } 54 55 if len(remotePath) > 0 { 56 do.fileName = path.Base(remotePath) 57 } 58 59 for _, option := range opts { 60 option(&do) 61 } 62 63 var err error 64 if do.allocationObj == nil { 65 if do.isViewer { 66 do.allocationObj, err = GetAllocationFromAuthTicket(do.authTicket) 67 if err != nil { 68 return nil, err 69 } 70 } else { 71 do.allocationObj, err = GetAllocation(allocationID) 72 if err != nil { 73 return nil, err 74 } 75 } 76 } 77 78 // fixed fileName if only auth ticket/lookup are known 79 if len(do.fileName) == 0 { 80 if do.isViewer { 81 at, err := InitAuthTicket(do.authTicket).Unmarshall() 82 83 if err != nil { 84 return nil, err 85 } 86 87 if at.RefType == fileref.FILE { 88 do.fileName = at.FileName 89 do.lookupHash = at.FilePathHash 90 } else if len(do.lookupHash) > 0 { 91 fileMeta, err := do.allocationObj.GetFileMetaFromAuthTicket(do.authTicket, do.lookupHash) 92 if err != nil { 93 return nil, err 94 } 95 do.fileName = fileMeta.Name 96 } else if len(remotePath) > 0 { 97 do.lookupHash = fileref.GetReferenceLookup(do.allocationObj.ID, remotePath) 98 do.fileName = path.Base(remotePath) 99 } else { 100 return nil, errors.New("Either remotepath or lookuphash is required when using authticket of directory type") 101 } 102 } else { 103 return nil, errors.New("remotepath is required") 104 } 105 } 106 107 if do.isFileHandlerDownload { 108 do.reqOpts = append(do.reqOpts, WithFileCallback(func() { 109 do.fileHandler.Close() //nolint:errcheck 110 })) 111 return &fileHandlerDownloader{ 112 baseDownloader: baseDownloader{ 113 DownloadOptions: do, 114 }, 115 }, nil 116 } 117 118 if do.isThumbnailDownload { 119 return &thumbnailDownloader{ 120 baseDownloader: baseDownloader{ 121 DownloadOptions: do, 122 }, 123 }, nil 124 } else if do.isBlockDownload { 125 return &blockDownloader{ 126 baseDownloader: baseDownloader{ 127 DownloadOptions: do, 128 }, 129 }, nil 130 } 131 132 return &fileDownloader{ 133 baseDownloader: baseDownloader{ 134 DownloadOptions: do, 135 }, 136 }, nil 137 } 138 139 type baseDownloader struct { 140 DownloadOptions 141 } 142 143 // GetAllocation get the allocation object 144 func (d *baseDownloader) GetAllocation() *Allocation { 145 if d == nil { 146 return nil 147 } 148 return d.allocationObj 149 } 150 151 // GetFileName get the file name 152 func (d *baseDownloader) GetFileName() string { 153 if d == nil { 154 return "" 155 } 156 157 return d.fileName 158 }