Skip to content

Dev multipartfix #223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading