github.com/0chain/gosdk@v1.17.11/zboxcore/sdk/chunked_upload_option.go (about) 1 package sdk 2 3 import ( 4 "crypto/md5" 5 "encoding/hex" 6 "math" 7 "os" 8 "time" 9 10 "github.com/0chain/gosdk/zboxcore/zboxutil" 11 "github.com/klauspost/reedsolomon" 12 ) 13 14 // ChunkedUploadOption Generic type for chunked upload option functions 15 type ChunkedUploadOption func(su *ChunkedUpload) 16 17 // WithThumbnail add thumbnail. stream mode is unnecessary for thumbnail 18 func WithThumbnail(buf []byte) ChunkedUploadOption { 19 return func(su *ChunkedUpload) { 20 21 size := len(buf) 22 23 if size > 0 { 24 su.shardUploadedThumbnailSize = int64(math.Ceil(float64(size) / float64(su.allocationObj.DataShards))) 25 26 su.thumbnailBytes = buf 27 su.fileMeta.ActualThumbnailSize = int64(len(buf)) 28 29 thumbnailHasher := md5.New() 30 thumbnailHasher.Write(buf) 31 32 su.fileMeta.ActualThumbnailHash = hex.EncodeToString(thumbnailHasher.Sum(nil)) 33 34 su.thumbailErasureEncoder, _ = reedsolomon.New(su.allocationObj.DataShards, su.allocationObj.ParityShards) 35 36 } 37 } 38 } 39 40 // WithThumbnailFile add thumbnail from file. stream mode is unnecessary for thumbnail. 41 // - fileName: file name of the thumbnail, which will be read and uploaded 42 func WithThumbnailFile(fileName string) ChunkedUploadOption { 43 44 buf, _ := os.ReadFile(fileName) 45 46 return WithThumbnail(buf) 47 } 48 49 // WithChunkNumber set the number of chunks should be upload in a request. ignore if size <=0 50 // - num: number of chunks 51 func WithChunkNumber(num int) ChunkedUploadOption { 52 return func(su *ChunkedUpload) { 53 if num > 0 { 54 su.chunkNumber = num 55 } 56 } 57 } 58 59 // WithEncrypt turn on/off encrypt on upload. It is turn off as default. 60 // - on: true to turn on, false to turn off 61 func WithEncrypt(on bool) ChunkedUploadOption { 62 return func(su *ChunkedUpload) { 63 su.encryptOnUpload = on 64 } 65 } 66 67 // WithStatusCallback return a wrapper option function to set status callback of the chunked upload instance, which is used to track upload progress 68 // - callback: StatusCallback instance 69 func WithStatusCallback(callback StatusCallback) ChunkedUploadOption { 70 return func(su *ChunkedUpload) { 71 su.statusCallback = callback 72 } 73 } 74 75 // WithProgressCallback return a wrapper option function to set progress callback of the chunked upload instance 76 func WithProgressStorer(progressStorer ChunkedUploadProgressStorer) ChunkedUploadOption { 77 return func(su *ChunkedUpload) { 78 su.progressStorer = progressStorer 79 } 80 } 81 82 // WithUploadTimeout return a wrapper option function to set upload timeout of the chunked upload instance 83 func WithUploadTimeout(t time.Duration) ChunkedUploadOption { 84 return func(su *ChunkedUpload) { 85 su.uploadTimeOut = t 86 } 87 } 88 89 // WithCommitTimeout return a wrapper option function to set commit timeout of the chunked upload instance 90 func WithCommitTimeout(t time.Duration) ChunkedUploadOption { 91 return func(su *ChunkedUpload) { 92 su.commitTimeOut = t 93 } 94 } 95 96 // WithUploadMask return a wrapper option function to set upload mask of the chunked upload instance 97 func WithMask(mask zboxutil.Uint128) ChunkedUploadOption { 98 return func(su *ChunkedUpload) { 99 su.uploadMask = mask 100 } 101 } 102 103 // WithEncryptedKeyPoint return a wrapper option function to set encrypted key point of the chunked upload instance 104 func WithEncryptedPoint(point string) ChunkedUploadOption { 105 return func(su *ChunkedUpload) { 106 su.encryptedKeyPoint = point 107 } 108 } 109 110 // WithActualHash return a wrapper option function to set actual hash of the chunked upload instance 111 func WithActualHash(hash string) ChunkedUploadOption { 112 return func(su *ChunkedUpload) { 113 su.fileMeta.ActualHash = hash 114 } 115 } 116 117 // WithActualSize return a wrapper option function to set the file hasher used in the chunked upload instance 118 func WithFileHasher(h Hasher) ChunkedUploadOption { 119 return func(su *ChunkedUpload) { 120 su.fileHasher = h 121 } 122 }