switch to streaming multipart, reduces memory usage for uploads

bot-api-6.1
Syfaro 2015-08-10 07:41:44 -05:00
parent d06eead68d
commit 5a72ea6814
1 changed files with 9 additions and 25 deletions

View File

@ -1,14 +1,12 @@
package tgbotapi package tgbotapi
import ( import (
"bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io" "github.com/technoweenie/multipartstreamer"
"io/ioutil" "io/ioutil"
"log" "log"
"mime/multipart"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
@ -180,46 +178,32 @@ func (bot *BotAPI) MakeRequest(endpoint string, params url.Values) (APIResponse,
// //
// Requires the parameter to hold the file not be in the params. // Requires the parameter to hold the file not be in the params.
func (bot *BotAPI) UploadFile(endpoint string, params map[string]string, fieldname string, filename string) (APIResponse, error) { func (bot *BotAPI) UploadFile(endpoint string, params map[string]string, fieldname string, filename string) (APIResponse, error) {
var b bytes.Buffer
w := multipart.NewWriter(&b)
f, err := os.Open(filename) f, err := os.Open(filename)
if err != nil { if err != nil {
return APIResponse{}, err return APIResponse{}, err
} }
defer f.Close()
fw, err := w.CreateFormFile(fieldname, filename) fi, err := os.Stat(filename)
if err != nil { if err != nil {
return APIResponse{}, err return APIResponse{}, err
} }
if _, err = io.Copy(fw, f); err != nil { ms := multipartstreamer.New()
return APIResponse{}, err ms.WriteFields(params)
} ms.WriteReader(fieldname, f.Name(), fi.Size(), f)
for key, val := range params { req, err := http.NewRequest("POST", fmt.Sprintf(APIEndpoint, bot.Token, endpoint), nil)
if fw, err = w.CreateFormField(key); err != nil { ms.SetupRequest(req)
return APIResponse{}, err
}
if _, err = fw.Write([]byte(val)); err != nil {
return APIResponse{}, err
}
}
w.Close()
req, err := http.NewRequest("POST", fmt.Sprintf(APIEndpoint, bot.Token, endpoint), &b)
if err != nil { if err != nil {
return APIResponse{}, err return APIResponse{}, err
} }
req.Header.Set("Content-Type", w.FormDataContentType())
res, err := bot.Client.Do(req) res, err := bot.Client.Do(req)
if err != nil { if err != nil {
return APIResponse{}, err return APIResponse{}, err
} }
defer res.Body.Close()
bytes, err := ioutil.ReadAll(res.Body) bytes, err := ioutil.ReadAll(res.Body)
if err != nil { if err != nil {