Skip to content

Commit

Permalink
fix: create multipart form when multipart request (#1117)
Browse files Browse the repository at this point in the history
* fix: create multipart form when multipart request

* call createFormFields in go func()

del coment

* Trigger GitHub Actions

---------

Co-authored-by: Lorenzo Aiello <[email protected]>
  • Loading branch information
EkeMinusYou and lorenzoaiello committed Aug 15, 2024
1 parent 5345c06 commit 242df46
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
27 changes: 24 additions & 3 deletions misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,12 @@ func (e *RateLimitedError) Retryable() bool {
return true
}

func fileUploadReq(ctx context.Context, path string, values url.Values, r io.Reader) (*http.Request, error) {
func fileUploadReq(ctx context.Context, path string, r io.Reader) (*http.Request, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, path, r)
if err != nil {
return nil, err
}

req.URL.RawQuery = values.Encode()
return req, nil
}

Expand Down Expand Up @@ -155,9 +154,16 @@ func postLocalWithMultipartResponse(ctx context.Context, client httpClient, meth
func postWithMultipartResponse(ctx context.Context, client httpClient, path, name, fieldname, token string, values url.Values, r io.Reader, intf interface{}, d Debug) error {
pipeReader, pipeWriter := io.Pipe()
wr := multipart.NewWriter(pipeWriter)

errc := make(chan error)
go func() {
defer pipeWriter.Close()
defer wr.Close()
err := createFormFields(wr, values)
if err != nil {
errc <- err
return
}
ioWriter, err := wr.CreateFormFile(fieldname, name)
if err != nil {
errc <- err
Expand All @@ -173,7 +179,8 @@ func postWithMultipartResponse(ctx context.Context, client httpClient, path, nam
return
}
}()
req, err := fileUploadReq(ctx, path, values, pipeReader)

req, err := fileUploadReq(ctx, path, pipeReader)
if err != nil {
return err
}
Expand All @@ -199,6 +206,20 @@ func postWithMultipartResponse(ctx context.Context, client httpClient, path, nam
}
}

func createFormFields(mw *multipart.Writer, values url.Values) error {
for key, value := range values {
writer, err := mw.CreateFormField(key)
if err != nil {
return err
}
_, err = writer.Write([]byte(value[0]))
if err != nil {
return err
}
}
return nil
}

func doPost(ctx context.Context, client httpClient, req *http.Request, parser responseParser, d Debug) error {
resp, err := client.Do(req)
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions remotefiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,7 @@ func (api *Client) UpdateRemoteFile(fileID string, params RemoteFileParameters)
// Slack API docs: https://api.slack.com/methods/files.remote.update
func (api *Client) UpdateRemoteFileContext(ctx context.Context, fileID string, params RemoteFileParameters) (remotefile *RemoteFile, err error) {
response := &remoteFileResponseFull{}
values := url.Values{
"token": {api.token},
}
values := url.Values{}
if fileID != "" {
values.Add("file", fileID)
}
Expand All @@ -271,6 +269,7 @@ func (api *Client) UpdateRemoteFileContext(ctx context.Context, fileID string, p
if params.PreviewImageReader != nil {
err = postWithMultipartResponse(ctx, api.httpclient, api.endpoint+"files.remote.update", "preview.png", "preview_image", api.token, values, params.PreviewImageReader, response, api)
} else {
values.Add("token", api.token)
response, err = api.remoteFileRequest(ctx, "files.remote.update", values)
}

Expand Down

0 comments on commit 242df46

Please sign in to comment.