Feat: task queue / compression task

This commit is contained in:
HFO4
2020-02-02 14:40:07 +08:00
parent b1490a665c
commit e722c33cd5
25 changed files with 573 additions and 338 deletions

View File

@@ -16,7 +16,7 @@ type Group struct {
Aria2Option string
Color string
SpeedLimit int
Options string `json:"-",gorm:"size:4096"`
Options string `json:"-",gorm:"type:text"`
// 数据库忽略字段
PolicyList []uint `gorm:"-"`
@@ -25,11 +25,11 @@ type Group struct {
// GroupOption 用户组其他配置
type GroupOption struct {
ArchiveDownloadEnabled bool `json:"archive_download,omitempty"`
ArchiveTaskEnabled bool `json:"archive_task,omitempty"`
OneTimeDownloadEnabled bool `json:"one_time_download,omitempty"`
ShareDownloadEnabled bool `json:"share_download,omitempty"`
ShareFreeEnabled bool `json:"share_free,omitempty"`
ArchiveDownload bool `json:"archive_download,omitempty"`
ArchiveTask bool `json:"archive_task,omitempty"`
OneTimeDownload bool `json:"one_time_download,omitempty"`
ShareDownload bool `json:"share_download,omitempty"`
ShareFree bool `json:"share_free,omitempty"`
}
// GetAria2Option 获取用户离线下载设备

View File

@@ -47,7 +47,7 @@ func Init() {
// Debug模式下输出所有 SQL 日志
if conf.SystemConfig.Debug {
db.LogMode(true)
db.LogMode(false)
}
//db.SetLogger(util.Log())

View File

@@ -29,7 +29,7 @@ func migration() {
if conf.DatabaseConfig.Type == "mysql" {
DB = DB.Set("gorm:table_options", "ENGINE=InnoDB")
}
DB.AutoMigrate(&User{}, &Setting{}, &Group{}, &Policy{}, &Folder{}, &File{}, &StoragePack{}, &Share{})
DB.AutoMigrate(&User{}, &Setting{}, &Group{}, &Policy{}, &Folder{}, &File{}, &StoragePack{}, &Share{}, &Task{})
// 创建初始存储策略
addDefaultPolicy()
@@ -159,7 +159,7 @@ Neue',Helvetica,Arial,sans-serif; box-sizing: border-box; font-size: 14px; verti
{Name: "aria2_token", Value: `your token`, Type: "aria2"},
{Name: "aria2_rpcurl", Value: `http://127.0.0.1:6800/`, Type: "aria2"},
{Name: "aria2_options", Value: `{"max-tries":5}`, Type: "aria2"},
{Name: "task_queue_token", Value: ``, Type: "task"},
{Name: "max_worker_num", Value: `10`, Type: "task"},
{Name: "secret_key", Value: util.RandStringRunes(256), Type: "auth"},
{Name: "temp_path", Value: "temp", Type: "path"},
{Name: "score_enabled", Value: "1", Type: "score"},
@@ -186,9 +186,9 @@ func addDefaultGroups() {
WebDAVEnabled: true,
Aria2Option: "0,0,0",
OptionsSerialized: GroupOption{
ArchiveDownloadEnabled: true,
ArchiveTaskEnabled: true,
ShareDownloadEnabled: true,
ArchiveDownload: true,
ArchiveTask: true,
ShareDownload: true,
},
}
if err := DB.Create(&defaultAdminGroup).Error; err != nil {

View File

@@ -22,14 +22,14 @@ type Policy struct {
BucketName string
IsPrivate bool
BaseURL string
AccessKey string `gorm:"size:1024"`
SecretKey string `gorm:"size:1024"`
AccessKey string `gorm:"type:text"`
SecretKey string `gorm:"type:text"`
MaxSize uint64
AutoRename bool
DirNameRule string
FileNameRule string
IsOriginLinkEnable bool
Options string `gorm:"size:4096"`
Options string `gorm:"type:text"`
// 数据库忽略字段
OptionsSerialized PolicyOption `gorm:"-"`

View File

@@ -123,7 +123,7 @@ func (share *Share) SourceFile() *File {
// CanBeDownloadBy 返回此分享是否可以被给定用户下载
func (share *Share) CanBeDownloadBy(user *User) error {
// 用户组权限
if !user.Group.OptionsSerialized.ShareDownloadEnabled {
if !user.Group.OptionsSerialized.ShareDownload {
if user.IsAnonymous() {
return errors.New("未登录用户无法下载")
}
@@ -169,7 +169,7 @@ func (share *Share) DownloadBy(user *User, c *gin.Context) error {
// Purchase 使用积分购买分享
func (share *Share) Purchase(user *User) error {
// 不需要付积分
if share.Score == 0 || user.Group.OptionsSerialized.ShareFreeEnabled || user.ID == share.UserID {
if share.Score == 0 || user.Group.OptionsSerialized.ShareFree || user.ID == share.UserID {
return nil
}

View File

@@ -156,7 +156,7 @@ func TestShare_CanBeDownloadBy(t *testing.T) {
user := &User{
Group: Group{
OptionsSerialized: GroupOption{
ShareDownloadEnabled: false,
ShareDownload: false,
},
},
}
@@ -169,7 +169,7 @@ func TestShare_CanBeDownloadBy(t *testing.T) {
Model: gorm.Model{ID: 1},
Group: Group{
OptionsSerialized: GroupOption{
ShareDownloadEnabled: false,
ShareDownload: false,
},
},
}
@@ -181,7 +181,7 @@ func TestShare_CanBeDownloadBy(t *testing.T) {
user := &User{
Group: Group{
OptionsSerialized: GroupOption{
ShareDownloadEnabled: true,
ShareDownload: true,
},
},
}
@@ -195,7 +195,7 @@ func TestShare_CanBeDownloadBy(t *testing.T) {
Model: gorm.Model{ID: 1},
Group: Group{
OptionsSerialized: GroupOption{
ShareDownloadEnabled: true,
ShareDownload: true,
},
},
}
@@ -259,10 +259,10 @@ func TestShare_Purchase(t *testing.T) {
asserts.NoError(share.Purchase(&user))
share.Score = 1
user.Group.OptionsSerialized.ShareFreeEnabled = true
user.Group.OptionsSerialized.ShareFree = true
asserts.NoError(share.Purchase(&user))
user.Group.OptionsSerialized.ShareFreeEnabled = false
user.Group.OptionsSerialized.ShareFree = false
share.UserID = 1
user.ID = 1
asserts.NoError(share.Purchase(&user))

48
models/task.go Normal file
View File

@@ -0,0 +1,48 @@
package model
import (
"github.com/HFO4/cloudreve/pkg/util"
"github.com/jinzhu/gorm"
)
// Task 任务模型
type Task struct {
gorm.Model
Status int // 任务状态
Type int // 任务类型
UserID uint // 发起者UID0表示为系统发起
Progress int // 进度
Error string // 错误信息
Props string `gorm:"type:text"` // 任务属性
}
// Create 创建任务记录
func (task *Task) Create() (uint, error) {
if err := DB.Create(task).Error; err != nil {
util.Log().Warning("无法插入任务记录, %s", err)
return 0, err
}
return task.ID, nil
}
// SetStatus 设定任务状态
func (task *Task) SetStatus(status int) error {
return DB.Model(task).Select("status").Updates(map[string]interface{}{"status": status}).Error
}
// SetProgress 设定任务进度
func (task *Task) SetProgress(progress int) error {
return DB.Model(task).Select("progress").Updates(map[string]interface{}{"progress": progress}).Error
}
// SetError 设定错误信息
func (task *Task) SetError(err string) error {
return DB.Model(task).Select("error").Updates(map[string]interface{}{"error": err}).Error
}
// GetTasksByStatus 根据状态检索任务
func GetTasksByStatus(status int) []Task {
var tasks []Task
DB.Where("status = ?", status).Find(&tasks)
return tasks
}

View File

@@ -37,8 +37,8 @@ type User struct {
TwoFactor string `json:"-"`
Delay int
Avatar string
Options string `json:"-",gorm:"size:4096"`
Authn string `gorm:"size:8192"`
Options string `json:"-",gorm:"type:text"`
Authn string `gorm:"type:text"`
Score int
// 关联模型