feat: add shared media assets and fix credit lifecycle
This commit is contained in:
+17
-10
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user