author | Mikael Berthe <mikael@lilotux.net> |
Sun, 13 Jan 2019 13:25:58 +0100 | |
changeset 247 | 17e32e951670 |
parent 238 | 1c0042e76902 |
child 250 | f2c8ab41725b |
permissions | -rw-r--r-- |
130 | 1 |
/* |
207 | 2 |
Copyright 2017-2018 Mikael Berthe |
130 | 3 |
|
4 |
Licensed under the MIT license. Please see the LICENSE file is this directory. |
|
5 |
*/ |
|
6 |
||
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
7 |
package madon |
126 | 8 |
|
9 |
import ( |
|
10 |
"bytes" |
|
11 |
"encoding/json" |
|
12 |
"io" |
|
13 |
"mime/multipart" |
|
14 |
"os" |
|
15 |
"path/filepath" |
|
216 | 16 |
"strconv" |
126 | 17 |
|
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
18 |
"github.com/pkg/errors" |
126 | 19 |
"github.com/sendgrid/rest" |
20 |
) |
|
21 |
||
22 |
// UploadMedia uploads the given file and returns an attachment |
|
215
40ed4e2db3b9
UploadMedia: Add description and focus parameters
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
23 |
// The description and focus arguments can be empty strings. |
40ed4e2db3b9
UploadMedia: Add description and focus parameters
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
24 |
// 'focus' is the "focal point", written as two comma-delimited floating points. |
40ed4e2db3b9
UploadMedia: Add description and focus parameters
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
25 |
func (mc *Client) UploadMedia(filePath, description, focus string) (*Attachment, error) { |
126 | 26 |
var b bytes.Buffer |
27 |
||
28 |
if filePath == "" { |
|
29 |
return nil, ErrInvalidParameter |
|
30 |
} |
|
31 |
||
32 |
f, err := os.Open(filePath) |
|
33 |
if err != nil { |
|
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
34 |
return nil, errors.Wrap(err, "cannot read file") |
126 | 35 |
} |
36 |
defer f.Close() |
|
37 |
||
38 |
w := multipart.NewWriter(&b) |
|
39 |
formWriter, err := w.CreateFormFile("file", filepath.Base(filePath)) |
|
40 |
if err != nil { |
|
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
41 |
return nil, errors.Wrap(err, "media upload") |
126 | 42 |
} |
43 |
if _, err = io.Copy(formWriter, f); err != nil { |
|
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
44 |
return nil, errors.Wrap(err, "media upload") |
126 | 45 |
} |
46 |
||
47 |
w.Close() |
|
48 |
||
215
40ed4e2db3b9
UploadMedia: Add description and focus parameters
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
49 |
var params apiCallParams |
40ed4e2db3b9
UploadMedia: Add description and focus parameters
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
50 |
if description != "" || focus != "" { |
40ed4e2db3b9
UploadMedia: Add description and focus parameters
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
51 |
params = make(apiCallParams) |
40ed4e2db3b9
UploadMedia: Add description and focus parameters
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
52 |
if description != "" { |
40ed4e2db3b9
UploadMedia: Add description and focus parameters
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
53 |
params["description"] = description |
40ed4e2db3b9
UploadMedia: Add description and focus parameters
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
54 |
} |
40ed4e2db3b9
UploadMedia: Add description and focus parameters
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
55 |
if focus != "" { |
40ed4e2db3b9
UploadMedia: Add description and focus parameters
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
56 |
params["focus"] = focus |
40ed4e2db3b9
UploadMedia: Add description and focus parameters
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
57 |
} |
40ed4e2db3b9
UploadMedia: Add description and focus parameters
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
58 |
} |
40ed4e2db3b9
UploadMedia: Add description and focus parameters
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
59 |
|
247
17e32e951670
Fix missing media API endpoint version
Mikael Berthe <mikael@lilotux.net>
parents:
238
diff
changeset
|
60 |
req, err := mc.prepareRequest("v1/media", rest.Post, params) |
128
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
126
diff
changeset
|
61 |
if err != nil { |
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
62 |
return nil, errors.Wrap(err, "media prepareRequest failed") |
128
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
126
diff
changeset
|
63 |
} |
126 | 64 |
req.Headers["Content-Type"] = w.FormDataContentType() |
65 |
req.Body = b.Bytes() |
|
66 |
||
67 |
// Make API call |
|
68 |
r, err := restAPI(req) |
|
69 |
if err != nil { |
|
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
70 |
return nil, errors.Wrap(err, "media upload failed") |
126 | 71 |
} |
72 |
||
73 |
// Check for error reply |
|
74 |
var errorResult Error |
|
75 |
if err := json.Unmarshal([]byte(r.Body), &errorResult); err == nil { |
|
76 |
// The empty object is not an error |
|
77 |
if errorResult.Text != "" { |
|
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
78 |
return nil, errors.New(errorResult.Text) |
126 | 79 |
} |
80 |
} |
|
81 |
||
82 |
// Not an error reply; let's unmarshal the data |
|
83 |
var attachment Attachment |
|
84 |
err = json.Unmarshal([]byte(r.Body), &attachment) |
|
85 |
if err != nil { |
|
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
86 |
return nil, errors.Wrap(err, "cannot decode API response (media)") |
126 | 87 |
} |
88 |
return &attachment, nil |
|
89 |
} |
|
216 | 90 |
|
91 |
// UpdateMedia updates the description and focal point of a media |
|
92 |
// One of the description and focus arguments can be nil to not be updated. |
|
93 |
func (mc *Client) UpdateMedia(mediaID int64, description, focus *string) (*Attachment, error) { |
|
94 |
params := make(apiCallParams) |
|
95 |
if description != nil { |
|
96 |
params["description"] = *description |
|
97 |
} |
|
98 |
if focus != nil { |
|
99 |
params["focus"] = *focus |
|
100 |
} |
|
101 |
||
102 |
endPoint := "media/" + strconv.FormatInt(mediaID, 10) |
|
103 |
var attachment Attachment |
|
238
1c0042e76902
Do not use a global API version
Mikael Berthe <mikael@lilotux.net>
parents:
216
diff
changeset
|
104 |
if err := mc.apiCall("v1/"+endPoint, rest.Put, params, nil, nil, &attachment); err != nil { |
216 | 105 |
return nil, err |
106 |
} |
|
107 |
return &attachment, nil |
|
108 |
} |