feat: add shared media assets and fix credit lifecycle

This commit is contained in:
OpenClaw Agent
2026-09-17 02:30:31 +08:00
parent 7d79d43b61
commit b853a239ff
11 changed files with 351 additions and 19 deletions
+17 -10
View File
@@ -40,17 +40,17 @@ func (a *App) download(id uint) {
if ext == "" {
ext = "mp3"
}
filename := safe(t.Artist) + " - " + safe(t.Title) + "." + ext
local := filepath.Join(a.cfg.DownloadDir, filename)
t.Format, t.ResolvedQuality, t.ActualQuality, t.LocalPath = ext, info.Level, "", local
// Reuse only the path recorded on this task. Never borrow another task's quality.
if t.LocalPath != "" {
if fi, statErr := os.Stat(t.LocalPath); statErr == nil && fi.Size() > 0 {
a.finishExisting(&t, fi.Size())
return
}
t.Format, t.ResolvedQuality, t.ActualQuality = ext, info.Level, ""
asset, reused, err := a.acquireAsset(&t)
if err != nil {
a.fail(&t, err)
return
}
if reused {
a.finishExisting(&t, asset.Size)
return
}
a.db.Model(&t).Updates(map[string]any{"format": t.Format, "local_path": t.LocalPath})
a.downloadFile(&t, info)
}
@@ -141,6 +141,13 @@ func (a *App) downloadFile(t *Task, info playInfo) {
t.Progress = 100
}
a.db.Save(t)
a.markAssetDone(t)
// The credit pays for the successful media download, not the later WebDAV sync.
// Commit before upload so a storage outage cannot turn a successful download into a free retry.
if err := a.commitCredit(t); err != nil {
a.fail(t, err)
return
}
a.upload(t)
}