github.com/cloudreve/Cloudreve/v3@v3.0.0-20240224133659-3edb00a6484c/models/download.go (about) 1 package model 2 3 import ( 4 "encoding/json" 5 6 "github.com/cloudreve/Cloudreve/v3/pkg/aria2/rpc" 7 "github.com/cloudreve/Cloudreve/v3/pkg/util" 8 "github.com/jinzhu/gorm" 9 ) 10 11 // Download 离线下载队列模型 12 type Download struct { 13 gorm.Model 14 Status int // 任务状态 15 Type int // 任务类型 16 Source string `gorm:"type:text"` // 文件下载地址 17 TotalSize uint64 // 文件大小 18 DownloadedSize uint64 // 文件大小 19 GID string `gorm:"size:32,index:gid"` // 任务ID 20 Speed int // 下载速度 21 Parent string `gorm:"type:text"` // 存储目录 22 Attrs string `gorm:"size:4294967295"` // 任务状态属性 23 Error string `gorm:"type:text"` // 错误描述 24 Dst string `gorm:"type:text"` // 用户文件系统存储父目录路径 25 UserID uint // 发起者UID 26 TaskID uint // 对应的转存任务ID 27 NodeID uint // 处理任务的节点ID 28 29 // 关联模型 30 User *User `gorm:"PRELOAD:false,association_autoupdate:false"` 31 32 // 数据库忽略字段 33 StatusInfo rpc.StatusInfo `gorm:"-"` 34 Task *Task `gorm:"-"` 35 NodeName string `gorm:"-"` 36 } 37 38 // AfterFind 找到下载任务后的钩子,处理Status结构 39 func (task *Download) AfterFind() (err error) { 40 // 解析状态 41 if task.Attrs != "" { 42 err = json.Unmarshal([]byte(task.Attrs), &task.StatusInfo) 43 } 44 45 if task.TaskID != 0 { 46 task.Task, _ = GetTasksByID(task.TaskID) 47 } 48 49 return err 50 } 51 52 // BeforeSave Save下载任务前的钩子 53 func (task *Download) BeforeSave() (err error) { 54 // 解析状态 55 if task.Attrs != "" { 56 err = json.Unmarshal([]byte(task.Attrs), &task.StatusInfo) 57 } 58 return err 59 } 60 61 // Create 创建离线下载记录 62 func (task *Download) Create() (uint, error) { 63 if err := DB.Create(task).Error; err != nil { 64 util.Log().Warning("Failed to insert download record: %s", err) 65 return 0, err 66 } 67 return task.ID, nil 68 } 69 70 // Save 更新 71 func (task *Download) Save() error { 72 if err := DB.Save(task).Error; err != nil { 73 util.Log().Warning("Failed to update download record: %s", err) 74 return err 75 } 76 return nil 77 } 78 79 // GetDownloadsByStatus 根据状态检索下载 80 func GetDownloadsByStatus(status ...int) []Download { 81 var tasks []Download 82 DB.Where("status in (?)", status).Find(&tasks) 83 return tasks 84 } 85 86 // GetDownloadsByStatusAndUser 根据状态检索和用户ID下载 87 // page 为 0 表示列出所有,非零时分页 88 func GetDownloadsByStatusAndUser(page, uid uint, status ...int) []Download { 89 var tasks []Download 90 dbChain := DB 91 if page > 0 { 92 dbChain = dbChain.Limit(10).Offset((page - 1) * 10).Order("updated_at DESC") 93 } 94 dbChain.Where("user_id = ? and status in (?)", uid, status).Find(&tasks) 95 return tasks 96 } 97 98 // GetDownloadByGid 根据GID和用户ID查找下载 99 func GetDownloadByGid(gid string, uid uint) (*Download, error) { 100 download := &Download{} 101 result := DB.Where("user_id = ? and g_id = ?", uid, gid).First(download) 102 return download, result.Error 103 } 104 105 // GetOwner 获取下载任务所属用户 106 func (task *Download) GetOwner() *User { 107 if task.User == nil { 108 if user, err := GetUserByID(task.UserID); err == nil { 109 return &user 110 } 111 } 112 return task.User 113 } 114 115 // Delete 删除离线下载记录 116 func (download *Download) Delete() error { 117 return DB.Model(download).Delete(download).Error 118 } 119 120 // GetNodeID 返回任务所属节点ID 121 func (task *Download) GetNodeID() uint { 122 // 兼容3.4版本之前生成的下载记录 123 if task.NodeID == 0 { 124 return 1 125 } 126 127 return task.NodeID 128 }