Skip to content

Commit

Permalink
feat(server): asset gzip upload
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Jan 20, 2025
1 parent e04cebf commit 544406b
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 79 deletions.
2 changes: 2 additions & 0 deletions server/internal/adapter/integration/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func (s *Server) AssetCreate(ctx context.Context, request AssetCreateRequestObje
Size: inp.File.FileSize(),
// ContentType: inp.File.ContentType(),
ContentType: "",
Gzip: lo.FromPtr(inp.Gzip),
}
skipDecompression = lo.FromPtrOr(inp.SkipDecompression, false)
}
Expand Down Expand Up @@ -173,6 +174,7 @@ func (s *Server) AssetUploadCreate(ctx context.Context, request AssetUploadCreat
ProjectID: request.ProjectId,
Filename: lo.FromPtr(request.Body.Name),
ContentLength: int64(lo.FromPtr(request.Body.ContentLength)),
Gzip: lo.FromPtr(request.Body.Gzip),
Cursor: lo.FromPtr(request.Body.Cursor),
}, op)

Expand Down
147 changes: 74 additions & 73 deletions server/internal/adapter/integration/server.gen.go

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

26 changes: 21 additions & 5 deletions server/internal/infrastructure/gcp/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (f *fileRepo) UploadAsset(ctx context.Context, file *file.File) (string, in
return "", 0, gateway.ErrInvalidFile
}

size, err := f.upload(ctx, p, file.Content)
size, err := f.upload(ctx, p, file.ContentType, file.Gzip, file.Content)
if err != nil {
return "", 0, err
}
Expand Down Expand Up @@ -154,6 +154,9 @@ func (f *fileRepo) IssueUploadAssetLink(ctx context.Context, param gateway.Issue
Expires: param.ExpiresAt,
ContentType: contentType,
}
if param.Gzip {
opt.Headers = []string{"Content-Encoding: gzip"}
}
uploadURL, err := bucket.SignedURL(p, opt)
if err != nil {
log.Warnf("gcs: failed to issue signed url: %v", err)
Expand All @@ -164,6 +167,7 @@ func (f *fileRepo) IssueUploadAssetLink(ctx context.Context, param gateway.Issue
ContentType: contentType,
ContentLength: param.ContentLength,
Next: "",
Gzip: param.Gzip,
}, nil
}

Expand Down Expand Up @@ -208,11 +212,15 @@ func (f *fileRepo) read(ctx context.Context, filename string) (io.ReadCloser, er
return reader, nil
}

