@@ -30,32 +30,31 @@ type UploadState struct {
30
30
// logging throughout the process.
31
31
32
32
// Parameters:
33
- // - method: A string representing the HTTP method to be used for the request. This method should be either POST or PUT
34
- // as these are the only methods that support multipart/form-data requests.
35
- // - endpoint: The target API endpoint for the request. This should be a relative path that will be appended to the base URL
36
- // configured for the HTTP client.
37
- // - files: A map where the key is the field name and the value is a slice of file paths to be included in the request.
38
- // - formDataFields: A map of additional form fields to be included in the multipart request, where the key is the field name
39
- // and the value is the field value.
40
- // - fileContentTypes: A map specifying the content type for each file part. The key is the field name and the value is the
41
- // content type (e.g., "image/jpeg").
42
- // - formDataPartHeaders: A map specifying custom headers for each part of the multipart form data. The key is the field name
43
- // and the value is an http.Header containing the headers for that part.
44
- // - out: A pointer to an output variable where the response will be deserialized. This should be a pointer to a struct that
45
- // matches the expected response schema.
46
-
33
+ // - method: A string representing the HTTP method to be used for the request. This method should be either POST or PUT
34
+ // as these are the only methods that support multipart/form-data requests.
35
+ // - endpoint: The target API endpoint for the request. This should be a relative path that will be appended to the base URL
36
+ // configured for the HTTP client.
37
+ // - files: A map where the key is the field name and the value is a slice of file paths to be included in the request.
38
+ // - formDataFields: A map of additional form fields to be included in the multipart request, where the key is the field name
39
+ // and the value is the field value.
40
+ // - fileContentTypes: A map specifying the content type for each file part. The key is the field name and the value is the
41
+ // content type (e.g., "image/jpeg").
42
+ // - formDataPartHeaders: A map specifying custom headers for each part of the multipart form data. The key is the field name
43
+ // and the value is an http.Header containing the headers for that part.
44
+ // - out: A pointer to an output variable where the response will be deserialized. This should be a pointer to a struct that
45
+ // matches the expected response schema.
46
+ //
47
47
// Returns:
48
- // - *http.Response: The HTTP response received from the server. In case of successful execution, this response contains
49
- // the status code, headers, and body of the response. In case of errors, this response may contain the last received
50
- // HTTP response that led to the failure.
51
- // - error: An error object indicating failure during request execution. This could be due to network issues, server errors,
52
- // or a failure in request serialization/deserialization.
53
-
48
+ // - *http.Response: The HTTP response received from the server. In case of successful execution, this response contains
49
+ // the status code, headers, and body of the response. In case of errors, this response may contain the last received
50
+ // HTTP response that led to the failure.
51
+ // - error: An error object indicating failure during request execution. This could be due to network issues, server errors,
52
+ // or a failure in request serialization/deserialization.
53
+ //
54
54
// Usage:
55
55
// This function is suitable for executing multipart/form-data HTTP requests, particularly for file uploads along with
56
56
// additional form fields. It ensures proper authentication, sets necessary headers, and logs the process for debugging
57
57
// and monitoring purposes.
58
-
59
58
// Example:
60
59
// var result MyResponseType
61
60
// resp, err := client.DoMultiPartRequest("POST", "/api/upload", files, formDataFields, fileContentTypes, formDataPartHeaders, &result)
@@ -142,19 +141,18 @@ func (c *Client) DoMultiPartRequest(method, endpoint string, files map[string][]
142
141
// This function constructs the body of a multipart/form-data request using an io.Pipe, allowing the request to be sent in chunks.
143
142
// It supports custom content types and headers for each part of the multipart request, and logs the process for debugging
144
143
// and monitoring purposes.
145
-
146
144
// Parameters:
147
- // - files: A map where the key is the field name and the value is a slice of file paths to be included in the request.
148
- // Each file path corresponds to a file that will be included in the multipart request.
149
- // - formDataFields: A map of additional form fields to be included in the multipart request, where the key is the field name
150
- // and the value is the field value. These are regular form fields that accompany the file uploads.
151
- // - fileContentTypes: A map specifying the content type for each file part. The key is the field name and the value is the
152
- // content type (e.g., "image/jpeg").
153
- // - formDataPartHeaders: A map specifying custom headers for each part of the multipart form data. The key is the field name
154
- // and the value is an http.Header containing the headers for that part.
155
- // - sugar: An instance of a logger implementing the logger.Logger interface, used to sugar informational messages, warnings,
156
- // and errors encountered during the construction of the multipart request body.
157
-
145
+ // - files: A map where the key is the field name and the value is a slice of file paths to be included in the request.
146
+ // Each file path corresponds to a file that will be included in the multipart request.
147
+ // - formDataFields: A map of additional form fields to be included in the multipart request, where the key is the field name
148
+ // and the value is the field value. These are regular form fields that accompany the file uploads.
149
+ // - fileContentTypes: A map specifying the content type for each file part. The key is the field name and the value is the
150
+ // content type (e.g., "image/jpeg").
151
+ // - formDataPartHeaders: A map specifying custom headers for each part of the multipart form data. The key is the field name
152
+ // and the value is an http.Header containing the headers for that part.
153
+ // - sugar: An instance of a logger implementing the logger.Logger interface, used to sugar informational messages, warnings,
154
+ // and errors encountered during the construction of the multipart request body.
155
+ //
158
156
// Returns:
159
157
// - io.Reader: The constructed multipart request body reader. This reader streams the multipart form data payload ready to be sent.
160
158
// - string: The content type of the multipart request body. This includes the boundary string used by the multipart writer.
@@ -200,18 +198,17 @@ func createStreamingMultipartRequestBody(files map[string][]string, formDataFiel
200
198
201
199
// addFilePart adds a base64 encoded file part to the multipart writer with the provided field name and file path.
202
200
// This function opens the specified file, sets the appropriate content type and headers, and adds it to the multipart writer.
203
-
204
201
// Parameters:
205
- // - writer: The multipart writer used to construct the multipart request body.
206
- // - fieldName: The field name for the file part.
207
- // - filePath: The path to the file to be included in the request.
208
- // - fileContentTypes: A map specifying the content type for each file part. The key is the field name and the value is the
209
- // content type (e.g., "image/jpeg").
210
- // - formDataPartHeaders: A map specifying custom headers for each part of the multipart form data. The key is the field name
211
- // and the value is an http.Header containing the headers for that part.
212
- // - sugar: An instance of a logger implementing the logger.Logger interface, used to sugar informational messages, warnings,
213
- // and errors encountered during the addition of the file part.
214
-
202
+ // - writer: The multipart writer used to construct the multipart request body.
203
+ // - fieldName: The field name for the file part.
204
+ // - filePath: The path to the file to be included in the request.
205
+ // - fileContentTypes: A map specifying the content type for each file part. The key is the field name and the value is the
206
+ // content type (e.g., "image/jpeg").
207
+ // - formDataPartHeaders: A map specifying custom headers for each part of the multipart form data. The key is the field name
208
+ // and the value is an http.Header containing the headers for that part.
209
+ // - sugar: An instance of a logger implementing the logger.Logger interface, used to sugar informational messages, warnings,
210
+ // and errors encountered during the addition of the file part.
211
+ //
215
212
// Returns:
216
213
// - error: An error object indicating failure during the addition of the file part. This could be due to issues such as
217
214
// file reading errors or multipart writer errors.
@@ -258,14 +255,13 @@ func addFilePart(writer *multipart.Writer, fieldName, filePath string, fileConte
258
255
259
256
// addFormField adds a form field to the multipart writer with the provided key and value.
260
257
// This function adds a regular form field (non-file) to the multipart request body.
261
-
262
258
// Parameters:
263
- // - writer: The multipart writer used to construct the multipart request body.
264
- // - key: The field name for the form field.
265
- // - val: The value of the form field.
266
- // - sugar: An instance of a logger implementing the logger.Logger interface, used to sugar informational messages, warnings,
267
- // and errors encountered during the addition of the form field.
268
-
259
+ // - writer: The multipart writer used to construct the multipart request body.
260
+ // - key: The field name for the form field.
261
+ // - val: The value of the form field.
262
+ // - sugar: An instance of a logger implementing the logger.Logger interface, used to sugar informational messages, warnings,
263
+ // and errors encountered during the addition of the form field.
264
+ //
269
265
// Returns:
270
266
// - error: An error object indicating failure during the addition of the form field. This could be due to issues such as
271
267
// multipart writer errors.
@@ -285,14 +281,13 @@ func addFormField(writer *multipart.Writer, key, val string, sugar *zap.SugaredL
285
281
// setFormDataPartHeader creates a textproto.MIMEHeader for a form data field with the provided field name, file name, content type, and custom headers.
286
282
// This function constructs the MIME headers for a multipart form data part, including the content disposition, content type,
287
283
// and any custom headers specified.
288
-
289
284
// Parameters:
290
- // - fieldname: The name of the form field.
291
- // - filename: The name of the file being uploaded (if applicable).
292
- // - contentType: The content type of the form data part (e.g., "image/jpeg").
293
- // - customHeaders: A map of custom headers to be added to the form data part. The key is the header name and the value is the
294
- // header value.
295
-
285
+ // - fieldname: The name of the form field.
286
+ // - filename: The name of the file being uploaded (if applicable).
287
+ // - contentType: The content type of the form data part (e.g., "image/jpeg").
288
+ // - customHeaders: A map of custom headers to be added to the form data part. The key is the header name and the value is the
289
+ // header value.
290
+ //
296
291
// Returns:
297
292
// - textproto.MIMEHeader: The constructed MIME header for the form data part.
298
293
func setFormDataPartHeader (fieldname , filename , contentType string , customHeaders http.Header ) textproto.MIMEHeader {
@@ -311,21 +306,18 @@ func setFormDataPartHeader(fieldname, filename, contentType string, customHeader
311
306
// chunkFileUpload reads the file upload into chunks and writes it to the writer.
312
307
// This function reads the file in chunks and writes it to the provided writer, allowing for progress logging during the upload.
313
308
// The chunk size is set to 8192 KB (8 MB) by default. This is a common chunk size used for file uploads to cloud storage services.
314
-
315
309
// Azure Blob Storage has a minimum chunk size of 4 MB and a maximum of 100 MB for block blobs.
316
310
// GCP Cloud Storage has a minimum chunk size of 256 KB and a maximum of 5 GB.
317
311
// AWS S3 has a minimum chunk size of 5 MB and a maximum of 5 GB.
318
-
319
312
// The function also calculates the total number of chunks and logs the chunk number during the upload process.
320
-
321
313
// Parameters:
322
- // - file: The file to be uploaded.
323
- // - writer: The writer to which the file content will be written.
324
- // - sugar: An instance of a logger implementing the logger.Logger interface, used to sugar informational messages, warnings,
325
- // and errors encountered during the file upload.
326
- // - updateProgress: A function to update the upload progress, typically used for logging purposes.
327
- // - uploadState: A pointer to an UploadState struct used to track the progress of the file upload for resumable uploads.
328
-
314
+ // - file: The file to be uploaded.
315
+ // - writer: The writer to which the file content will be written.
316
+ // - sugar: An instance of a logger implementing the logger.Logger interface, used to sugar informational messages, warnings,
317
+ // and errors encountered during the file upload.
318
+ // - updateProgress: A function to update the upload progress, typically used for logging purposes.
319
+ // - uploadState: A pointer to an UploadState struct used to track the progress of the file upload for resumable uploads.
320
+ //
329
321
// Returns:
330
322
// - error: An error object indicating failure during the file upload. This could be due to issues such as file reading errors
331
323
// or writer errors.
@@ -397,13 +389,12 @@ func chunkFileUpload(file *os.File, writer io.Writer, updateProgress func(int64)
397
389
398
390
// logUploadProgress logs the upload progress based on the percentage of the total file size.
399
391
// This function returns a closure that logs the upload progress each time it is called, updating the percentage completed.
400
-
401
392
// Parameters:
402
- // - file: The file being uploaded. used for logging the file name.
403
- // - fileSize: The total size of the file being uploaded.
404
- // - sugar: An instance of a logger implementing the logger.Logger interface, used to sugar informational messages, warnings,
405
- // and errors encountered during the upload.
406
-
393
+ // - file: The file being uploaded. used for logging the file name.
394
+ // - fileSize: The total size of the file being uploaded.
395
+ // - sugar: An instance of a logger implementing the logger.Logger interface, used to sugar informational messages, warnings,
396
+ // and errors encountered during the upload.
397
+ //
407
398
// Returns:
408
399
// - func(int64): A function that takes the number of bytes written as an argument and logs the upload progress.
409
400
// logUploadProgress logs the upload progress based on the percentage of the total file size.
0 commit comments