aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Kiryukhin <a.kiryukhin@mail.ru>2019-04-17 15:56:26 +0300
committerGitHub <noreply@github.com>2019-04-17 15:56:26 +0300
commitabd1ab76895ef4fef9256aaf2a4b2826756fabef (patch)
treedd0ba15e542cedf944ec3a34efa0c9df0406c859
parent8a0b9cfac18ff685e0c69bc61352931e0305c288 (diff)
parentc324f327acdec88a24cb09d5da08a85c8dffb6b1 (diff)
Merge pull request #1 from neonxp/file_uploadv0.1.1
WIP: upload images
-rw-r--r--api.go60
-rw-r--r--models.go6
2 files changed, 57 insertions, 9 deletions
diff --git a/api.go b/api.go
index 5444d9b..4d22f31 100644
--- a/api.go
+++ b/api.go
@@ -11,8 +11,10 @@ import (
"io"
"io/ioutil"
"log"
+ "mime/multipart"
"net/http"
"net/url"
+ "os"
"strconv"
"time"
)
@@ -68,6 +70,48 @@ func (a *Api) GetUploadURL(uploadType UploadType) (*UploadEndpoint, error) {
return result, json.NewDecoder(body).Decode(result)
}
+func (a *Api) UploadMedia(uploadType UploadType, filename string) (*UploadedInfo, error) {
+ bodyBuf := &bytes.Buffer{}
+ bodyWriter := multipart.NewWriter(bodyBuf)
+
+ // this step is very important
+ fileWriter, err := bodyWriter.CreateFormFile("data", filename)
+ if err != nil {
+ return nil, err
+ }
+
+ // open file handle
+ fh, err := os.Open(filename)
+ if err != nil {
+ return nil, err
+ }
+ defer fh.Close()
+
+ //iocopy
+ _, err = io.Copy(fileWriter, fh)
+ if err != nil {
+ return nil, err
+ }
+
+ if err := bodyWriter.Close(); err != nil {
+ return nil, err
+ }
+
+ target, err := a.GetUploadURL(uploadType)
+ if err != nil {
+ return nil, err
+ }
+ contentType := bodyWriter.FormDataContentType()
+
+ resp, err := http.Post(target.Url, contentType, bodyBuf)
+ if err != nil {
+ return nil, err
+ }
+ defer resp.Body.Close()
+ result := new(UploadedInfo)
+ return result, json.NewDecoder(resp.Body).Decode(result)
+}
+
func (a *Api) GetUpdatesLoop(ctx context.Context, updates chan interface{}) error {
for {
select {
@@ -479,20 +523,24 @@ func (a *Api) getUpdates(limit int, timeout int, marker int64, types []string) (
}
func (a *Api) request(method, path string, query url.Values, body interface{}) (io.ReadCloser, error) {
+ j, err := json.Marshal(body)
+ if err != nil {
+ return nil, err
+ }
+ return a.requestReader(method, path, query, bytes.NewReader(j))
+}
+
+func (a *Api) requestReader(method, path string, query url.Values, body io.Reader) (io.ReadCloser, error) {
c := http.DefaultClient
u := *a.url
u.Path = path
query.Set("access_token", a.key)
query.Set("v", a.version)
u.RawQuery = query.Encode()
- j, err := json.Marshal(body)
- if err != nil {
- return nil, err
- }
if a.logging {
- log.Printf("Sent: [%s %s] Query: %#v Body: %s", method, path, query, string(j))
+ log.Printf("Sent: [%s %s] Query: %#v", method, path, query)
}
- req, err := http.NewRequest(method, u.String(), bytes.NewReader(j))
+ req, err := http.NewRequest(method, u.String(), body)
if err != nil {
return nil, err
}
diff --git a/models.go b/models.go
index f2f13ea..0afd8f0 100644
--- a/models.go
+++ b/models.go
@@ -482,10 +482,10 @@ type PhotoAttachment struct {
type PhotoAttachmentPayload struct {
// Unique identifier of this image
- PhotoId int64 `json:"photo_id"`
- Token string `json:"token"`
+ PhotoId *int64 `json:"photo_id,omitempty"`
+ Token *string `json:"token,omitempty"`
// Image URL
- Url string `json:"url"`
+ Url *string `json:"url,omitempty"`
}
type PhotoAttachmentRequest struct {