Skip to content

Commit

Permalink
Merge pull request #223 from deploymenttheory/dev-multipartfix
Browse files Browse the repository at this point in the history
Dev multipartfix
  • Loading branch information
ShocOne authored Jun 6, 2024
2 parents 383cc93 + 44f3863 commit 5bf47a5
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions httpclient/multipartrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,9 @@ func setFormDataPartHeader(fieldname, filename, contentType string, customHeader
return header
}

// chunkFileUpload reads the file in chunks and writes it to the writer.
// chunkFileUpload reads the file upload into chunks and writes it to the writer.
// This function reads the file in chunks and writes it to the provided writer, allowing for progress logging during the upload.
// chunk size is set to 5124 KB (5 MB) by default.

// Parameters:
// - file: The file to be uploaded.
Expand All @@ -338,10 +339,11 @@ func setFormDataPartHeader(fieldname, filename, contentType string, customHeader
// - error: An error object indicating failure during the file upload. This could be due to issues such as file reading errors
// or writer errors.
func chunkFileUpload(file *os.File, writer io.Writer, log logger.Logger, updateProgress func(int64)) error {
const chunkSize = 2024 * 1024 // 2024 KB in bytes, adjust this as needed
const chunkSize = 5124 * 1024 // 5124 KB in bytes (5 MB)
buffer := make([]byte, chunkSize)
totalWritten := int64(0)
chunkWritten := int64(0)
fileName := filepath.Base(file.Name())

for {
n, err := file.Read(buffer)
Expand All @@ -361,16 +363,21 @@ func chunkFileUpload(file *os.File, writer io.Writer, log logger.Logger, updateP
chunkWritten += int64(written)
updateProgress(int64(written))

// Log progress for every 1MB chunk written
if chunkWritten >= chunkSize {
log.Debug("Chunk written", zap.Int64("bytes_written", chunkWritten), zap.Int64("total_written", totalWritten))
log.Debug("File Upload Chunk Sent",
zap.String("file_name", fileName),
zap.Int64("kb_sent", chunkWritten/1024),
zap.Int64("total_kb_sent", totalWritten/1024))
chunkWritten = 0
}
}

// Log any remaining bytes that were written but didn't reach the log threshold
if chunkWritten > 0 {
log.Debug("Final chunk written", zap.Int64("bytes_written", chunkWritten), zap.Int64("total_written", totalWritten))
log.Debug("Final Upload Chunk Sent",
zap.String("file_name", fileName),
zap.Int64("kb_sent", chunkWritten/1024),
zap.Int64("total_kb_sent", totalWritten/1024))
}

return nil
Expand Down

0 comments on commit 5bf47a5

Please sign in to comment.