-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from rarimo/feat/s3-integrations
Add endpoint for getting url for put image in s3 storage
- Loading branch information
Showing
23 changed files
with
358 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,7 +66,8 @@ allOf: | |
type: string | ||
example: "+13165282105" | ||
email: | ||
type: [email protected] | ||
type: string | ||
example: [email protected] | ||
image: | ||
type: string | ||
description: | | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
allOf: | ||
- $ref: '#/components/schemas/UploadImageKey' | ||
- type: object | ||
x-go-is-request: true | ||
required: | ||
- attributes | ||
properties: | ||
attributes: | ||
type: object | ||
required: | ||
- content_type | ||
- content_length | ||
properties: | ||
content_type: | ||
type: string | ||
example: image/png | ||
description: Allowed content-type is `image/png` or `image/jpeg` | ||
content_length: | ||
type: integer | ||
format: int64 | ||
example: 150000 | ||
description: Image size. It cannot be more than 4 megabytes. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
type: object | ||
required: | ||
- type | ||
properties: | ||
type: | ||
type: string | ||
enum: [ upload_image ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
allOf: | ||
- $ref: '#/components/schemas/UploadImageResponseKey' | ||
- type: object | ||
required: | ||
- attributes | ||
properties: | ||
attributes: | ||
type: object | ||
required: | ||
- url | ||
properties: | ||
url: | ||
type: string | ||
description: Pre-signed URL to upload the file | ||
example: https://bucket.nyc3.digitaloceanspaces.com/somefile?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=DO00PTJRCBZELX6E4EEK%2F20240722%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20240722T133921Z&X-Amz-Expires=300&X-Amz-SignedHeaders=host&X-Amz-Signature=940c9058b90e8836b03470fdb51af1f24baabc16a7a83b80352d3d618aa4f23f |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
type: object | ||
required: | ||
- id | ||
- type | ||
properties: | ||
id: | ||
type: string | ||
description: UUID to check form submission status | ||
type: | ||
type: string | ||
enum: [ upload_image_response ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
post: | ||
tags: | ||
- User form | ||
summary: Generate pre-signed url | ||
description: | | ||
Generate pre-signed URL for the provided content-length | ||
and content-type, with a configurable lifetime. | ||
The response contains a URL with a signature and | ||
other information that should be used to upload image | ||
in S3 Storage. The name is generated on the server side. | ||
'verified: true' must be specified in the JWT. | ||
The cooldown of this endpoint is the same as in the submit form. | ||
operationId: uploadImage | ||
security: | ||
- BearerAuth: [] | ||
requestBody: | ||
content: | ||
application/vnd.api+json: | ||
schema: | ||
type: object | ||
required: | ||
- data | ||
properties: | ||
data: | ||
$ref: '#/components/schemas/UploadImage' | ||
responses: | ||
200: | ||
description: "Success" | ||
content: | ||
application/vnd.api+json: | ||
schema: | ||
$ref: '#/components/schemas/UploadImageResponse' | ||
400: | ||
$ref: '#/components/responses/invalidParameter' | ||
401: | ||
$ref: '#/components/responses/invalidAuth' | ||
429: | ||
description: "It is necessary to wait some time before sending the next form" | ||
content: | ||
application/vnd.api+json: | ||
schema: | ||
$ref: '#/components/schemas/Errors' | ||
500: | ||
$ref: '#/components/responses/internalError' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package handlers | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
"time" | ||
|
||
"github.com/rarimo/geo-auth-svc/pkg/auth" | ||
"github.com/rarimo/geo-forms-svc/internal/service/requests" | ||
"github.com/rarimo/geo-forms-svc/resources" | ||
"gitlab.com/distributed_lab/ape" | ||
"gitlab.com/distributed_lab/ape/problems" | ||
) | ||
|
||
func UploadImage(w http.ResponseWriter, r *http.Request) { | ||
req, err := requests.NewUploadImage(r) | ||
if err != nil { | ||
ape.RenderErr(w, problems.BadRequest(err)...) | ||
return | ||
} | ||
|
||
nullifier := strings.ToLower(UserClaims(r)[0].Nullifier) | ||
if !auth.Authenticates(UserClaims(r), auth.VerifiedGrant(nullifier)) { | ||
ape.RenderErr(w, problems.Unauthorized()) | ||
return | ||
} | ||
|
||
lastForm, err := FormsQ(r).Last(nullifier) | ||
if err != nil { | ||
Log(r).WithError(err).Errorf("Failed to get last user form for nullifier [%s]", nullifier) | ||
ape.RenderErr(w, problems.InternalError()) | ||
return | ||
} | ||
|
||
if lastForm != nil { | ||
next := lastForm.CreatedAt.Add(Forms(r).Cooldown) | ||
if next.After(time.Now().UTC()) { | ||
Log(r).Debugf("Form submitted time: %s; next available time: %s", lastForm.CreatedAt, next) | ||
ape.RenderErr(w, problems.TooManyRequests()) | ||
return | ||
} | ||
} | ||
|
||
signedURL, key, err := Storage(r).GeneratePutURL(req.Data.Attributes.ContentType, req.Data.Attributes.ContentLength) | ||
if err != nil { | ||
Log(r).WithError(err).Error("Failed to generate pre-signed url") | ||
ape.RenderErr(w, problems.InternalError()) | ||
return | ||
} | ||
|
||
ape.Render(w, resources.UploadImageResponseResponse{ | ||
Data: resources.UploadImageResponse{ | ||
Key: resources.Key{ | ||
ID: key, | ||
Type: resources.UPLOAD_IMAGE_RESPONSE, | ||
}, | ||
Attributes: resources.UploadImageResponseAttributes{ | ||
Url: signedURL, | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package requests | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
validation "github.com/go-ozzo/ozzo-validation/v4" | ||
"github.com/rarimo/geo-forms-svc/resources" | ||
) | ||
|
||
const maxImageSize = 1 << 22 | ||
|
||
func NewUploadImage(r *http.Request) (req resources.UploadImageRequest, err error) { | ||
if err = json.NewDecoder(r.Body).Decode(&req); err != nil { | ||
err = newDecodeError("body", err) | ||
return | ||
} | ||
|
||
errs := validation.Errors{ | ||
"data/type": validation.Validate(req.Data.Type, validation.Required, validation.In(resources.UPLOAD_IMAGE)), | ||
"data/attributes/content_type": validation.Validate(req.Data.Attributes.ContentType, validation.Required, validation.In("image/png", "image/jpeg")), | ||
"data/attributes/content_length": validation.Validate(req.Data.Attributes.ContentLength, validation.Required, validation.Length(1, int(maxImageSize))), | ||
} | ||
|
||
return req, errs.Filter() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.