changes to how sendfile works for protocol buffers

Signed-off-by: Michael <michael.lindman@gmail.com>
This commit is contained in:
Michael 2021-07-25 21:58:18 +01:00
parent 10350164e2
commit 80df1be3a6
1 changed files with 6 additions and 25 deletions

View File

@ -4,7 +4,6 @@ import (
"bytes"
"io/fs"
"io/ioutil"
"mime/multipart"
"strconv"
"git.0cd.xyz/michael/request"
@ -36,34 +35,16 @@ func WriteFile(path string, message protoreflect.ProtoMessage, perm fs.FileMode)
return nil
}
// SendFile protocol buffer as multipart file to http address
func SendFile(filename, url string, headers map[string]string, message protoreflect.ProtoMessage) (req *request.Response, err error) {
body, err := proto.Marshal(message)
// SendFile protocol buffer to http address
func SendFile(url string, headers map[string]string, message protoreflect.ProtoMessage) (req *request.Response, err error) {
proto, err := proto.Marshal(message)
if err != nil {
return nil, err
}
buf := &bytes.Buffer{}
writer := multipart.NewWriter(buf)
part, err := writer.CreateFormFile("file", filename)
if err != nil {
return nil, err
}
part.Write(body)
for key, val := range map[string]string{
"type": "upload",
"synchronous": "true",
} {
writer.WriteField(key, val)
}
writer.Close()
buf := bytes.NewBuffer(proto)
headers["Content-Type"] = "application/x-protobuf"
headers["Content-Length"] = strconv.Itoa(buf.Len())
headers["Content-Type"] = writer.FormDataContentType()
req, err = request.Post(url,
headers, buf)
req, err = request.Post(url, headers, buf)
if err != nil {
return nil, err
}