17 |
17 |
18 "github.com/pkg/errors" |
18 "github.com/pkg/errors" |
19 "github.com/sendgrid/rest" |
19 "github.com/sendgrid/rest" |
20 ) |
20 ) |
21 |
21 |
|
22 const mediaUploadFieldName = "file" |
|
23 |
22 // UploadMedia uploads the given file and returns an attachment |
24 // UploadMedia uploads the given file and returns an attachment |
23 // The description and focus arguments can be empty strings. |
25 // The description and focus arguments can be empty strings. |
24 // 'focus' is the "focal point", written as two comma-delimited floating points. |
26 // 'focus' is the "focal point", written as two comma-delimited floating points. |
25 func (mc *Client) UploadMedia(filePath, description, focus string) (*Attachment, error) { |
27 func (mc *Client) UploadMedia(filePath, description, focus string) (*Attachment, error) { |
26 var b bytes.Buffer |
|
27 |
|
28 if filePath == "" { |
28 if filePath == "" { |
29 return nil, ErrInvalidParameter |
29 return nil, ErrInvalidParameter |
30 } |
30 } |
31 |
31 |
32 f, err := os.Open(filePath) |
32 f, err := os.Open(filePath) |
33 if err != nil { |
33 if err != nil { |
34 return nil, errors.Wrap(err, "cannot read file") |
34 return nil, errors.Wrap(err, "cannot read file") |
35 } |
35 } |
36 defer f.Close() |
36 defer f.Close() |
37 |
37 |
38 w := multipart.NewWriter(&b) |
38 return mc.UploadMediaReader(f, filepath.Base(f.Name()), description, focus) |
39 formWriter, err := w.CreateFormFile("file", filepath.Base(filePath)) |
39 } |
|
40 |
|
41 // UploadMediaReader uploads data from the given reader and returns an attachment |
|
42 // name, description and focus arguments can be empty strings. |
|
43 // 'focus' is the "focal point", written as two comma-delimited floating points. |
|
44 func (mc *Client) UploadMediaReader(f io.Reader, name, description, focus string) (*Attachment, error) { |
|
45 buf := bytes.Buffer{} |
|
46 |
|
47 w := multipart.NewWriter(&buf) |
|
48 var formWriter io.Writer |
|
49 var err error |
|
50 if len(name) > 0 { |
|
51 formWriter, err = w.CreateFormFile(mediaUploadFieldName, name) |
|
52 } else { |
|
53 formWriter, err = w.CreateFormField(mediaUploadFieldName) |
|
54 } |
40 if err != nil { |
55 if err != nil { |
41 return nil, errors.Wrap(err, "media upload") |
56 return nil, errors.Wrap(err, "media upload") |
42 } |
57 } |
|
58 |
43 if _, err = io.Copy(formWriter, f); err != nil { |
59 if _, err = io.Copy(formWriter, f); err != nil { |
44 return nil, errors.Wrap(err, "media upload") |
60 return nil, errors.Wrap(err, "media upload") |
45 } |
61 } |
46 |
62 |
47 w.Close() |
63 w.Close() |
60 req, err := mc.prepareRequest("v1/media", rest.Post, params) |
76 req, err := mc.prepareRequest("v1/media", rest.Post, params) |
61 if err != nil { |
77 if err != nil { |
62 return nil, errors.Wrap(err, "media prepareRequest failed") |
78 return nil, errors.Wrap(err, "media prepareRequest failed") |
63 } |
79 } |
64 req.Headers["Content-Type"] = w.FormDataContentType() |
80 req.Headers["Content-Type"] = w.FormDataContentType() |
65 req.Body = b.Bytes() |
81 req.Body = buf.Bytes() |
66 |
82 |
67 // Make API call |
83 // Make API call |
68 r, err := restAPI(req) |
84 r, err := restAPI(req) |
69 if err != nil { |
85 if err != nil { |
70 return nil, errors.Wrap(err, "media upload failed") |
86 return nil, errors.Wrap(err, "media upload failed") |