Skip to content

Commit

Permalink
add etag
Browse files Browse the repository at this point in the history
  • Loading branch information
orisano committed Mar 7, 2024
1 parent 6e72d2c commit a2f9738
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 13 deletions.
10 changes: 9 additions & 1 deletion server/internal/adapter/gql/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions server/internal/adapter/gql/gqlmodel/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/internal/adapter/gql/resolver_mutation_asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func (r *mutationResolver) CreateAssetUpload(ctx context.Context, input gqlmodel
Filename: lo.FromPtr(input.Filename),
ContentLength: int64(lo.FromPtr(input.ContentLength)),
Cursor: lo.FromPtr(input.Cursor),
ETags: input.Etags,

Check warning on line 105 in server/internal/adapter/gql/resolver_mutation_asset.go

View check run for this annotation

Codecov / codecov/patch

server/internal/adapter/gql/resolver_mutation_asset.go#L100-L105

Added lines #L100 - L105 were not covered by tests
}, getOperator(ctx))
if err != nil {
return nil, err
Expand Down
15 changes: 12 additions & 3 deletions server/internal/infrastructure/aws/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/google/uuid"
"github.com/reearth/reearth-cms/server/internal/usecase/gateway"
"github.com/reearth/reearth-cms/server/pkg/asset"
Expand Down Expand Up @@ -151,10 +152,18 @@ func (f *fileRepo) IssueUploadAssetLink(ctx context.Context, param gateway.Issue
}
uploaded := maxPartSize * (cursor.Part - 1)
if completed := param.ContentLength <= uploaded; completed {
var mu types.CompletedMultipartUpload
for i, etag := range param.ETags {
mu.Parts = append(mu.Parts, types.CompletedPart{
ETag: aws.String(etag),
PartNumber: aws.Int32(int32(i + 1)),
})
}
if _, err := f.s3Client.CompleteMultipartUpload(ctx, &s3.CompleteMultipartUploadInput{
Bucket: aws.String(f.bucketName),
Key: aws.String(p),
UploadId: aws.String(cursor.UploadID),
Bucket: aws.String(f.bucketName),
Key: aws.String(p),
UploadId: aws.String(cursor.UploadID),
MultipartUpload: &mu,
}); err != nil {
return nil, fmt.Errorf("complete multipart uplaod: %w", err)
}
Expand Down
1 change: 1 addition & 0 deletions server/internal/usecase/gateway/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type IssueUploadAssetParam struct {
ExpiresAt time.Time

Cursor string
ETags []string
}

func (p IssueUploadAssetParam) ContentType() string {
Expand Down
1 change: 1 addition & 0 deletions server/internal/usecase/interactor/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ func (i *Asset) CreateUpload(ctx context.Context, inp interfaces.CreateAssetUplo
ContentLength: au.ContentLength(),
ExpiresAt: au.ExpiresAt(),
Cursor: wrapped.Cursor,
ETags: inp.ETags,
}

Check warning on line 307 in server/internal/usecase/interactor/asset.go

View check run for this annotation

Codecov / codecov/patch

server/internal/usecase/interactor/asset.go#L279-L307

Added lines #L279 - L307 were not covered by tests
}

Expand Down
1 change: 1 addition & 0 deletions server/internal/usecase/interfaces/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type CreateAssetUploadParam struct {
ContentLength int64

Cursor string
ETags []string
}

var (
Expand Down
1 change: 1 addition & 0 deletions server/schemas/asset.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ input CreateAssetUploadInput {

# Required if uploading in multiple parts.
cursor: String
etags: [String!]
}

input UpdateAssetInput {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export default (isItemsRequired: boolean) => {
let cursor = "";
let offset = 0;
let uploadToken = "";
const etags: string[] = [];
// eslint-disable-next-line no-constant-condition
while (true) {
const createAssetUploadResult = await createAssetUploadMutation({
Expand All @@ -125,6 +126,7 @@ export default (isItemsRequired: boolean) => {
filename: file.name,
contentLength: file.size ?? 0,
cursor,
etags,

Check warning on line 129 in web/src/components/organisms/Project/Asset/AssetList/hooks.ts

View check run for this annotation

Codecov / codecov/patch

web/src/components/organisms/Project/Asset/AssetList/hooks.ts#L117-L129

Added lines #L117 - L129 were not covered by tests
},
});
if (
Expand All @@ -142,14 +144,16 @@ export default (isItemsRequired: boolean) => {
break;
}
const headers = contentType ? { "content-type": contentType } : undefined;
await fetch(url, {
const res = await fetch(url, {
method: "PUT",
body: (file as any).slice(offset, offset + contentLength),
headers,
});
if (!next) {
break;
}
const etag = res.headers.get("ETag");
if (etag) etags.push(etag);
cursor = next;
offset += contentLength;

Check warning on line 158 in web/src/components/organisms/Project/Asset/AssetList/hooks.ts

View check run for this annotation

Codecov / codecov/patch

web/src/components/organisms/Project/Asset/AssetList/hooks.ts#L132-L158

Added lines #L132 - L158 were not covered by tests
}
Expand Down
7 changes: 5 additions & 2 deletions web/src/gql/graphql-client-api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ export type CreateAssetPayload = {
export type CreateAssetUploadInput = {
contentLength?: InputMaybe<Scalars['Int']>;
cursor?: InputMaybe<Scalars['String']>;
etags?: InputMaybe<Array<Scalars['String']>>;
filename?: InputMaybe<Scalars['String']>;
projectId: Scalars['ID'];
};
Expand Down Expand Up @@ -2228,6 +2229,7 @@ export type CreateAssetUploadMutationVariables = Exact<{
filename: Scalars['String'];
cursor: Scalars['String'];
contentLength: Scalars['Int'];
etags?: InputMaybe<Array<Scalars['String']> | Scalars['String']>;
}>;


Expand Down Expand Up @@ -3489,9 +3491,9 @@ export type DecompressAssetMutationHookResult = ReturnType<typeof useDecompressA
export type DecompressAssetMutationResult = Apollo.MutationResult<DecompressAssetMutation>;
export type DecompressAssetMutationOptions = Apollo.BaseMutationOptions<DecompressAssetMutation, DecompressAssetMutationVariables>;
export const CreateAssetUploadDocument = gql`
mutation CreateAssetUpload($projectId: ID!, $filename: String!, $cursor: String!, $contentLength: Int!) {
mutation CreateAssetUpload($projectId: ID!, $filename: String!, $cursor: String!, $contentLength: Int!, $etags: [String!]) {
createAssetUpload(
input: {projectId: $projectId, filename: $filename, cursor: $cursor, contentLength: $contentLength}
input: {projectId: $projectId, filename: $filename, cursor: $cursor, contentLength: $contentLength, etags: $etags}
) {
url
token
Expand Down Expand Up @@ -3520,6 +3522,7 @@ export type CreateAssetUploadMutationFn = Apollo.MutationFunction<CreateAssetUpl
* filename: // value for 'filename'
* cursor: // value for 'cursor'
* contentLength: // value for 'contentLength'
* etags: // value for 'etags'
* },
* });
*/
Expand Down
20 changes: 20 additions & 0 deletions web/src/gql/graphql.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2061,6 +2061,26 @@
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "etags",
"description": null,
"type": {
"kind": "LIST",
"name": null,
"ofType": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
}
},
"defaultValue": null,
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "filename",
"description": null,
Expand Down
18 changes: 16 additions & 2 deletions web/src/gql/queries/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,22 @@ export const DECOMPRESS_ASSET = gql`
`;

export const CREATE_ASSET_UPLOAD = gql`
mutation CreateAssetUpload($projectId: ID!, $filename: String!, $cursor: String!, $contentLength: Int!) {
createAssetUpload(input: { projectId: $projectId, filename: $filename, cursor: $cursor, contentLength: $contentLength }) {
mutation CreateAssetUpload(
$projectId: ID!
$filename: String!
$cursor: String!
$contentLength: Int!
$etags: [String!]
) {
createAssetUpload(
input: {
projectId: $projectId
filename: $filename
cursor: $cursor
contentLength: $contentLength
etags: $etags
}
) {

Check warning on line 144 in web/src/gql/queries/assets.ts

View check run for this annotation

Codecov / codecov/patch

web/src/gql/queries/assets.ts#L129-L144

Added lines #L129 - L144 were not covered by tests
url
token
contentType
Expand Down

0 comments on commit a2f9738

Please sign in to comment.