|
|
@ -1,9 +1,13 @@
|
|
|
|
package aria2
|
|
|
|
package aria2
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"errors"
|
|
|
|
model "github.com/HFO4/cloudreve/models"
|
|
|
|
model "github.com/HFO4/cloudreve/models"
|
|
|
|
|
|
|
|
"github.com/HFO4/cloudreve/pkg/filesystem"
|
|
|
|
|
|
|
|
"github.com/HFO4/cloudreve/pkg/filesystem/driver/local"
|
|
|
|
|
|
|
|
"github.com/HFO4/cloudreve/pkg/filesystem/fsctx"
|
|
|
|
"github.com/HFO4/cloudreve/pkg/task"
|
|
|
|
"github.com/HFO4/cloudreve/pkg/task"
|
|
|
|
"github.com/HFO4/cloudreve/pkg/util"
|
|
|
|
"github.com/HFO4/cloudreve/pkg/util"
|
|
|
|
"github.com/zyxar/argo/rpc"
|
|
|
|
"github.com/zyxar/argo/rpc"
|
|
|
@ -71,9 +75,18 @@ func (monitor *Monitor) Update() bool {
|
|
|
|
return true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 磁力链下载需要跟随
|
|
|
|
|
|
|
|
if len(status.FollowedBy) > 0 {
|
|
|
|
|
|
|
|
util.Log().Debug("离线下载[%s]重定向至[%s]", monitor.Task.GID, status.FollowedBy[0])
|
|
|
|
|
|
|
|
monitor.Task.GID = status.FollowedBy[0]
|
|
|
|
|
|
|
|
monitor.Task.Save()
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 更新任务信息
|
|
|
|
// 更新任务信息
|
|
|
|
if err := monitor.UpdateTaskInfo(status); err != nil {
|
|
|
|
if err := monitor.UpdateTaskInfo(status); err != nil {
|
|
|
|
util.Log().Warning("无法更新下载任务[%s]的任务信息[%s],", monitor.Task.GID, err)
|
|
|
|
util.Log().Warning("无法更新下载任务[%s]的任务信息[%s],", monitor.Task.GID, err)
|
|
|
|
|
|
|
|
monitor.setErrorStatus(err)
|
|
|
|
return true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -96,6 +109,9 @@ func (monitor *Monitor) Update() bool {
|
|
|
|
|
|
|
|
|
|
|
|
// UpdateTaskInfo 更新数据库中的任务信息
|
|
|
|
// UpdateTaskInfo 更新数据库中的任务信息
|
|
|
|
func (monitor *Monitor) UpdateTaskInfo(status rpc.StatusInfo) error {
|
|
|
|
func (monitor *Monitor) UpdateTaskInfo(status rpc.StatusInfo) error {
|
|
|
|
|
|
|
|
originSize := monitor.Task.TotalSize
|
|
|
|
|
|
|
|
originPath := monitor.Task.Path
|
|
|
|
|
|
|
|
|
|
|
|
monitor.Task.GID = status.Gid
|
|
|
|
monitor.Task.GID = status.Gid
|
|
|
|
monitor.Task.Status = getStatus(status.Status)
|
|
|
|
monitor.Task.Status = getStatus(status.Status)
|
|
|
|
|
|
|
|
|
|
|
@ -126,7 +142,68 @@ func (monitor *Monitor) UpdateTaskInfo(status rpc.StatusInfo) error {
|
|
|
|
attrs, _ := json.Marshal(status)
|
|
|
|
attrs, _ := json.Marshal(status)
|
|
|
|
monitor.Task.Attrs = string(attrs)
|
|
|
|
monitor.Task.Attrs = string(attrs)
|
|
|
|
|
|
|
|
|
|
|
|
return monitor.Task.Save()
|
|
|
|
if err := monitor.Task.Save(); err != nil {
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if originSize != monitor.Task.TotalSize || originPath != monitor.Task.Path {
|
|
|
|
|
|
|
|
// 大小、文件名更新后,对文件限制等进行校验
|
|
|
|
|
|
|
|
if err := monitor.ValidateFile(); err != nil {
|
|
|
|
|
|
|
|
// 验证失败时取消任务
|
|
|
|
|
|
|
|
monitor.Cancel()
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Cancel 取消上传并尝试删除临时文件
|
|
|
|
|
|
|
|
func (monitor *Monitor) Cancel() {
|
|
|
|
|
|
|
|
if err := Instance.Cancel(monitor.Task); err != nil {
|
|
|
|
|
|
|
|
util.Log().Warning("无法取消离线下载任务[%s], %s", monitor.Task.GID, err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
util.Log().Debug("离线下载任务[%s]已取消,1 分钟后删除临时文件", monitor.Task.GID)
|
|
|
|
|
|
|
|
go func(monitor *Monitor) {
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
|
|
|
case <-time.After(time.Duration(60) * time.Second):
|
|
|
|
|
|
|
|
monitor.RemoveTempFolder()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}(monitor)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ValidateFile 上传过程中校验文件大小、文件名
|
|
|
|
|
|
|
|
func (monitor *Monitor) ValidateFile() error {
|
|
|
|
|
|
|
|
// 找到任务创建者
|
|
|
|
|
|
|
|
user := monitor.Task.GetOwner()
|
|
|
|
|
|
|
|
if user == nil {
|
|
|
|
|
|
|
|
return ErrUserNotFound
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建文件系统
|
|
|
|
|
|
|
|
fs, err := filesystem.NewFileSystem(user)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
defer fs.Recycle()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建上下文环境
|
|
|
|
|
|
|
|
ctx := context.WithValue(context.Background(), fsctx.FileHeaderCtx, local.FileStream{
|
|
|
|
|
|
|
|
Size: monitor.Task.TotalSize,
|
|
|
|
|
|
|
|
Name: filepath.Base(monitor.Task.Path),
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 验证文件
|
|
|
|
|
|
|
|
if err := filesystem.HookValidateFile(ctx, fs); err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 验证用户容量
|
|
|
|
|
|
|
|
if err := filesystem.HookValidateCapacityWithoutIncrease(ctx, fs); err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Error 任务下载出错处理,返回是否中断监控
|
|
|
|
// Error 任务下载出错处理,返回是否中断监控
|
|
|
|