-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmedia.go
137 lines (117 loc) · 3.52 KB
/
media.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
Copyright 2017-2018 Mikael Berthe
Licensed under the MIT license. Please see the LICENSE file is this directory.
*/
package madon
import (
"bytes"
"encoding/json"
"io"
"mime/multipart"
"os"
"path/filepath"
"github.com/pkg/errors"
"github.com/sendgrid/rest"
)
const mediaUploadFieldName = "file"
// UploadMedia uploads the given file and returns an attachment
// The description and focus arguments can be empty strings.
// 'focus' is the "focal point", written as two comma-delimited floating points.
func (mc *Client) UploadMedia(filePath, description, focus string) (*Attachment, error) {
if filePath == "" {
return nil, ErrInvalidParameter
}
f, err := os.Open(filePath)
if err != nil {
return nil, errors.Wrap(err, "cannot read file")
}
defer f.Close()
return mc.UploadMediaReader(f, filepath.Base(f.Name()), description, focus)
}
// UploadMediaReader uploads data from the given reader and returns an attachment
// name, description and focus arguments can be empty strings.
// 'focus' is the "focal point", written as two comma-delimited floating points.
func (mc *Client) UploadMediaReader(f io.Reader, name, description, focus string) (*Attachment, error) {
buf := bytes.Buffer{}
w := multipart.NewWriter(&buf)
var formWriter io.Writer
var err error
if len(name) > 0 {
formWriter, err = w.CreateFormFile(mediaUploadFieldName, name)
} else {
formWriter, err = w.CreateFormField(mediaUploadFieldName)
}
if err != nil {
return nil, errors.Wrap(err, "media upload")
}
if _, err = io.Copy(formWriter, f); err != nil {
return nil, errors.Wrap(err, "media upload")
}
var params apiCallParams
if description != "" || focus != "" {
params = make(apiCallParams)
if description != "" {
params["description"] = description
}
if focus != "" {
params["focus"] = focus
}
}
for k, v := range params {
fw, err := w.CreateFormField(k)
if err != nil {
return nil, errors.Wrapf(err, "form field: %s", k)
}
n, err := io.WriteString(fw, v)
if err != nil {
return nil, errors.Wrapf(err, "writing field: %s", k)
}
if n != len(v) {
return nil, errors.Wrapf(err, "partial field: %s", k)
}
}
w.Close()
req, err := mc.prepareRequest("v1/media", rest.Post, params)
if err != nil {
return nil, errors.Wrap(err, "media prepareRequest failed")
}
req.Headers["Content-Type"] = w.FormDataContentType()
req.Body = buf.Bytes()
// Make API call
r, err := restAPI(req)
if err != nil {
return nil, errors.Wrap(err, "media upload failed")
}
// Check for error reply
var errorResult Error
if err := json.Unmarshal([]byte(r.Body), &errorResult); err == nil {
// The empty object is not an error
if errorResult.Text != "" {
return nil, errors.New(errorResult.Text)
}
}
// Not an error reply; let's unmarshal the data
var attachment Attachment
err = json.Unmarshal([]byte(r.Body), &attachment)
if err != nil {
return nil, errors.Wrap(err, "cannot decode API response (media)")
}
return &attachment, nil
}
// UpdateMedia updates the description and focal point of a media
// One of the description and focus arguments can be nil to not be updated.
func (mc *Client) UpdateMedia(mediaID ActivityID, description, focus *string) (*Attachment, error) {
params := make(apiCallParams)
if description != nil {
params["description"] = *description
}
if focus != nil {
params["focus"] = *focus
}
endPoint := "media/" + mediaID
var attachment Attachment
if err := mc.apiCall("v1/"+endPoint, rest.Put, params, nil, nil, &attachment); err != nil {
return nil, err
}
return &attachment, nil
}