media.go
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--
UploadMedia: Add description and focus parameters
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
130
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 128
diff changeset
     1
/*
207
301d5b94be3f Update copyrights
Mikael Berthe <mikael@lilotux.net>
parents: 162
diff changeset
     2
Copyright 2017-2018 Mikael Berthe
130
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 128
diff changeset
     3
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 128
diff changeset
     4
Licensed under the MIT license.  Please see the LICENSE file is this directory.
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 128
diff changeset
     5
*/
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 128
diff changeset
     6
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
     7
package madon
126
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
import (
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
	"bytes"
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
	"encoding/json"
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
	"io"
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
	"mime/multipart"
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    14
	"os"
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
	"path/filepath"
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
162
68df3a01e1a7 Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents: 138
diff changeset
    17
	"github.com/pkg/errors"
126
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
	"github.com/sendgrid/rest"
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    19
)
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    20
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    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
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
	var b bytes.Buffer
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
	if filePath == "" {
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    28
		return nil, ErrInvalidParameter
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
	}
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    31
	f, err := os.Open(filePath)
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    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
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
	}
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    35
	defer f.Close()
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    36
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    37
	w := multipart.NewWriter(&b)
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    38
	formWriter, err := w.CreateFormFile("file", filepath.Base(filePath))
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    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
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
	}
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    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
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    44
	}
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    45
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    46
	w.Close()
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    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
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    63
	req.Headers["Content-Type"] = w.FormDataContentType()
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    64
	req.Body = b.Bytes()
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    65
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    66
	// Make API call
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    67
	r, err := restAPI(req)
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    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
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    70
	}
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    71
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    72
	// Check for error reply
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    73
	var errorResult Error
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    74
	if err := json.Unmarshal([]byte(r.Body), &errorResult); err == nil {
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    75
		// The empty object is not an error
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    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
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    78
		}
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    79
	}
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    80
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    81
	// Not an error reply; let's unmarshal the data
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    82
	var attachment Attachment
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    83
	err = json.Unmarshal([]byte(r.Body), &attachment)
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    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
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    86
	}
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    87
	return &attachment, nil
330c4b83f7d3 Add UploadMedia()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    88
}