diff --git a/models/download.go b/models/download.go new file mode 100644 index 0000000..eb80ce0 --- /dev/null +++ b/models/download.go @@ -0,0 +1,30 @@ +package model + +import ( + "github.com/HFO4/cloudreve/pkg/util" + "github.com/jinzhu/gorm" +) + +// Download 离线下载队列模型 +type Download struct { + gorm.Model + Status int // 任务状态 + Type int // 任务类型 + Name string // 任务文件名 + Size uint64 // 文件大小 + PID string // 任务ID + Path string `gorm:"type:text"` // 存储路径 + Attrs string `gorm:"type:text"` // 任务状态属性 + FolderID uint // 存储父目录ID + UserID uint // 发起者UID + TaskID uint // 对应的转存任务ID +} + +// Create 创建离线下载记录 +func (task *Download) Create() (uint, error) { + if err := DB.Create(task).Error; err != nil { + util.Log().Warning("无法插入离线下载记录, %s", err) + return 0, err + } + return task.ID, nil +} diff --git a/models/file.go b/models/file.go index 759425e..17473a2 100644 --- a/models/file.go +++ b/models/file.go @@ -13,8 +13,8 @@ type File struct { // 表字段 gorm.Model Name string `gorm:"unique_index:idx_only_one"` - SourceName string - UserID uint `gorm:"index:user_id;unique_index:idx_only_one"` + SourceName string `gorm:"type:text"` + UserID uint `gorm:"index:user_id;unique_index:idx_only_one"` Size uint64 PicInfo string FolderID uint `gorm:"index:folder_id;unique_index:idx_only_one"` diff --git a/pkg/filesystem/driver/onedrive/api.go b/pkg/filesystem/driver/onedrive/api.go index 4c26eaf..d0a674d 100644 --- a/pkg/filesystem/driver/onedrive/api.go +++ b/pkg/filesystem/driver/onedrive/api.go @@ -269,7 +269,28 @@ func (client *Client) SimpleUpload(ctx context.Context, dst string, body io.Read return &uploadRes, nil } -// Delete 并行删除文件,返回删除失败的文件,及第一个遇到的错误 +// BatchDelete 并行删除给出的文件,返回删除失败的文件,及第一个遇到的错误。此方法将文件分为 +// 20个一组,调用Delete并行删除 +// TODO 测试 +func (client *Client) BatchDelete(ctx context.Context, dst []string) ([]string, error) { + groupNum := len(dst)/20 + 1 + finalRes := make([]string, 0, len(dst)) + res := make([]string, 0, 20) + var err error + + for i := 0; i < groupNum; i++ { + end := 20*i + 20 + if i == groupNum-1 { + end = len(dst) + } + res, err = client.Delete(ctx, dst[20*i:end]) + finalRes = append(finalRes, res...) + } + + return finalRes, err +} + +// Delete 并行删除文件,返回删除失败的文件,及第一个遇到的错误,最多删除20个 func (client *Client) Delete(ctx context.Context, dst []string) ([]string, error) { body := client.makeBatchDeleteRequestsBody(dst) res, err := client.requestWithStr(ctx, "POST", client.getRequestURL("$batch"), body, 200) diff --git a/pkg/filesystem/driver/onedrive/handller.go b/pkg/filesystem/driver/onedrive/handller.go index b04dce3..6663545 100644 --- a/pkg/filesystem/driver/onedrive/handller.go +++ b/pkg/filesystem/driver/onedrive/handller.go @@ -64,7 +64,7 @@ func (handler Driver) Put(ctx context.Context, file io.ReadCloser, dst string, s // Delete 删除一个或多个文件, // 返回未删除的文件,及遇到的最后一个错误 func (handler Driver) Delete(ctx context.Context, files []string) ([]string, error) { - return handler.Client.Delete(ctx, files) + return handler.Client.BatchDelete(ctx, files) } // Thumb 获取文件缩略图 diff --git a/routers/router.go b/routers/router.go index b24ac73..7fe27e8 100644 --- a/routers/router.go +++ b/routers/router.go @@ -273,6 +273,9 @@ func InitMasterRouter() *gin.Engine { file.POST("decompress", controllers.Decompress) } + // 离线下载任务 + //aria2 := auth.Group("aria2") + // 目录 directory := auth.Group("directory") {