-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.go
68 lines (60 loc) · 1.62 KB
/
upload.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/snapas/go-snapas"
"github.com/writeas/impart"
"io"
"mime/multipart"
"net/http"
"os"
)
// UploadPhoto uploads a photo, and returns a Snap.as Photo. See:
// https://developers.snap.as/docs/api/#upload-a-photo
func UploadPhoto(client *snapas.Client, fileName, fileTag string) (*snapas.Photo, error) {
f, err := os.Open(fileName)
if err != nil {
return nil, fmt.Errorf("open file: %s", err)
}
defer func() { _ = f.Close() }()
body := &bytes.Buffer{}
w := multipart.NewWriter(body)
part, err := w.CreateFormFile("file", fileTag)
if err != nil {
return nil, fmt.Errorf("create form file: %s", err)
}
_, err = io.Copy(part, f)
if err != nil {
return nil, fmt.Errorf("copy file: %s", err)
}
err = w.Close()
if err != nil {
return nil, fmt.Errorf("close writer: %s", err)
}
url := fmt.Sprintf("%s%s", client.Config.BaseURL, "/photos/upload")
req, err := http.NewRequest("POST", url, body)
if err != nil {
return nil, fmt.Errorf("create request: %s", err)
}
req.Header.Add("User-Agent", client.Config.UserAgent)
req.Header.Add("Content-Type", w.FormDataContentType())
req.Header.Add("Authorization", client.Token)
resp, err := client.Config.Client.Do(req)
if err != nil {
return nil, fmt.Errorf("request: %s", err)
}
defer func() { _ = resp.Body.Close() }()
env := &impart.Envelope{
Code: resp.StatusCode,
Data: &snapas.Photo{},
}
err = json.NewDecoder(resp.Body).Decode(&env)
if err != nil {
return nil, err
}
if env.Code != http.StatusCreated {
return nil, fmt.Errorf("%s", env.ErrorMessage)
}
return env.Data.(*snapas.Photo), nil
}