feat: harden multi-user download concurrency

This commit is contained in:
OpenClaw Agent
2026-09-18 09:06:59 +08:00
parent 0735cb2e09
commit b116303abe
11 changed files with 1003 additions and 202 deletions
+67 -25
View File
@@ -7,48 +7,63 @@ import (
)
func (a *App) upload(t *Task) {
if t.RemotePath != "" && t.Status == "done" {
return
}
storage, root, enabled := a.storageForTask(t)
if !enabled || storage == nil || root == "" {
t.Status, t.Progress = "done", 100
a.db.Save(t)
if err := a.commitCredit(t); err != nil {
a.fail(t, err)
return
if t.Status == "upload_pending" {
if err := a.db.Model(t).Updates(map[string]any{
"status": "done", "error": "",
}).Error; err != nil {
return
}
t.Status, t.Error = "done", ""
}
return
}
a.db.Model(t).Updates(map[string]any{"status": "uploading"})
remote := strings.TrimRight(root, "/") + "/" + safe(t.Artist) + "/" + safe(t.Album) + "/" + safe(t.Title) + "." + safe(t.Format)
if !a.claimUpload(t) {
return
}
remote := strings.TrimRight(root, "/") + "/" + safe(t.Artist) + "/" +
safe(t.Album) + "/" + safe(t.Title) + "." + safe(t.Format)
if err := storage.MkdirAll(filepath.ToSlash(filepath.Dir(remote))); err != nil {
a.failUpload(t, err)
return
}
if fi, err := os.Stat(t.LocalPath); err == nil {
if same, _ := storage.ExistsSameSize(remote, fi.Size()); same {
t.Status, t.Progress = "done", 100
a.db.Save(t)
if err := a.commitCredit(t); err != nil {
a.fail(t, err)
return
}
a.cleanupLocalFile(t)
fi, statErr := os.Stat(t.LocalPath)
if statErr != nil {
if a.recoverMissingLocalUpload(t) {
return
}
a.failUpload(t, statErr)
return
}
if same, _ := storage.ExistsSameSize(remote, fi.Size()); same {
t.RemotePath = remote
t.Status, t.Progress, t.Error = "done", 100, ""
_ = a.db.Model(t).Updates(map[string]any{
"remote_path": remote, "status": "done",
"progress": 100, "error": "",
}).Error
a.cleanupLocalFile(t)
return
}
if err := storage.Upload(t.LocalPath, remote); err != nil {
a.failUpload(t, err)
return
}
t.RemotePath = remote
t.Status, t.Progress, t.Error = "done", 100, ""
if err := a.db.Model(t).Updates(map[string]any{
"remote_path": remote, "status": "done",
"progress": 100, "error": "",
}).Error; err != nil {
return
}
if a.cfg.WebDAVVersionPolicy == "replace_lower" {
a.cleanupLowerQualityVersions(t, storage)
}
t.Status, t.Progress = "done", 100
a.db.Save(t)
if err := a.commitCredit(t); err != nil {
a.fail(t, err)
return
}
a.cleanupLocalFile(t)
}
@@ -56,20 +71,47 @@ func (a *App) cleanupLocalFile(t *Task) {
if a.cfg.LocalFilePolicy != "delete_after_upload" || t.LocalPath == "" {
return
}
if t.AssetID != 0 {
var pending int64
if err := a.db.Model(&Task{}).
Where("asset_id = ? AND id <> ? AND local_path = ? AND "+
"(status IN ? OR (status = ? AND (remote_path = '' OR remote_path IS NULL)))",
t.AssetID, t.ID, t.LocalPath,
[]string{"pending", "resolving", "downloading", "uploading", "upload_pending"},
"done").Count(&pending).Error; err != nil {
return
}
if pending > 0 {
return
}
}
if err := os.Remove(t.LocalPath); err != nil && !os.IsNotExist(err) {
t.Error = "WebDAV 上传成功,但删除本地文件失败: " + err.Error()
a.db.Save(t)
_ = a.db.Save(t)
return
}
if t.AssetID != 0 {
_ = a.db.Model(&MediaAsset{}).
Where("id = ? AND local_path = ?", t.AssetID, t.LocalPath).
Updates(map[string]any{
"status": "evicted", "local_path": "",
"size": 0, "owner_task_id": 0, "lease_until": nil,
}).Error
}
}
func (a *App) cleanupLowerQualityVersions(t *Task, storage RemoteStorage) {
levels := map[string]int{"standard": 1, "exhigh": 2, "sq": 3, "lossless": 4, "hires": 5, "jyeffect": 6, "sky": 7, "jymaster": 8, "atmos": 9, "atmos_plus": 10, "master": 11}
levels := map[string]int{"standard": 1, "exhigh": 2, "sq": 3,
"lossless": 4, "hires": 5, "jyeffect": 6, "sky": 7,
"jymaster": 8, "atmos": 9, "atmos_plus": 10, "master": 11}
current := qualityRank(levels, t.ActualQuality, t.ResolvedQuality, t.Quality)
if current == 0 {
return
}
var old []Task
a.db.Where("user_id = ? AND artist = ? AND title = ? AND album = ? AND status = ? AND id <> ?", t.UserID, t.Artist, t.Title, t.Album, "done", t.ID).Find(&old)
a.db.Where("user_id = ? AND artist = ? AND title = ? AND album = ? "+
"AND status = ? AND id <> ?", t.UserID, t.Artist, t.Title, t.Album, "done", t.ID).Find(&old)
for _, candidate := range old {
q := qualityRank(levels, candidate.ActualQuality, candidate.ResolvedQuality, candidate.Quality)
if q == 0 || q >= current || candidate.RemotePath == "" || candidate.RemotePath == t.RemotePath {