feat: implement multi-user credits redeem and storage isolation

This commit is contained in:
OpenClaw Agent
2026-09-17 02:18:49 +08:00
parent 7628390966
commit 8b31956c6d
12 changed files with 624 additions and 28 deletions
+22 -9
View File
@@ -7,35 +7,48 @@ import (
)
func (a *App) upload(t *Task) {
if a.storage == nil || a.cfg.WebDAVURL == "" {
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
}
return
}
a.db.Model(t).Updates(map[string]any{"status": "uploading"})
remote := strings.TrimRight(a.cfg.WebDAVPath, "/") + "/" + safe(t.Artist) + "/" + safe(t.Album) + "/" + safe(t.Title) + "." + safe(t.Format)
remote := strings.TrimRight(root, "/") + "/" + safe(t.Artist) + "/" + safe(t.Album) + "/" + safe(t.Title) + "." + safe(t.Format)
t.RemotePath = remote
if err := a.storage.MkdirAll(filepath.ToSlash(filepath.Dir(remote))); err != nil {
if err := storage.MkdirAll(filepath.ToSlash(filepath.Dir(remote))); err != nil {
a.fail(t, err)
return
}
if fi, err := os.Stat(t.LocalPath); err == nil {
if same, _ := a.storage.ExistsSameSize(remote, fi.Size()); same {
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)
return
}
}
if err := a.storage.Upload(t.LocalPath, remote); err != nil {
if err := storage.Upload(t.LocalPath, remote); err != nil {
a.fail(t, err)
return
}
if a.cfg.WebDAVVersionPolicy == "replace_lower" {
a.cleanupLowerQualityVersions(t)
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)
}
@@ -49,20 +62,20 @@ func (a *App) cleanupLocalFile(t *Task) {
}
}
func (a *App) cleanupLowerQualityVersions(t *Task) {
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}
current := qualityRank(levels, t.ActualQuality, t.ResolvedQuality, t.Quality)
if current == 0 {
return
}
var old []Task
a.db.Where("artist = ? AND title = ? AND album = ? AND status = ? AND id <> ?", 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 {
continue
}
_ = a.storage.Remove(candidate.RemotePath)
_ = storage.Remove(candidate.RemotePath)
}
}