Skip to content

Commit

Permalink
feat: add support presignUpload fetch upload url.
Browse files Browse the repository at this point in the history
  • Loading branch information
xufeixiang committed Sep 3, 2024
1 parent ad32de4 commit 35717c6
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 4 deletions.
50 changes: 50 additions & 0 deletions app/docs/docs.go

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

50 changes: 50 additions & 0 deletions app/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1375,6 +1375,50 @@
}
}
},
"/storageClient/{dataName}/presignUpload": {
"get": {
"description": "\"获取上传地址\"",
"tags": [
"storage"
],
"parameters": [
{
"type": "string",
"description": "dataName",
"name": "dataName",
"in": "path",
"required": true
},
{
"type": "string",
"description": "dirname",
"name": "dirname",
"in": "query"
},
{
"type": "string",
"description": "filename",
"name": "filename",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "string"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/i18n.CustomError"
}
}
}
}
},
"/storageClient/{dataName}/remove": {
"post": {
"description": "\"移除\"",
Expand Down Expand Up @@ -2238,6 +2282,12 @@
"icon": {
"type": "string"
},
"keywords": {
"type": "array",
"items": {
"type": "string"
}
},
"language": {
"type": "string"
},
Expand Down
33 changes: 33 additions & 0 deletions app/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ definitions:
type: string
icon:
type: string
keywords:
items:
type: string
type: array
language:
type: string
name:
Expand Down Expand Up @@ -1339,6 +1343,35 @@ paths:
$ref: '#/definitions/i18n.CustomError'
tags:
- storage
/storageClient/{dataName}/presignUpload:
get:
description: '"获取上传地址"'
parameters:
- description: dataName
in: path
name: dataName
required: true
type: string
- description: dirname
in: query
name: dirname
type: string
- description: filename
in: query
name: filename
required: true
type: string
responses:
"200":
description: OK
schema:
type: string
"400":
description: Bad Request
schema:
$ref: '#/definitions/i18n.CustomError'
tags:
- storage
/storageClient/{dataName}/remove:
post:
description: '"移除"'
Expand Down
33 changes: 29 additions & 4 deletions pkg/api/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func StorageExtraRouter(rootRouter, storageRouter *echo.Group, baseHandler *base
clientRouter.POST("/ping", handler.ping)
clientRouter.POST(base.DataNamePath+"/mkdir", handler.mkdir)
clientRouter.POST(base.DataNamePath+"/upload", handler.upload)
clientRouter.GET(base.DataNamePath+"/presignUpload", handler.presignUpload)
clientRouter.POST(base.DataNamePath+"/remove", handler.remove)
clientRouter.POST(base.DataNamePath+"/rename", handler.rename)
clientRouter.GET(base.DataNamePath+"/list", handler.list)
Expand Down Expand Up @@ -85,7 +86,7 @@ func (s *storage) mkdir(c echo.Context) (err error) {
// @Param dataName path string true "dataName"
// @Param dirname query string true "dirname"
// @Param file formData file true "file"
// @Success 200 "OK"
// @Success 200 "OK"
// @Failure 400 {object} i18n.CustomError
// @Router /storageClient/{dataName}/upload [post]
func (s *storage) upload(c echo.Context) (err error) {
Expand All @@ -108,11 +109,35 @@ func (s *storage) upload(c echo.Context) (err error) {
return c.NoContent(http.StatusOK)
}

// @Tags storage
// @Description "获取上传地址"
// @Param dataName path string true "dataName"
// @Param dirname query string false "dirname"
// @Param filename query string true "filename"
// @Success 200 {string} string "OK"
// @Failure 400 {object} i18n.CustomError
// @Router /storageClient/{dataName}/presignUpload [get]
func (s *storage) presignUpload(c echo.Context) (err error) {
data, filename, err := s.getStorageAndQueryParam(c, consts.QueryParamFilename, true)
if err != nil {
return
}

dirname := c.QueryParam(consts.QueryParamDirname)
signedUrl, err := s.clientCache.PresignPutObject(c.Request().Context(), data, dirname, filename)
if err != nil {
err = i18n.NewCustomErrorWithMode(s.modelName, err, i18n.StorageTouchError)
return
}

return c.String(http.StatusOK, signedUrl)
}

// @Tags storage
// @Description "移除"
// @Param dataName path string true "dataName"
// @Param filename query string true "filename"
// @Success 200 "OK"
// @Success 200 "OK"
// @Failure 400 {object} i18n.CustomError
// @Router /storageClient/{dataName}/remove [post]
func (s *storage) remove(c echo.Context) (err error) {
Expand All @@ -134,7 +159,7 @@ func (s *storage) remove(c echo.Context) (err error) {
// @Description "重命名"
// @Param dataName path string true "dataName"
// @Param data body fileloader.DataMutation true "DataMutation"
// @Success 200 "OK"
// @Success 200 "OK"
// @Failure 400 {object} i18n.CustomError
// @Router /storageClient/{dataName}/rename [post]
func (s *storage) rename(c echo.Context) (err error) {
Expand Down Expand Up @@ -194,7 +219,7 @@ func (s *storage) detail(c echo.Context) (err error) {

file, err := s.clientCache.StatObject(c.Request().Context(), data, filename)
if err != nil {
err = i18n.NewCustomErrorWithMode(s.modelName, err, i18n.StorageListError)
err = i18n.NewCustomErrorWithMode(s.modelName, err, i18n.StorageDetailError)
return
}

Expand Down
20 changes: 20 additions & 0 deletions pkg/common/models/storage_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,26 @@ func (s *StorageClientCache) StatObject(ctx context.Context, storage *Storage, f
return
}

func (s *StorageClientCache) PresignPutObject(ctx context.Context, storage *Storage, dirname, filename string) (signedUrl string, err error) {
client, err := s.buildClient(storage)
if err != nil {
return
}

if dirname != "" {
filename = utils.NormalizePath(dirname, filename)
}
filename = strings.TrimPrefix(filename, "/")
bucketName := utils.GetVariableString(storage.BucketName)
URL, err := client.PresignedPutObject(ctx, bucketName, filename, signedUrlExpires)
if err != nil {
return
}

signedUrl = URL.String()
return
}

func (s *StorageClientCache) PresignGetObject(ctx context.Context, storage *Storage, filename string) (signedUrl string, err error) {
client, err := s.buildClient(storage)
if err != nil {
Expand Down

0 comments on commit 35717c6

Please sign in to comment.