aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--uploads.go39
1 files changed, 39 insertions, 0 deletions
diff --git a/uploads.go b/uploads.go
index f56cad6..b6f30dc 100644
--- a/uploads.go
+++ b/uploads.go
@@ -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{}