Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit 63a303b

Browse files
committed
Improve file persister for consistency
Some small changes to make it more readable (subjectively!) - Move checkStatus utility to bottom to reduce noise while reading - pre-signed to presigned and preSigned to presigned for consistency - getPresignedURL to requestPresignedURL for saying it's a request - newUploadRequest to newFileUploadRequest for specificity - readResponseBody generic naming to specific readPresignedURLResponse - preSignedURLGetterURL to presignedURLRequestURL for consistency
1 parent fcb6d5c commit 63a303b

File tree

2 files changed

+37
-32
lines changed

2 files changed

+37
-32
lines changed

storage/file_persister.go

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ func (l *LocalFilePersister) Persist(_ context.Context, path string, data io.Rea
5353
}
5454

5555
// 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
5858
// to the remote location.
5959
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
6363

6464
httpClient *http.Client
6565
}
@@ -69,22 +69,22 @@ type PresignedURLResponse struct {
6969
Service string `json:"service"`
7070
URLs []struct {
7171
Name string `json:"name"`
72-
PreSignedURL string `json:"pre_signed_url"` //nolint:tagliatelle
72+
PresignedURL string `json:"pre_signed_url"` //nolint:tagliatelle
7373
Method string `json:"method"`
7474
FormFields map[string]string `json:"form_fields"` //nolint:tagliatelle
7575
} `json:"urls"`
7676
}
7777

7878
// NewRemoteFilePersister creates a new instance of RemoteFilePersister.
7979
func NewRemoteFilePersister(
80-
preSignedURLGetterURL string,
80+
presignedURLRequestURL string,
8181
headers map[string]string,
8282
basePath string,
8383
) *RemoteFilePersister {
8484
return &RemoteFilePersister{
85-
preSignedURLGetterURL: preSignedURLGetterURL,
86-
headers: headers,
87-
basePath: basePath,
85+
presignedURLRequestURL: presignedURLRequestURL,
86+
headers: headers,
87+
basePath: basePath,
8888
httpClient: &http.Client{
8989
Timeout: time.Second * 10,
9090
},
@@ -93,12 +93,12 @@ func NewRemoteFilePersister(
9393

9494
// Persist will upload the contents of data to a remote location.
9595
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)
9797
if err != nil {
98-
return fmt.Errorf("getting presigned url: %w", err)
98+
return fmt.Errorf("requesting presigned url: %w", err)
9999
}
100100

101-
req, err := newUploadRequest(ctx, psResp, data)
101+
req, err := newFileUploadRequest(ctx, psResp, data)
102102
if err != nil {
103103
return fmt.Errorf("creating upload request: %w", err)
104104
}
@@ -120,23 +120,20 @@ func (r *RemoteFilePersister) Persist(ctx context.Context, path string, data io.
120120
return nil
121121
}
122122

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) {
134126
b, err := buildPresignedRequestBody(r.basePath, path)
135127
if err != nil {
136128
return PresignedURLResponse{}, fmt.Errorf("building request body: %w", err)
137129
}
138130

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+
)
140137
if err != nil {
141138
return PresignedURLResponse{}, fmt.Errorf("creating request: %w", err)
142139
}
@@ -155,7 +152,7 @@ func (r *RemoteFilePersister) getPreSignedURL(ctx context.Context, path string)
155152
return PresignedURLResponse{}, err
156153
}
157154

158-
return readResponseBody(resp)
155+
return readPresignedURLResponse(resp)
159156
}
160157

161158
func buildPresignedRequestBody(basePath, path string) ([]byte, error) {
@@ -185,7 +182,7 @@ func buildPresignedRequestBody(basePath, path string) ([]byte, error) {
185182
return bb, nil
186183
}
187184

188-
func readResponseBody(resp *http.Response) (PresignedURLResponse, error) {
185+
func readPresignedURLResponse(resp *http.Response) (PresignedURLResponse, error) {
189186
var rb PresignedURLResponse
190187

191188
decoder := json.NewDecoder(resp.Body)
@@ -201,9 +198,9 @@ func readResponseBody(resp *http.Response) (PresignedURLResponse, error) {
201198
return rb, nil
202199
}
203200

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
205202
// form to the presigned URL received from the server.
206-
func newUploadRequest(
203+
func newFileUploadRequest(
207204
ctx context.Context,
208205
resp PresignedURLResponse,
209206
data io.Reader,
@@ -235,7 +232,7 @@ func newUploadRequest(
235232
req, err := http.NewRequestWithContext(
236233
ctx,
237234
psu.Method,
238-
psu.PreSignedURL,
235+
psu.PresignedURL,
239236
&form,
240237
)
241238
if err != nil {
@@ -245,3 +242,11 @@ func newUploadRequest(
245242

246243
return req, nil
247244
}
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+
}

storage/file_persister_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func TestRemoteFilePersister(t *testing.T) {
158158
},
159159
wantPresignedURLMethod: http.MethodPost,
160160
getPresignedURLResponse: http.StatusTooManyRequests,
161-
wantError: "getting presigned url: server returned 429 (too many requests)",
161+
wantError: "requesting presigned url: server returned 429 (too many requests)",
162162
},
163163
{
164164
name: "get_presigned_fails",
@@ -175,7 +175,7 @@ func TestRemoteFilePersister(t *testing.T) {
175175
},
176176
wantPresignedURLMethod: http.MethodPost,
177177
getPresignedURLResponse: http.StatusInternalServerError,
178-
wantError: "getting presigned url: server returned 500 (internal server error)",
178+
wantError: "requesting presigned url: server returned 500 (internal server error)",
179179
},
180180
{
181181
name: "upload_rate_limited",

0 commit comments

Comments
 (0)