diff options
author | Alexander Kiryukhin <a.kiryukhin@mail.ru> | 2019-09-05 19:52:39 +0300 |
---|---|---|
committer | Alexander Kiryukhin <a.kiryukhin@mail.ru> | 2019-09-05 19:52:39 +0300 |
commit | 29322bf303815d477a83fee0e598006b651599a4 (patch) | |
tree | b5db3d62adb5b4376322e8cbe1de2a5ec12dee7f /uploads.go | |
parent | a9d0d8de0bd30fde69a93de4313f3b35060fd94e (diff) |
Upload file from urlv0.2.4
Diffstat (limited to 'uploads.go')
-rw-r--r-- | uploads.go | 39 |
1 files changed, 39 insertions, 0 deletions
@@ -9,6 +9,7 @@ import ( "net/http" "net/url" "os" + "path" ) type uploads struct { @@ -77,6 +78,44 @@ func (a *uploads) UploadMedia(endpoint *UploadEndpoint, filename string) (*Uploa return result, json.NewDecoder(resp.Body).Decode(result) } +//UploadMediaFromUrl uploads file from remote server to TamTam server +func (a *uploads) UploadMediaFromUrl(endpoint *UploadEndpoint, u url.URL) (*UploadedInfo, error) { + respFile, err := http.Get(u.String()) + if err != nil { + return nil, err + } + defer respFile.Body.Close() + bodyBuf := &bytes.Buffer{} + bodyWriter := multipart.NewWriter(bodyBuf) + fileWriter, err := bodyWriter.CreateFormFile("data", path.Base(u.Path)) + if err != nil { + return nil, err + } + _, err = io.Copy(fileWriter, respFile.Body) + if err != nil { + return nil, err + } + + if err := bodyWriter.Close(); err != nil { + return nil, err + } + contentType := bodyWriter.FormDataContentType() + if err := bodyWriter.Close(); err != nil { + return nil, err + } + 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{} |