diff options
author | Alexander Kiryukhin <a.kiryukhin@mail.ru> | 2019-04-17 15:06:56 +0300 |
---|---|---|
committer | Alexander Kiryukhin <a.kiryukhin@mail.ru> | 2019-04-17 15:06:56 +0300 |
commit | c324f327acdec88a24cb09d5da08a85c8dffb6b1 (patch) | |
tree | dd0ba15e542cedf944ec3a34efa0c9df0406c859 | |
parent | 8a0b9cfac18ff685e0c69bc61352931e0305c288 (diff) |
WIP: upload images
-rw-r--r-- | api.go | 60 | ||||
-rw-r--r-- | models.go | 6 |
2 files changed, 57 insertions, 9 deletions
@@ -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 } @@ -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 { |