Skip to content

Commit cb61107

Browse files
author
Saveliy Yudin
committed
fix: buffer multipart upload body to include Content-Length header
Max Bot API requires Content-Length for file uploads, returning HTTP 406 "File size is unknown" when streaming via io.Pipe. Replaced pipe-based streaming with in-memory buffering via bytes.Buffer.
1 parent 4a1d5fd commit cb61107

2 files changed

Lines changed: 18 additions & 16 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## [v0.2.1] - 2026-02-15
4+
5+
### Fixed
6+
- **Upload Content-Length**: `doUpload` now buffers multipart body in memory instead of streaming via `io.Pipe` — the Max Bot API requires a `Content-Length` header, which was missing with pipe-based streaming (HTTP 406: "File size is unknown")
7+
38
## [v0.2.0] - 2026-02-14
49

510
### Fixed

client.go

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -129,24 +129,21 @@ func (c *Client) doUpload(ctx context.Context, op, uploadURL, filename string, r
129129
defer cancel()
130130
}
131131

132-
pr, pw := io.Pipe()
133-
defer pr.Close() // ensure cleanup on all exit paths
134-
writer := multipart.NewWriter(pw)
132+
var buf bytes.Buffer
133+
writer := multipart.NewWriter(&buf)
135134

136-
go func() {
137-
part, err := writer.CreateFormFile("data", filename)
138-
if err != nil {
139-
pw.CloseWithError(err)
140-
return
141-
}
142-
if _, err := io.Copy(part, reader); err != nil {
143-
pw.CloseWithError(err)
144-
return
145-
}
146-
pw.CloseWithError(writer.Close())
147-
}()
135+
part, err := writer.CreateFormFile("data", filename)
136+
if err != nil {
137+
return nil, networkError(op, fmt.Errorf("create form file: %w", err))
138+
}
139+
if _, err := io.Copy(part, reader); err != nil {
140+
return nil, networkError(op, fmt.Errorf("copy file data: %w", err))
141+
}
142+
if err := writer.Close(); err != nil {
143+
return nil, networkError(op, fmt.Errorf("close multipart writer: %w", err))
144+
}
148145

149-
req, err := http.NewRequestWithContext(ctx, http.MethodPost, uploadURL, pr)
146+
req, err := http.NewRequestWithContext(ctx, http.MethodPost, uploadURL, &buf)
150147
if err != nil {
151148
return nil, networkError(op, fmt.Errorf("create upload request: %w", err))
152149
}

0 commit comments

Comments
 (0)