@@ -5,12 +5,9 @@ import (
55 "bytes"
66 "encoding/json"
77 "encoding/xml"
8- "io"
98 "mime/multipart"
10- "path/filepath"
119 "strings"
1210
13- "github.com/deploymenttheory/go-api-http-client/helpers"
1411 "github.com/deploymenttheory/go-api-http-client/logger"
1512 "go.uber.org/zap"
1613)
@@ -57,42 +54,49 @@ func (j *JamfAPIHandler) MarshalRequest(body interface{}, method string, endpoin
5754}
5855
5956// MarshalMultipartRequest handles multipart form data encoding with secure file handling and returns the encoded body and content type.
60- func (j * JamfAPIHandler ) MarshalMultipartRequest (fields map [string ]string , files map [string ]string , log logger.Logger ) ([]byte , string , error ) {
61- body := & bytes.Buffer {}
62- writer := multipart .NewWriter (body )
63-
64- // Add the simple fields to the form data
65- for field , value := range fields {
66- if err := writer .WriteField (field , value ); err != nil {
67- return nil , "" , err
57+ func (j * JamfAPIHandler ) MarshalMultipartRequest (formFields map [string ]string , fileContents map [string ][]byte , log * zap.Logger ) ([]byte , string , string , error ) {
58+ const snippetLength = 20
59+ var b bytes.Buffer
60+ writer := multipart .NewWriter (& b )
61+
62+ // Log form fields
63+ for key , val := range formFields {
64+ err := writer .WriteField (key , val )
65+ if err != nil {
66+ log .Error ("Failed to add form field to multipart request" , zap .String ("key" , key ), zap .Error (err ))
67+ return nil , "" , "" , err
6868 }
69+ log .Debug ("Added form field" , zap .String ("key" , key ), zap .String ("value" , val ))
6970 }
7071
71- // Add the files to the form data, using safeOpenFile to ensure secure file access
72- for formField , filePath := range files {
73- file , err := helpers .SafeOpenFile (filePath )
74- if err != nil {
75- log .Error ("Failed to open file securely" , zap .String ("file" , filePath ), zap .Error (err ))
76- return nil , "" , err
72+ // Log file contents snippets
73+ for key , val := range fileContents {
74+ contentSnippet := string (val )
75+ if len (contentSnippet ) > snippetLength {
76+ contentSnippet = contentSnippet [:snippetLength ] + "..."
7777 }
78- defer file . Close ( )
78+ log . Debug ( "File content snippet" , zap . String ( "key" , key ), zap . String ( "snippet" , contentSnippet ) )
7979
80- part , err := writer .CreateFormFile (formField , filepath . Base ( filePath ) )
80+ part , err := writer .CreateFormFile (key , key )
8181 if err != nil {
82- return nil , "" , err
82+ log .Error ("Failed to create form file in multipart request" , zap .String ("key" , key ), zap .Error (err ))
83+ return nil , "" , "" , err
8384 }
84- if _ , err := io .Copy (part , file ); err != nil {
85- return nil , "" , err
85+ _ , err = part .Write (val )
86+ if err != nil {
87+ log .Error ("Failed to write file to multipart request" , zap .String ("key" , key ), zap .Error (err ))
88+ return nil , "" , "" , err
8689 }
8790 }
8891
89- // Close the writer to finish writing the multipart message
90- if err := writer .Close (); err != nil {
91- return nil , "" , err
92+ // Close the writer
93+ err := writer .Close ()
94+ if err != nil {
95+ log .Error ("Failed to close multipart writer" , zap .Error (err ))
96+ return nil , "" , "" , err
9297 }
9398
94- contentType := writer .FormDataContentType ()
95- log .Debug ("Multipart request body" , zap .String ("Body" , body .String ())) // Log the body
99+ log .Debug ("Multipart request constructed" , zap .Any ("formFields" , formFields ))
96100
97- return body .Bytes (), contentType , nil
101+ return b .Bytes (), writer . FormDataContentType (), b . String ()[: snippetLength ] , nil
98102}
0 commit comments