@@ -31,8 +31,11 @@ import (
3131 "io"
3232 "os"
3333 "mime/multipart"
34+ "path/filepath"
3435)
3536
37+ const chunkSize = 1024 * 1024 // 1 MiB
38+
3639type RequestError struct {
3740 StatusCode int
3841 Err string
@@ -192,20 +195,31 @@ func UploadFiles(address, token, path string, filePaths []string, skipTlsVerific
192195 writer := multipart .NewWriter (body )
193196 outputMap := map [string ]interface {}{}
194197
195- for _ , path := range filePaths {
198+ // Open and stream each file into multipart writer
199+ for idx , path := range filePaths {
196200 file , err := os .Open (path )
197201 if err != nil {
198- return outputMap , fmt .Errorf ("failed to open file %s: %w " , path , err )
202+ return outputMap , fmt .Errorf ("failed to open file %s: %v " , path , err )
199203 }
200204 defer file .Close ()
201205
202- part , err := writer .CreateFormFile ("file" , path )
206+ part , err := writer .CreateFormFile (fmt . Sprintf ( "file%d " , idx ), filepath . Base ( path ) )
203207 if err != nil {
204- return outputMap , fmt .Errorf ("failed to create form file for %s: %w " , path , err )
208+ return outputMap , fmt .Errorf ("failed to create form part for %s: %v " , path , err )
205209 }
206210
207- if _ , err := io .Copy (part , file ); err != nil {
208- return outputMap , fmt .Errorf ("failed to copy file data for %s: %w" , path , err )
211+ buf := make ([]byte , chunkSize )
212+ for {
213+ n , err := file .Read (buf )
214+ if err != nil && err != io .EOF {
215+ return outputMap , fmt .Errorf ("error reading file %s: %v" , path , err )
216+ }
217+ if n == 0 {
218+ break
219+ }
220+ if _ , err := part .Write (buf [:n ]); err != nil {
221+ return outputMap , fmt .Errorf ("error writing part for %s: %v" , path , err )
222+ }
209223 }
210224 }
211225
0 commit comments