Skip to content

Commit

Permalink
Feat: support chunk size option in policy
Browse files Browse the repository at this point in the history
  • Loading branch information
HFO4 committed Feb 10, 2022
1 parent de9c410 commit 8443a30
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 12 deletions.
2 changes: 2 additions & 0 deletions models/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ type PolicyOption struct {
Region string `json:"region,omitempty"`
// ServerSideEndpoint 服务端请求使用的 Endpoint,为空时使用 Policy.Server 字段
ServerSideEndpoint string `json:"server_side_endpoint,omitempty"`
// 分片上传的分片大小
ChunkSize uint64 `json:"chunk_size,omitempty"`
}

var thumbSuffix = map[string][]string{
Expand Down
1 change: 1 addition & 0 deletions pkg/filesystem/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ func (fs *FileSystem) CreateUploadSession(ctx context.Context, path string, size
Name: name,
Size: size,
SavePath: savePath,
ChunkSize: fs.Policy.OptionsSerialized.ChunkSize,
}

// 获取上传凭证
Expand Down
22 changes: 12 additions & 10 deletions pkg/serializer/explorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ type Object struct {

// PolicySummary 用于前端组件使用的存储策略概况
type PolicySummary struct {
ID string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
MaxSize uint64 `json:"max_size"`
FileType []string `json:"file_type"`
ID string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
MaxSize uint64 `json:"max_size"`
FileType []string `json:"file_type"`
ChunkSize uint64 `json:"chunk_size"`
}

// BuildObjectList 构建列目录响应
Expand All @@ -65,11 +66,12 @@ func BuildObjectList(parent uint, objects []Object, policy *model.Policy) Object

if policy != nil {
res.Policy = &PolicySummary{
ID: hashid.HashID(policy.ID, hashid.PolicyID),
Name: policy.Name,
Type: policy.Type,
MaxSize: policy.MaxSize,
FileType: policy.OptionsSerialized.FileType,
ID: hashid.HashID(policy.ID, hashid.PolicyID),
Name: policy.Name,
Type: policy.Type,
MaxSize: policy.MaxSize,
FileType: policy.OptionsSerialized.FileType,
ChunkSize: policy.OptionsSerialized.ChunkSize,
}
}

Expand Down
1 change: 1 addition & 0 deletions pkg/serializer/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type UploadSession struct {
Name string
Size uint64
SavePath string
ChunkSize uint64
}

// UploadCallback 上传回调正文
Expand Down
11 changes: 9 additions & 2 deletions service/explorer/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/cloudreve/Cloudreve/v3/pkg/filesystem"
"github.com/cloudreve/Cloudreve/v3/pkg/hashid"
"github.com/cloudreve/Cloudreve/v3/pkg/serializer"
"github.com/gin-gonic/gin"
)
Expand All @@ -13,7 +14,7 @@ type UploadSessionService struct {
Path string `json:"path" binding:"required"`
Size uint64 `json:"size" binding:"min=0"`
Name string `json:"name" binding:"required"`
PolicyID uint `json:"policy_id" binding:"required"`
PolicyID string `json:"policy_id" binding:"required"`
}

// Create 创建新的上传会话
Expand All @@ -24,7 +25,13 @@ func (service *UploadSessionService) Create(ctx context.Context, c *gin.Context)
return serializer.Err(serializer.CodePolicyNotAllowed, err.Error(), err)
}

if fs.Policy.ID != service.PolicyID {
// 取得存储策略的ID
rawID, err := hashid.DecodeHashID(service.PolicyID, hashid.PolicyID)
if err != nil {
return serializer.Err(serializer.CodeNotFound, "存储策略不存在", err)
}

if fs.Policy.ID != rawID {
return serializer.Err(serializer.CodePolicyNotAllowed, "存储策略发生变化,请刷新文件列表并重新添加此任务", nil)
}

Expand Down

0 comments on commit 8443a30

Please sign in to comment.