author | Mikael Berthe <mikael@lilotux.net> |
Tue, 20 Mar 2018 20:51:27 +0100 | |
changeset 215 | 40ed4e2db3b9 |
parent 207 | 301d5b94be3f |
child 216 | 403ce36a94ab |
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" |
|
16 |
||
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
17 |
"github.com/pkg/errors" |
126 | 18 |
"github.com/sendgrid/rest" |
19 |
) |
|
20 |
||
21 |
// 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
|
22 |
// 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
|
23 |
// '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
|
24 |
func (mc *Client) UploadMedia(filePath, description, focus string) (*Attachment, error) { |
126 | 25 |
var b bytes.Buffer |
26 |
||
27 |
if filePath == "" { |
|
28 |
return nil, ErrInvalidParameter |
|
29 |
} |
|
30 |
||
31 |
f, err := os.Open(filePath) |
|
32 |
if err != nil { |
|
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
33 |
return nil, errors.Wrap(err, "cannot read file") |
126 | 34 |
} |
35 |
defer f.Close() |
|
36 |
||
37 |
w := multipart.NewWriter(&b) |
|
38 |
formWriter, err := w.CreateFormFile("file", filepath.Base(filePath)) |
|
39 |
if err != nil { |
|
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
40 |
return nil, errors.Wrap(err, "media upload") |
126 | 41 |
} |
42 |
if _, err = io.Copy(formWriter, f); err != nil { |
|
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
43 |
return nil, errors.Wrap(err, "media upload") |
126 | 44 |
} |
45 |
||
46 |
w.Close() |
|
47 |
||
215
40ed4e2db3b9
UploadMedia: Add description and focus parameters
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
48 |
var params apiCallParams |
40ed4e2db3b9
UploadMedia: Add description and focus parameters
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
49 |
if description != "" || focus != "" { |
40ed4e2db3b9
UploadMedia: Add description and focus parameters
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
50 |
params = make(apiCallParams) |
40ed4e2db3b9
UploadMedia: Add description and focus parameters
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
51 |
if description != "" { |
40ed4e2db3b9
UploadMedia: Add description and focus parameters
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
52 |
params["description"] = description |
40ed4e2db3b9
UploadMedia: Add description and focus parameters
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
53 |
} |
40ed4e2db3b9
UploadMedia: Add description and focus parameters
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
54 |
if focus != "" { |
40ed4e2db3b9
UploadMedia: Add description and focus parameters
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
55 |
params["focus"] = focus |
40ed4e2db3b9
UploadMedia: Add description and focus parameters
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
56 |
} |
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 |
req, err := mc.prepareRequest("media", rest.Post, params) |
128
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
126
diff
changeset
|
60 |
if err != nil { |
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
61 |
return nil, errors.Wrap(err, "media prepareRequest failed") |
128
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
126
diff
changeset
|
62 |
} |
126 | 63 |
req.Headers["Content-Type"] = w.FormDataContentType() |
64 |
req.Body = b.Bytes() |
|
65 |
||
66 |
// Make API call |
|
67 |
r, err := restAPI(req) |
|
68 |
if err != nil { |
|
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
69 |
return nil, errors.Wrap(err, "media upload failed") |
126 | 70 |
} |
71 |
||
72 |
// Check for error reply |
|
73 |
var errorResult Error |
|
74 |
if err := json.Unmarshal([]byte(r.Body), &errorResult); err == nil { |
|
75 |
// The empty object is not an error |
|
76 |
if errorResult.Text != "" { |
|
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
77 |
return nil, errors.New(errorResult.Text) |
126 | 78 |
} |
79 |
} |
|
80 |
||
81 |
// Not an error reply; let's unmarshal the data |
|
82 |
var attachment Attachment |
|
83 |
err = json.Unmarshal([]byte(r.Body), &attachment) |
|
84 |
if err != nil { |
|
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
85 |
return nil, errors.Wrap(err, "cannot decode API response (media)") |
126 | 86 |
} |
87 |
return &attachment, nil |
|
88 |
} |