Skip to content

Commit b66ed74

Browse files
authored
Merge pull request #13 from 4ier/fix/upload-timeout-context
refactor(client): use context.WithTimeout for upload timeout
2 parents 3d0fb6b + f1d4d11 commit b66ed74

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

internal/client/client.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package client
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/json"
67
"fmt"
78
"io"
@@ -294,7 +295,7 @@ func (c *Client) UploadFileContent(uploadID, fileName, contentType string, fileB
294295
body := &bytes.Buffer{}
295296
writer := multipart.NewWriter(body)
296297
partHeader := make(textproto.MIMEHeader)
297-
partHeader.Set("Content-Disposition", multipart.FileContentDisposition("file", fileName))
298+
partHeader.Set("Content-Disposition", fmt.Sprintf(`form-data; name="file"; filename="%s"`, strings.NewReplacer(`\`, `\\`, `"`, `\"`).Replace(fileName)))
298299
if contentType == "" {
299300
contentType = "application/octet-stream"
300301
}
@@ -323,10 +324,9 @@ func (c *Client) UploadFileContent(uploadID, fileName, contentType string, fileB
323324
fmt.Printf("→ POST %s (multipart, %d bytes)\n", url, body.Len())
324325
}
325326

326-
origTimeout := c.httpClient.Timeout
327-
c.httpClient.Timeout = UploadTimeout
328-
resp, err := c.httpClient.Do(req)
329-
c.httpClient.Timeout = origTimeout
327+
ctx, cancel := context.WithTimeout(context.Background(), UploadTimeout)
328+
defer cancel()
329+
resp, err := c.httpClient.Do(req.WithContext(ctx))
330330
if err != nil {
331331
return nil, fmt.Errorf("upload request failed: %w", err)
332332
}

0 commit comments

Comments
 (0)