43 lines
825 B
Go
43 lines
825 B
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"path/filepath"
|
|
)
|
|
|
|
func (a *App) fail(t *Task, err error) {
|
|
if t.LocalPath != "" {
|
|
_ = os.Remove(t.LocalPath + ".part")
|
|
}
|
|
a.releaseAsset(t)
|
|
if err == nil {
|
|
err = errors.New("unknown download error")
|
|
}
|
|
t.Status, t.Error = "error", err.Error()
|
|
a.db.Save(t)
|
|
if refundErr := a.refundCredit(t); refundErr != nil {
|
|
t.Error += "; credit refund failed: " + refundErr.Error()
|
|
a.db.Save(t)
|
|
}
|
|
}
|
|
|
|
var bad = regexp.MustCompile(`[/\\:*?"<>|]+`)
|
|
|
|
func safe(s string) string {
|
|
s = strings.TrimSpace(s)
|
|
s = bad.ReplaceAllString(s, "_")
|
|
s = strings.Trim(s, ". ")
|
|
if s == "" {
|
|
return "Unknown"
|
|
}
|
|
return s
|
|
}
|
|
|
|
func audioPath(dir, artist, title, format string) string {
|
|
return filepath.Join(dir, safe(artist)+" - "+safe(title)+"."+normalizedFormat(format))
|
|
}
|