func (f *fileRepo) upload(ctx context.Context, filename string, content io.Reader) (int64, error) {
func (f *fileRepo) upload(ctx context.Context, filename, contentType string, gzip bool, content io.Reader) (int64, error) {
if filename == "" {
return 0, gateway.ErrInvalidFile
}

if gzip {
filename = strings.TrimSuffix(filename, ".gz")
}

bucket, err := f.bucket(ctx)
if err != nil {
log.Errorf("gcs: upload bucket err: %+v\n", err)
Expand All @@ -227,9 +235,17 @@ func (f *fileRepo) upload(ctx context.Context, filename string, content io.Reade

writer := object.NewWriter(ctx)
writer.ObjectAttrs.CacheControl = f.cacheControl
ct := getContentType(filename)
if ct != "" {
writer.ObjectAttrs.ContentType = ct
if contentType == "" {
contentType = getContentType(filename)
}
if contentType != "" {
writer.ObjectAttrs.ContentType = contentType
}
if gzip {
writer.ObjectAttrs.ContentEncoding = "gzip"
if writer.ObjectAttrs.ContentType == "" || writer.ObjectAttrs.ContentType == "application/gzip" {
writer.ObjectAttrs.ContentType = "application/octet-stream"
}
}

if _, err := io.Copy(writer, content); err != nil {
Expand Down
2 changes: 2 additions & 0 deletions server/internal/usecase/gateway/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ type UploadAssetLink struct {
ContentType string
ContentLength int64
Next string
Gzip bool
}

type IssueUploadAssetParam struct {
UUID string
Filename string
ContentLength int64
ExpiresAt time.Time
Gzip bool

Cursor string
}
Expand Down
11 changes: 11 additions & 0 deletions server/internal/usecase/interactor/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ func (i *Asset) Create(ctx context.Context, inp interfaces.CreateAssetParam, op
return nil, nil, interfaces.ErrFileNotIncluded
}

if inp.File.Gzip {
inp.File.Name = strings.TrimSuffix(inp.File.Name, ".gz")
}

prj, err := i.repos.Project.FindByID(ctx, inp.ProjectID)
if err != nil {
return nil, nil, err
Expand Down Expand Up @@ -304,6 +308,10 @@ func (i *Asset) CreateUpload(ctx context.Context, inp interfaces.CreateAssetUplo
return nil, interfaces.ErrInvalidOperator
}

if inp.Gzip {
inp.Filename = strings.TrimSuffix(inp.Filename, ".gz")
}

var param *gateway.IssueUploadAssetParam
if inp.Cursor == "" {
if inp.Filename == "" {
Expand All @@ -319,6 +327,7 @@ func (i *Asset) CreateUpload(ctx context.Context, inp interfaces.CreateAssetUplo
ContentLength: inp.ContentLength,
ExpiresAt: expiresAt,
Cursor: "",
Gzip: inp.Gzip,
}
} else {
wrapped, err := parseWrappedUploadCursor(inp.Cursor)
Expand All @@ -338,6 +347,7 @@ func (i *Asset) CreateUpload(ctx context.Context, inp interfaces.CreateAssetUplo
ContentLength: au.ContentLength(),
ExpiresAt: au.ExpiresAt(),
Cursor: wrapped.Cursor,
Gzip: inp.Gzip,
}
}

Expand Down Expand Up @@ -375,6 +385,7 @@ func (i *Asset) CreateUpload(ctx context.Context, inp interfaces.CreateAssetUplo
ContentType: uploadLink.ContentType,
ContentLength: uploadLink.ContentLength,
Next: wrapUploadCursor(param.UUID, uploadLink.Next),
Gzip: inp.Gzip,
}, nil
}

Expand Down
2 changes: 2 additions & 0 deletions server/internal/usecase/interfaces/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type CreateAssetUploadParam struct {

Filename string
ContentLength int64
Gzip bool

Cursor string
}
Expand All @@ -54,6 +55,7 @@ type AssetUpload struct {
ContentType string
ContentLength int64
Next string
Gzip bool
}

type Asset interface {
Expand Down
1 change: 1 addition & 0 deletions server/pkg/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type File struct {
Name string
Size int64
ContentType string
Gzip bool
}

func FromMultipart(multipartReader *multipart.Reader, formName string) (*File, error) {
Expand Down
3 changes: 3 additions & 0 deletions server/pkg/integrationapi/types.gen.go

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

12 changes: 11 additions & 1 deletion server/schemas/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1403,6 +1403,9 @@ paths:
skipDecompression:
type: boolean
default: false
gzip:
type: boolean
default: false
application/json:
schema:
type: object
Expand All @@ -1415,6 +1418,10 @@ paths:
type: boolean
nullable: true
default: false
gzip:
type: boolean
nullable: true
default: false
responses:
'200':
description: assets
Expand All @@ -1436,7 +1443,7 @@ paths:
security:
- bearerAuth: []
summary: Upload an asset.
description: Issue a URL and a token to upload an asset.
description: Issue an URL and a token to upload an asset.
parameters:
- $ref: '#/components/parameters/projectIdParam'
requestBody:
Expand All @@ -1451,6 +1458,9 @@ paths:
type: integer
cursor:
type: string
gzip:
type: boolean
default: false
responses:
'200':
description: asset upload
Expand Down

0 comments on commit 544406b

Please sign in to comment.