6
6
"encoding/base64"
7
7
"fmt"
8
8
"io"
9
+ "math"
9
10
"mime/multipart"
10
11
"net/http"
11
12
"net/textproto"
@@ -51,8 +52,10 @@ func (c *Client) DoMultiPartRequest(method, endpoint string, files map[string][]
51
52
if err != nil {
52
53
return nil , err
53
54
}
55
+
54
56
// Log the constructed request body for debugging
55
57
logMultiPartRequestBody (body , log )
58
+
56
59
// Construct the full URL for the API endpoint.
57
60
url := c .APIHandler .ConstructAPIResourceEndpoint (endpoint , log )
58
61
@@ -77,9 +80,7 @@ func (c *Client) DoMultiPartRequest(method, endpoint string, files map[string][]
77
80
headerHandler .SetRequestHeaders (endpoint )
78
81
headerHandler .LogHeaders (c .clientConfig .ClientOptions .Logging .HideSensitiveData )
79
82
80
- // Start tracking upload time
81
83
startTime := time .Now ()
82
-
83
84
resp , err := c .httpClient .Do (req )
84
85
if err != nil {
85
86
log .Error ("Failed to send request" , zap .String ("method" , method ), zap .String ("endpoint" , endpoint ), zap .Error (err ))
@@ -231,20 +232,31 @@ func chunkFileUpload(file *os.File, writer io.Writer, log logger.Logger, updateP
231
232
return nil
232
233
}
233
234
234
- // logUploadProgress logs the upload progress based on the percentage of the total file size .
235
- func logUploadProgress (fileSize int64 , log logger.Logger ) func (int64 ) {
236
- var uploaded int64 = 0
237
- const logInterval = 5 // Log every 5% increment
238
- lastLoggedPercentage := int64 ( 0 )
235
+ // logUploadProgress logs the upload progress based on the percentage of the total upload .
236
+ func logUploadProgress (totalSize int64 , log logger.Logger ) func (int64 ) {
237
+ var uploadedSize int64
238
+ var lastLoggedPercentage float64
239
+ startTime := time . Now ( )
239
240
240
241
return func (bytesWritten int64 ) {
241
- uploaded += bytesWritten
242
- percentage := (uploaded * 100 ) / fileSize
243
-
244
- if percentage >= lastLoggedPercentage + logInterval {
245
- log .Debug ("Upload progress" , zap .Int64 ("uploaded_bytes" , uploaded ), zap .Int64 ("total_bytes" , fileSize ), zap .Int64 ("percentage" , percentage ))
242
+ uploadedSize += bytesWritten
243
+ percentage := math .Floor (float64 (uploadedSize ) / float64 (totalSize ) * 100 )
244
+ uploadedMB := float64 (uploadedSize ) / (1024 * 1024 )
245
+
246
+ if percentage != lastLoggedPercentage {
247
+ log .Info ("File upload progress" ,
248
+ zap .String ("completed" , fmt .Sprintf ("%.0f%%" , percentage )),
249
+ zap .Float64 ("uploaded_megabytes" , uploadedMB ),
250
+ zap .Duration ("elapsed_time" , time .Since (startTime )))
246
251
lastLoggedPercentage = percentage
247
252
}
253
+
254
+ if uploadedSize == totalSize {
255
+ totalTime := time .Since (startTime )
256
+ log .Info ("File upload completed" ,
257
+ zap .Float64 ("total_uploaded_megabytes" , float64 (uploadedSize )/ (1024 * 1024 )),
258
+ zap .Duration ("total_upload_time" , totalTime ))
259
+ }
248
260
}
249
261
}
250
262
0 commit comments