| 1 | package filesscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/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 | "logscip-go gomod github.com/golang/go/src go1.13 log/" |
| 7 | "mathscip-go gomod github.com/golang/go/src go1.13 math/" |
| 8 | "runtimescip-go gomod github.com/golang/go/src go1.13 runtime/" |
| 9 | "syncscip-go gomod github.com/golang/go/src go1.13 sync/" |
| 10 | |
| 11 | "github.com/Azure/go-autorest/autorestscip-go gomod github.com/Azure/go-autorest/autorest v0.11.19 `github.com/Azure/go-autorest/autorest`/" |
| 12 | ) |
| 13 | |
| 14 | // GetFile is a helper method to download a file by chunking it automatically |
| 15 | func (client Clientscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/Client#) GetFilescip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/Client#GetFile().(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, parallelism int) (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#, outputBytes []byte, err error) { |
| 16 | |
| 17 | // first look up the file and check out how many bytes it is |
| 18 | file, e := client.GetPropertiesscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/Client#GetProperties().(ctx, accountName, shareName, path, fileName) |
| 19 | if err != nil { |
| 20 | result = file.Responsescip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/GetResult#Response. |
| 21 | err = e |
| 22 | return |
| 23 | } |
| 24 | |
| 25 | if file.ContentLengthscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/GetResult#ContentLength. == nil { |
| 26 | err = fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("Content-Length was nil!") |
| 27 | return |
| 28 | } |
| 29 | |
| 30 | length := int64(*file.ContentLengthscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/GetResult#ContentLength.) |
| 31 | chunkSize := int64(4 * 1024 * 1024) // 4MB |
| 32 | |
| 33 | if chunkSize > length { |
| 34 | chunkSize = length |
| 35 | } |
| 36 | |
| 37 | // then split that up into chunks and retrieve it retrieve it into the 'results' set |
| 38 | 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(length) / float64(chunkSize))) |
| 39 | workerCount := parallelism * runtimescip-go gomod github.com/golang/go/src go1.13 runtime/.NumCPUscip-go gomod github.com/golang/go/src go1.13 runtime/NumCPU().() |
| 40 | if workerCount > chunks { |
| 41 | workerCount = chunks |
| 42 | } |
| 43 | |
| 44 | 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# |
| 45 | waitGroup.Addscip-go gomod github.com/golang/go/src go1.13 sync/WaitGroup#Add().(workerCount) |
| 46 | |
| 47 | results := make([]*downloadFileChunkResultscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/downloadFileChunkResult#, chunks) |
| 48 | errors := make(chan error, chunkSize) |
| 49 | |
| 50 | for i := 0; i < chunks; i++ { |
| 51 | go func(i int) { |
| 52 | logscip-go gomod github.com/golang/go/src go1.13 log/.Printfscip-go gomod github.com/golang/go/src go1.13 log/Printf().("[DEBUG] Downloading Chunk %d of %d", i+1, chunks) |
| 53 | |
| 54 | dfci := downloadFileChunkInputscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/downloadFileChunkInput#{ |
| 55 | thisChunkscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/downloadFileChunkInput#thisChunk.: i, |
| 56 | chunkSizescip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/downloadFileChunkInput#chunkSize.: chunkSize, |
| 57 | fileSizescip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/downloadFileChunkInput#fileSize.: length, |
| 58 | } |
| 59 | |
| 60 | result, err := client.downloadFileChunkscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/Client#downloadFileChunk().(ctx, accountName, shareName, path, fileName, dfci) |
| 61 | if err != nil { |
| 62 | errors <- err |
| 63 | waitGroup.Donescip-go gomod github.com/golang/go/src go1.13 sync/WaitGroup#Done().() |
| 64 | return |
| 65 | } |
| 66 | |
| 67 | // if there's no error, we should have bytes, so this is safe |
| 68 | results[i] = result |
| 69 | |
| 70 | waitGroup.Donescip-go gomod github.com/golang/go/src go1.13 sync/WaitGroup#Done().() |
| 71 | }(i) |
| 72 | } |
| 73 | waitGroup.Waitscip-go gomod github.com/golang/go/src go1.13 sync/WaitGroup#Wait().() |
| 74 | |
| 75 | // TODO: we should switch to hashicorp/multi-error here |
| 76 | if len(errors) > 0 { |
| 77 | err = fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("Error downloading file: %s", <-errors) |
| 78 | return |
| 79 | } |
| 80 | |
| 81 | // then finally put it all together, in order and return it |
| 82 | output := make([]byte, length) |
| 83 | for _, v := range results { |
| 84 | copy(output[v.startBytesscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/downloadFileChunkResult#startBytes.:v.endBytesscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/downloadFileChunkResult#endBytes.], v.bytesscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/downloadFileChunkResult#bytes.) |
| 85 | } |
| 86 | |
| 87 | outputBytes = output |
| 88 | return |
| 89 | } |
| 90 | |
| 91 | type downloadFileChunkInputscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/downloadFileChunkInput# struct { |
| 92 | thisChunkscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/downloadFileChunkInput#thisChunk. int |
| 93 | chunkSizescip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/downloadFileChunkInput#chunkSize. int64 |
| 94 | fileSizescip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/downloadFileChunkInput#fileSize. int64 |
| 95 | } |
| 96 | |
| 97 | type downloadFileChunkResultscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/downloadFileChunkResult# struct { |
| 98 | startBytesscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/downloadFileChunkResult#startBytes. int64 |
| 99 | endBytesscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/downloadFileChunkResult#endBytes. int64 |
| 100 | bytesscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/downloadFileChunkResult#bytes. []byte |
| 101 | } |
| 102 | |
| 103 | func (client Clientscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/Client#) downloadFileChunkscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/Client#downloadFileChunk().(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 downloadFileChunkInputscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/downloadFileChunkInput#) (*downloadFileChunkResultscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/downloadFileChunkResult#, error) { |
| 104 | startBytes := input.chunkSizescip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/downloadFileChunkInput#chunkSize. * int64(input.thisChunkscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/downloadFileChunkInput#thisChunk.) |
| 105 | endBytes := startBytes + input.chunkSizescip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/downloadFileChunkInput#chunkSize. |
| 106 | |
| 107 | // the last chunk may exceed the size of the file |
| 108 | remaining := input.fileSizescip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/downloadFileChunkInput#fileSize. - startBytes |
| 109 | if input.chunkSizescip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/downloadFileChunkInput#chunkSize. > remaining { |
| 110 | endBytes = startBytes + remaining |
| 111 | } |
| 112 | |
| 113 | getInput := GetByteRangeInputscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/GetByteRangeInput#{ |
| 114 | StartBytesscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/GetByteRangeInput#StartBytes.: startBytes, |
| 115 | EndBytesscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/GetByteRangeInput#EndBytes.: endBytes, |
| 116 | } |
| 117 | result, err := client.GetByteRangescip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/Client#GetByteRange().(ctx, accountName, shareName, path, fileName, getInput) |
| 118 | if err != nil { |
| 119 | return nil, 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) |
| 120 | } |
| 121 | |
| 122 | output := downloadFileChunkResultscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/downloadFileChunkResult#{ |
| 123 | startBytesscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/downloadFileChunkResult#startBytes.: startBytes, |
| 124 | endBytesscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/downloadFileChunkResult#endBytes.: endBytes, |
| 125 | bytesscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/downloadFileChunkResult#bytes.: result.Contentsscip-go gomod github.com/tombuildsstuff/giovanni . `github.com/tombuildsstuff/giovanni/storage/2017-07-29/file/files`/GetByteRangeResult#Contents., |
| 126 | } |
| 127 | return &output, nil |
| 128 | } |