Skip to content

Commit

Permalink
Merge pull request tucnak#313 from aofei/v2
Browse files Browse the repository at this point in the history
api: make sure io.Pipe in sendFiles will be closed
  • Loading branch information
tucnak authored Jun 18, 2020
2 parents 1a9d4b1 + a760478 commit b9ba8c4
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,18 @@ func (b *Bot) sendFiles(method string, files map[string]File, params map[string]

for field, file := range rawFiles {
if err := addFileToWriter(writer, params["file_name"], field, file); err != nil {
pipeWriter.CloseWithError(wrapError(err))
pipeWriter.CloseWithError(err)
return
}
}
for field, value := range params {
if err := writer.WriteField(field, value); err != nil {
pipeWriter.CloseWithError(wrapError(err))
pipeWriter.CloseWithError(err)
return
}
}
if err := writer.Close(); err != nil {
pipeWriter.CloseWithError(wrapError(err))
pipeWriter.CloseWithError(err)
return
}
}()
Expand All @@ -108,7 +108,9 @@ func (b *Bot) sendFiles(method string, files map[string]File, params map[string]

resp, err := b.client.Post(url, writer.FormDataContentType(), pipeReader)
if err != nil {
return nil, wrapError(err)
err = wrapError(err)
pipeReader.CloseWithError(err)
return nil, err
}
resp.Close = true
defer resp.Body.Close()
Expand Down

0 comments on commit b9ba8c4

Please sign in to comment.