From 79b91a682e72eb46e5b70611c42b204f3bf2c6f8 Mon Sep 17 00:00:00 2001 From: nateshao Date: Sun, 18 Aug 2024 23:07:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0=E5=89=8D?= =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E4=BF=A1=E6=81=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/core.api | 20 ++++++-- .../handler/file_upload_prepare_handler.go | 28 +++++++++++ core/internal/handler/routes.go | 5 ++ .../logic/file_upload_prepare_logic.go | 47 +++++++++++++++++++ core/internal/types/types.go | 12 +++++ 5 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 core/internal/handler/file_upload_prepare_handler.go create mode 100644 core/internal/logic/file_upload_prepare_logic.go diff --git a/core/core.api b/core/core.api index c334fd8..0f46a40 100644 --- a/core/core.api +++ b/core/core.api @@ -67,10 +67,10 @@ service core-api { // 刷新Authorization @handler RefreshAuthorization post /refresh/authorization (RefreshAuthorizationRequest) returns (RefreshAuthorizationReply) -// -// // 文件上传前基本信息处理 -// @handler FileUploadPrepare -// post /file/upload/prepare(FileUploadPrepareRequest) returns (FileUploadPrepareReply) + + // 文件上传前基本信息处理 + @handler FileUploadPrepare + post /file/upload/prepare (FileUploadPrepareRequest) returns (FileUploadPrepareReply) // // // 文件分片上传 // @handler FileUploadChunk @@ -81,6 +81,18 @@ service core-api { // post /file/upload/chunk/complete(FileUploadChunkCompleteRequest) returns (FileUploadChunkCompleteReply) } +type FileUploadPrepareRequest { + Md5 string `json:"md5"` + Name string `json:"name"` + Ext string `json:"ext"` +} + +type FileUploadPrepareReply { + Identity string `json:"identity"` + UploadId string `json:"upload_id"` + Key string `json:"key"` +} + type RefreshAuthorizationRequest {} type RefreshAuthorizationReply { diff --git a/core/internal/handler/file_upload_prepare_handler.go b/core/internal/handler/file_upload_prepare_handler.go new file mode 100644 index 0000000..d9c30c9 --- /dev/null +++ b/core/internal/handler/file_upload_prepare_handler.go @@ -0,0 +1,28 @@ +package handler + +import ( + "net/http" + + "baidu_cloud_disk/core/internal/logic" + "baidu_cloud_disk/core/internal/svc" + "baidu_cloud_disk/core/internal/types" + "github.com/zeromicro/go-zero/rest/httpx" +) + +func FileUploadPrepareHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.FileUploadPrepareRequest + if err := httpx.Parse(r, &req); err != nil { + httpx.ErrorCtx(r.Context(), w, err) + return + } + + l := logic.NewFileUploadPrepareLogic(r.Context(), svcCtx) + resp, err := l.FileUploadPrepare(&req) + if err != nil { + httpx.ErrorCtx(r.Context(), w, err) + } else { + httpx.OkJsonCtx(r.Context(), w, resp) + } + } +} diff --git a/core/internal/handler/routes.go b/core/internal/handler/routes.go index 147c1cc..e1d0dbb 100644 --- a/core/internal/handler/routes.go +++ b/core/internal/handler/routes.go @@ -49,6 +49,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/file/upload", Handler: FileUploadHandler(serverCtx), }, + { + Method: http.MethodPost, + Path: "/file/upload/prepare", + Handler: FileUploadPrepareHandler(serverCtx), + }, { Method: http.MethodPost, Path: "/refresh/authorization", diff --git a/core/internal/logic/file_upload_prepare_logic.go b/core/internal/logic/file_upload_prepare_logic.go new file mode 100644 index 0000000..8eb3a0e --- /dev/null +++ b/core/internal/logic/file_upload_prepare_logic.go @@ -0,0 +1,47 @@ +package logic + +import ( + "baidu_cloud_disk/core/helper" + "baidu_cloud_disk/core/models" + "context" + + "baidu_cloud_disk/core/internal/svc" + "baidu_cloud_disk/core/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type FileUploadPrepareLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewFileUploadPrepareLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FileUploadPrepareLogic { + return &FileUploadPrepareLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *FileUploadPrepareLogic) FileUploadPrepare(req *types.FileUploadPrepareRequest) (resp *types.FileUploadPrepareReply, err error) { + // todo: add your logic here and delete this line + rp := new(models.RepositoryPool) + has, err := l.svcCtx.Engine.Where("hash = ?", req.Md5).Get(rp) + if err != nil { + return nil, err + } + if has { + // 妙传成功 + resp.Identity = rp.Identity + } + key, uploadId, err := helper.CosInitPart(req.Ext) + if err != nil { + return nil, err + } + resp.Key = key + resp.UploadId = uploadId + + return +} diff --git a/core/internal/types/types.go b/core/internal/types/types.go index c8db71a..20e6519 100644 --- a/core/internal/types/types.go +++ b/core/internal/types/types.go @@ -1,6 +1,18 @@ // Code generated by goctl. DO NOT EDIT. package types +type FileUploadPrepareReply struct { + Identity string `json:"identity"` + UploadId string `json:"upload_id"` + Key string `json:"key"` +} + +type FileUploadPrepareRequest struct { + Md5 string `json:"md5"` + Name string `json:"name"` + Ext string `json:"ext"` +} + type FileUploadReply struct { Identity string `json:"identity"` Ext string `json:"ext"`