@@ -53,13 +53,13 @@ func (l *LocalFilePersister) Persist(_ context.Context, path string, data io.Rea
53
53
}
54
54
55
55
// RemoteFilePersister is to be used when files created by the browser module need
56
- // to be uploaded to a remote location. This uses a preSignedURLGetterURL to
57
- // retrieve one pre-signed URL. The pre-signed url is used to upload the file
56
+ // to be uploaded to a remote location. This uses a presignedURLRequestURL to
57
+ // retrieve one presigned URL. The presigned url is used to upload the file
58
58
// to the remote location.
59
59
type RemoteFilePersister struct {
60
- preSignedURLGetterURL string
61
- headers map [string ]string
62
- basePath string
60
+ presignedURLRequestURL string
61
+ headers map [string ]string
62
+ basePath string
63
63
64
64
httpClient * http.Client
65
65
}
@@ -69,22 +69,22 @@ type PresignedURLResponse struct {
69
69
Service string `json:"service"`
70
70
URLs []struct {
71
71
Name string `json:"name"`
72
- PreSignedURL string `json:"pre_signed_url"` //nolint:tagliatelle
72
+ PresignedURL string `json:"pre_signed_url"` //nolint:tagliatelle
73
73
Method string `json:"method"`
74
74
FormFields map [string ]string `json:"form_fields"` //nolint:tagliatelle
75
75
} `json:"urls"`
76
76
}
77
77
78
78
// NewRemoteFilePersister creates a new instance of RemoteFilePersister.
79
79
func NewRemoteFilePersister (
80
- preSignedURLGetterURL string ,
80
+ presignedURLRequestURL string ,
81
81
headers map [string ]string ,
82
82
basePath string ,
83
83
) * RemoteFilePersister {
84
84
return & RemoteFilePersister {
85
- preSignedURLGetterURL : preSignedURLGetterURL ,
86
- headers : headers ,
87
- basePath : basePath ,
85
+ presignedURLRequestURL : presignedURLRequestURL ,
86
+ headers : headers ,
87
+ basePath : basePath ,
88
88
httpClient : & http.Client {
89
89
Timeout : time .Second * 10 ,
90
90
},
@@ -93,12 +93,12 @@ func NewRemoteFilePersister(
93
93
94
94
// Persist will upload the contents of data to a remote location.
95
95
func (r * RemoteFilePersister ) Persist (ctx context.Context , path string , data io.Reader ) (err error ) {
96
- psResp , err := r .getPreSignedURL (ctx , path )
96
+ psResp , err := r .requestPresignedURL (ctx , path )
97
97
if err != nil {
98
- return fmt .Errorf ("getting presigned url: %w" , err )
98
+ return fmt .Errorf ("requesting presigned url: %w" , err )
99
99
}
100
100
101
- req , err := newUploadRequest (ctx , psResp , data )
101
+ req , err := newFileUploadRequest (ctx , psResp , data )
102
102
if err != nil {
103
103
return fmt .Errorf ("creating upload request: %w" , err )
104
104
}
@@ -120,23 +120,20 @@ func (r *RemoteFilePersister) Persist(ctx context.Context, path string, data io.
120
120
return nil
121
121
}
122
122
123
- func checkStatusCode (resp * http.Response ) error {
124
- if resp .StatusCode < http .StatusOK || resp .StatusCode >= http .StatusMultipleChoices {
125
- return fmt .Errorf ("server returned %d (%s)" , resp .StatusCode , strings .ToLower (http .StatusText (resp .StatusCode )))
126
- }
127
-
128
- return nil
129
- }
130
-
131
- // getPreSignedURL will request a new presigned URL from the remote server for the given path.
132
- // Returns a [PresignedURLResponse] that contains the presigned URL details.
133
- func (r * RemoteFilePersister ) getPreSignedURL (ctx context.Context , path string ) (PresignedURLResponse , error ) {
123
+ // requestPresignedURL will request a new presigned URL from the remote server
124
+ // and returns a [PresignedURLResponse] that contains the presigned URL details.
125
+ func (r * RemoteFilePersister ) requestPresignedURL (ctx context.Context , path string ) (PresignedURLResponse , error ) {
134
126
b , err := buildPresignedRequestBody (r .basePath , path )
135
127
if err != nil {
136
128
return PresignedURLResponse {}, fmt .Errorf ("building request body: %w" , err )
137
129
}
138
130
139
- req , err := http .NewRequestWithContext (ctx , http .MethodPost , r .preSignedURLGetterURL , bytes .NewReader (b ))
131
+ req , err := http .NewRequestWithContext (
132
+ ctx ,
133
+ http .MethodPost ,
134
+ r .presignedURLRequestURL ,
135
+ bytes .NewReader (b ),
136
+ )
140
137
if err != nil {
141
138
return PresignedURLResponse {}, fmt .Errorf ("creating request: %w" , err )
142
139
}
@@ -155,7 +152,7 @@ func (r *RemoteFilePersister) getPreSignedURL(ctx context.Context, path string)
155
152
return PresignedURLResponse {}, err
156
153
}
157
154
158
- return readResponseBody (resp )
155
+ return readPresignedURLResponse (resp )
159
156
}
160
157
161
158
func buildPresignedRequestBody (basePath , path string ) ([]byte , error ) {
@@ -185,7 +182,7 @@ func buildPresignedRequestBody(basePath, path string) ([]byte, error) {
185
182
return bb , nil
186
183
}
187
184
188
- func readResponseBody (resp * http.Response ) (PresignedURLResponse , error ) {
185
+ func readPresignedURLResponse (resp * http.Response ) (PresignedURLResponse , error ) {
189
186
var rb PresignedURLResponse
190
187
191
188
decoder := json .NewDecoder (resp .Body )
@@ -201,9 +198,9 @@ func readResponseBody(resp *http.Response) (PresignedURLResponse, error) {
201
198
return rb , nil
202
199
}
203
200
204
- // newUploadRequest creates a new HTTP request to upload a file as a multipart
201
+ // newFileUploadRequest creates a new HTTP request to upload a file as a multipart
205
202
// form to the presigned URL received from the server.
206
- func newUploadRequest (
203
+ func newFileUploadRequest (
207
204
ctx context.Context ,
208
205
resp PresignedURLResponse ,
209
206
data io.Reader ,
@@ -235,7 +232,7 @@ func newUploadRequest(
235
232
req , err := http .NewRequestWithContext (
236
233
ctx ,
237
234
psu .Method ,
238
- psu .PreSignedURL ,
235
+ psu .PresignedURL ,
239
236
& form ,
240
237
)
241
238
if err != nil {
@@ -245,3 +242,11 @@ func newUploadRequest(
245
242
246
243
return req , nil
247
244
}
245
+
246
+ func checkStatusCode (resp * http.Response ) error {
247
+ if resp .StatusCode < http .StatusOK || resp .StatusCode >= http .StatusMultipleChoices {
248
+ return fmt .Errorf ("server returned %d (%s)" , resp .StatusCode , strings .ToLower (http .StatusText (resp .StatusCode )))
249
+ }
250
+
251
+ return nil
252
+ }
0 commit comments