Skip to content

Commit

Permalink
文件上传前基本信息处理
Browse files Browse the repository at this point in the history
  • Loading branch information
nateshao committed Aug 18, 2024
1 parent 29e24f0 commit 79b91a6
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 4 deletions.
20 changes: 16 additions & 4 deletions core/core.api
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
28 changes: 28 additions & 0 deletions core/internal/handler/file_upload_prepare_handler.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
5 changes: 5 additions & 0 deletions core/internal/handler/routes.go

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

47 changes: 47 additions & 0 deletions core/internal/logic/file_upload_prepare_logic.go
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 12 additions & 0 deletions core/internal/types/types.go

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

0 comments on commit 79b91a6

Please sign in to comment.