diff options
author | Alexander Kiryukhin <a.kiryukhin@mail.ru> | 2019-08-09 17:52:33 +0300 |
---|---|---|
committer | Alexander Kiryukhin <a.kiryukhin@mail.ru> | 2019-08-09 17:52:48 +0300 |
commit | b87e42f3294318244deae8126b3ac18d543fd3d7 (patch) | |
tree | 2b4fe1f84b07ddf80a6cb863c385293c5d2c4496 /uploads.go | |
parent | a8ca31679d01ddfe461cadb994131986639b4ea1 (diff) |
fix uploadsv0.2.0
Diffstat (limited to 'uploads.go')
-rw-r--r-- | uploads.go | 49 |
1 files changed, 45 insertions, 4 deletions
@@ -37,7 +37,7 @@ func (a *uploads) GetUploadURL(uploadType UploadType) (*UploadEndpoint, error) { } //UploadMedia uploads file to TamTam server -func (a *uploads) UploadMedia(uploadType UploadType, filename string) (*UploadedInfo, error) { +func (a *uploads) UploadMedia(endpoint *UploadEndpoint, filename string) (*UploadedInfo, error) { bodyBuf := &bytes.Buffer{} bodyWriter := multipart.NewWriter(bodyBuf) @@ -63,14 +63,55 @@ func (a *uploads) UploadMedia(uploadType UploadType, filename string) (*Uploaded if err := bodyWriter.Close(); err != nil { return nil, err } + contentType := bodyWriter.FormDataContentType() + resp, err := http.Post(endpoint.Url, contentType, bodyBuf) + if err != nil { + return nil, err + } + defer func() { + if err := resp.Body.Close(); err != nil { + log.Println(err) + } + }() + result := new(UploadedInfo) + return result, json.NewDecoder(resp.Body).Decode(result) +} + +//UploadPhoto uploads photos to TamTam server +func (a *uploads) UploadPhoto(filename string) (*PhotoTokens, error) { + bodyBuf := &bytes.Buffer{} + bodyWriter := multipart.NewWriter(bodyBuf) - target, err := a.GetUploadURL(uploadType) + fileWriter, err := bodyWriter.CreateFormFile("data", filename) + if err != nil { + return nil, err + } + + fh, err := os.Open(filename) + if err != nil { + return nil, err + } + defer func() { + if err := fh.Close(); err != nil { + log.Println(err) + } + }() + _, err = io.Copy(fileWriter, fh) + if err != nil { + return nil, err + } + + if err := bodyWriter.Close(); err != nil { + return nil, err + } + + endpoint, err := a.GetUploadURL(PHOTO) if err != nil { return nil, err } contentType := bodyWriter.FormDataContentType() - resp, err := http.Post(target.Url, contentType, bodyBuf) + resp, err := http.Post(endpoint.Url, contentType, bodyBuf) if err != nil { return nil, err } @@ -79,6 +120,6 @@ func (a *uploads) UploadMedia(uploadType UploadType, filename string) (*Uploaded log.Println(err) } }() - result := new(UploadedInfo) + result := new(PhotoTokens) return result, json.NewDecoder(resp.Body).Decode(result) } |