Repository navigation
Expand file tree
/
Copy pathuploads.go
More file actions
139 lines (118 loc) · 4.31 KB
/
Copy pathuploads.go
File metadata and controls
139 lines (118 loc) · 4.31 KB
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
138
139
package loops
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
// CreateUploadRequest is the request body for [Client.CreateUpload]. The
// API restricts ContentType to a small set of image types (currently
// image/jpeg, image/png, image/gif and image/webp) and enforces a maximum
// ContentLength; see the Loops API docs for the current limits.
type CreateUploadRequest struct {
ContentType string `json:"contentType"`
ContentLength int64 `json:"contentLength"`
}
// CreateUploadResponse is returned by [Client.CreateUpload]. PresignedURL
// is a short-lived S3 URL that the caller must PUT the asset bytes to
// before calling [Client.CompleteUpload] with EmailAssetID.
type CreateUploadResponse struct {
EmailAssetID string `json:"emailAssetId"`
PresignedURL string `json:"presignedUrl"`
}
// CompleteUploadResponse is returned by [Client.CompleteUpload] and
// [Client.Upload]. FinalURL is the permanent, publicly addressable URL for
// the asset, suitable for referencing in email content.
type CompleteUploadResponse struct {
EmailAssetID string `json:"emailAssetId"`
FinalURL string `json:"finalUrl"`
}
// UploadRequest is the input to [Client.Upload], the one-call helper that
// performs the full three-step upload flow. Body is read up to
// ContentLength bytes and sent as the asset body.
type UploadRequest struct {
ContentType string
ContentLength int64
Body io.Reader
}
// CreateUpload reserves an asset slot on the account and returns a
// presigned S3 URL the caller must PUT the asset bytes to. After a
// successful PUT, call [Client.CompleteUpload] to finalize the asset. Most
// callers should use [Client.Upload] instead, which performs all three
// steps.
func (c *Client) CreateUpload(req CreateUploadRequest) (*CreateUploadResponse, error) {
b, err := json.Marshal(req)
if err != nil {
return nil, fmt.Errorf("failed to encode request: %w", err)
}
httpReq, err := c.newRequest(http.MethodPost, "/uploads", bytes.NewReader(b))
if err != nil {
return nil, err
}
resp, err := c.do(httpReq)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errorFromResponse(resp)
}
var result CreateUploadResponse
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}
return &result, nil
}
// Upload performs the full three-step asset upload: it calls
// [Client.CreateUpload] to obtain a presigned URL, PUTs req.Body to that
// URL using the SDK's HTTP client, and finalizes the asset with
// [Client.CompleteUpload]. The PUT to S3 deliberately does not carry the
// Loops Authorization header. On success the returned FinalURL is the
// public URL of the uploaded asset.
func (c *Client) Upload(req UploadRequest) (*CompleteUploadResponse, error) {
created, err := c.CreateUpload(CreateUploadRequest{
ContentType: req.ContentType,
ContentLength: req.ContentLength,
})
if err != nil {
return nil, err
}
putReq, err := http.NewRequest(http.MethodPut, created.PresignedURL, req.Body)
if err != nil {
return nil, fmt.Errorf("failed to build upload request: %w", err)
}
putReq.Header.Set("Content-Type", req.ContentType)
putReq.ContentLength = req.ContentLength
putResp, err := c.httpClient.Do(putReq)
if err != nil {
return nil, fmt.Errorf("upload to presigned URL failed: %w", err)
}
defer putResp.Body.Close()
if putResp.StatusCode < 200 || putResp.StatusCode >= 300 {
return nil, fmt.Errorf("upload to presigned URL failed: status %d", putResp.StatusCode)
}
return c.CompleteUpload(created.EmailAssetID)
}
// CompleteUpload finalizes an asset previously created with
// [Client.CreateUpload] after its bytes have been PUT to the presigned
// URL. The id argument is the EmailAssetID returned by CreateUpload.
func (c *Client) CompleteUpload(id string) (*CompleteUploadResponse, error) {
req, err := c.newRequest(http.MethodPost, "/uploads/"+id+"/complete", nil)
if err != nil {
return nil, err
}
resp, err := c.do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errorFromResponse(resp)
}
var result CompleteUploadResponse
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}
return &result, nil
}