| 1 | package filesscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2018-11-09/file/files`/ |
| 2 | |
| 3 | import ( |
| 4 | "contextscip-go gomod github.com/golang/go/src go1.13 context/" |
| 5 | "fmtscip-go gomod github.com/golang/go/src go1.13 fmt/" |
| 6 | "ioscip-go gomod github.com/golang/go/src go1.13 io/" |
| 7 | "logscip-go gomod github.com/golang/go/src go1.13 log/" |
| 8 | "mathscip-go gomod github.com/golang/go/src go1.13 math/" |
| 9 | "osscip-go gomod github.com/golang/go/src go1.13 os/" |
| 10 | "syncscip-go gomod github.com/golang/go/src go1.13 sync/" |
| 11 | |
| 12 | "github.com/Azure/go-autorest/autorestscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest`/" |
| 13 | ) |
| 14 | |
| 15 | // PutFile is a helper method which takes a file, and automatically chunks it up, rather than having to do this yourself |
| 16 | func (client Clientscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2018-11-09/file/files`/Client#) PutFilescip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2018-11-09/file/files`/Client#PutFile().(ctx contextscip-go gomod github.com/golang/go/src go1.13 context/.Contextscip-go gomod github.com/golang/go/src go1.13 context/Context#, accountName, shareName, path, fileName string, file *osscip-go gomod github.com/golang/go/src go1.13 os/.Filescip-go gomod github.com/golang/go/src go1.13 os/File#, parallelism int) error { |
| 17 | fileInfo, err := file.Statscip-go gomod github.com/golang/go/src go1.13 os/File#Stat().() |
| 18 | if err != nil { |
| 19 | return fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("Error loading file info: %s", err) |
| 20 | } |
| 21 | |
| 22 | fileSize := fileInfo.Sizescip-go gomod github.com/golang/go/src go1.13 `io/fs`/FileInfo#Size().() |
| 23 | chunkSize := 4 * 1024 * 1024 // 4MB |
| 24 | if chunkSize > int(fileSize) { |
| 25 | chunkSize = int(fileSize) |
| 26 | } |
| 27 | chunks := int(mathscip-go gomod github.com/golang/go/src go1.13 math/.Ceilscip-go gomod github.com/golang/go/src go1.13 math/Ceil().(float64(fileSize) / float64(chunkSize*1.0))) |
| 28 | |
| 29 | workerCount := parallelism |
| 30 | if workerCount > chunks { |
| 31 | workerCount = chunks |
| 32 | } |
| 33 | |
| 34 | var waitGroup syncscip-go gomod github.com/golang/go/src go1.13 sync/.WaitGroupscip-go gomod github.com/golang/go/src go1.13 sync/WaitGroup# |
| 35 | waitGroup.Addscip-go gomod github.com/golang/go/src go1.13 sync/WaitGroup#Add().(workerCount) |
| 36 | |
| 37 | jobs := make(chan int, workerCount) |
| 38 | errors := make(chan error, chunkSize) |
| 39 | |
| 40 | for i := 0; i < workerCount; i++ { |
| 41 | go func() { |
| 42 | for i := range jobs { |
| 43 | logscip-go gomod github.com/golang/go/src go1.13 log/.Printfscip-go gomod github.com/golang/go/src go1.13 log/Printf().("[DEBUG] Chunk %d of %d", i+1, chunks) |
| 44 | |
| 45 | uci := uploadChunkInputscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2018-11-09/file/files`/uploadChunkInput#{ |
| 46 | thisChunkscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2018-11-09/file/files`/uploadChunkInput#thisChunk.: i, |
| 47 | chunkSizescip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2018-11-09/file/files`/uploadChunkInput#chunkSize.: chunkSize, |
| 48 | fileSizescip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2018-11-09/file/files`/uploadChunkInput#fileSize.: fileSize, |
| 49 | } |
| 50 | |
| 51 | _, err := client.uploadChunkscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2018-11-09/file/files`/Client#uploadChunk().(ctx, accountName, shareName, path, fileName, uci, file) |
| 52 | if err != nil { |
| 53 | errors <- err |
| 54 | } |
| 55 | } |
| 56 | waitGroup.Donescip-go gomod github.com/golang/go/src go1.13 sync/WaitGroup#Done().() |
| 57 | }() |
| 58 | } |
| 59 | |
| 60 | for i := 0; i < chunks; i++ { |
| 61 | jobs <- i |
| 62 | } |
| 63 | close(jobs) |
| 64 | waitGroup.Waitscip-go gomod github.com/golang/go/src go1.13 sync/WaitGroup#Wait().() |
| 65 | |
| 66 | // TODO: we should switch to hashicorp/multi-error here |
| 67 | if len(errors) > 0 { |
| 68 | return fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("Error uploading file: %s", <-errors) |
| 69 | } |
| 70 | |
| 71 | return nil |
| 72 | } |
| 73 | |
| 74 | type uploadChunkInputscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2018-11-09/file/files`/uploadChunkInput# struct { |
| 75 | thisChunkscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2018-11-09/file/files`/uploadChunkInput#thisChunk. int |
| 76 | chunkSizescip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2018-11-09/file/files`/uploadChunkInput#chunkSize. int |
| 77 | fileSizescip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2018-11-09/file/files`/uploadChunkInput#fileSize. int64 |
| 78 | } |
| 79 | |
| 80 | func (client Clientscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2018-11-09/file/files`/Client#) uploadChunkscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2018-11-09/file/files`/Client#uploadChunk().(ctx contextscip-go gomod github.com/golang/go/src go1.13 context/.Contextscip-go gomod github.com/golang/go/src go1.13 context/Context#, accountName, shareName, path, fileName string, input uploadChunkInputscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2018-11-09/file/files`/uploadChunkInput#, file *osscip-go gomod github.com/golang/go/src go1.13 os/.Filescip-go gomod github.com/golang/go/src go1.13 os/File#) (result autorestscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest`/.Responsescip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest`/Response#, err error) { |
| 81 | startBytes := int64(input.chunkSizescip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2018-11-09/file/files`/uploadChunkInput#chunkSize. * input.thisChunkscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2018-11-09/file/files`/uploadChunkInput#thisChunk.) |
| 82 | endBytes := startBytes + int64(input.chunkSizescip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2018-11-09/file/files`/uploadChunkInput#chunkSize.) |
| 83 | |
| 84 | // the last size may exceed the size of the file |
| 85 | remaining := input.fileSizescip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2018-11-09/file/files`/uploadChunkInput#fileSize. - startBytes |
| 86 | if int64(input.chunkSizescip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2018-11-09/file/files`/uploadChunkInput#chunkSize.) > remaining { |
| 87 | endBytes = startBytes + remaining |
| 88 | } |
| 89 | |
| 90 | bytesToRead := int(endBytes) - int(startBytes) |
| 91 | bytes := make([]byte, bytesToRead) |
| 92 | |
| 93 | _, err = file.ReadAtscip-go gomod github.com/golang/go/src go1.13 os/File#ReadAt().(bytes, startBytes) |
| 94 | if err != nil { |
| 95 | if err != ioscip-go gomod github.com/golang/go/src go1.13 io/.EOFscip-go gomod github.com/golang/go/src go1.13 io/EOF. { |
| 96 | return result, fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("Error reading bytes: %s", err) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | putBytesInput := PutByteRangeInputscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2018-11-09/file/files`/PutByteRangeInput#{ |
| 101 | StartBytesscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2018-11-09/file/files`/PutByteRangeInput#StartBytes.: startBytes, |
| 102 | EndBytesscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2018-11-09/file/files`/PutByteRangeInput#EndBytes.: endBytes, |
| 103 | Contentscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2018-11-09/file/files`/PutByteRangeInput#Content.: bytes, |
| 104 | } |
| 105 | result, err = client.PutByteRangescip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2018-11-09/file/files`/Client#PutByteRange().(ctx, accountName, shareName, path, fileName, putBytesInput) |
| 106 | if err != nil { |
| 107 | return result, fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("Error putting bytes: %s", err) |
| 108 | } |
| 109 | |
| 110 | return |
| 111 | } |