aboutsummaryrefslogtreecommitdiff
path: root/uploads.go
diff options
context:
space:
mode:
Diffstat (limited to 'uploads.go')
-rw-r--r--uploads.go49
1 files changed, 45 insertions, 4 deletions
diff --git a/uploads.go b/uploads.go
index e953aab..f56cad6 100644
--- a/uploads.go
+++ b/uploads.go
@@ -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)
}