use a "CacheCover" folder for scaled covers

This commit is contained in:
sentriz
2020-07-24 20:24:57 +01:00
committed by Senan Kelly
parent 850f2f1216
commit dae1e03940
4 changed files with 32 additions and 15 deletions

View File

@@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"path"
"regexp" "regexp"
"time" "time"
@@ -17,6 +18,11 @@ import (
"go.senan.xyz/gonic/version" "go.senan.xyz/gonic/version"
) )
const (
cleanTimeDuration = 10 * time.Minute
coverCachePrefix = "covers"
)
func main() { func main() {
set := flag.NewFlagSet(version.NAME, flag.ExitOnError) set := flag.NewFlagSet(version.NAME, flag.ExitOnError)
listenAddr := set.String("listen-addr", "0.0.0.0:4747", "listen address (optional)") listenAddr := set.String("listen-addr", "0.0.0.0:4747", "listen address (optional)")
@@ -45,6 +51,7 @@ func main() {
set.VisitAll(func(f *flag.Flag) { set.VisitAll(func(f *flag.Flag) {
log.Printf(" %-15s %s\n", f.Name, f.Value) log.Printf(" %-15s %s\n", f.Name, f.Value)
}) })
//
if _, err := os.Stat(*musicPath); os.IsNotExist(err) { if _, err := os.Stat(*musicPath); os.IsNotExist(err) {
log.Fatal("please provide a valid music directory") log.Fatal("please provide a valid music directory")
} }
@@ -53,6 +60,12 @@ func main() {
log.Fatalf("couldn't create cache path: %v\n", err) log.Fatalf("couldn't create cache path: %v\n", err)
} }
} }
coverCachePath := path.Join(*cachePath, "covers")
if _, err := os.Stat(coverCachePath); os.IsNotExist(err) {
if err := os.MkdirAll(coverCachePath, os.ModePerm); err != nil {
log.Fatalf("couldn't create cover cache path: %v\n", err)
}
}
db, err := db.New(*dbPath) db, err := db.New(*dbPath)
if err != nil { if err != nil {
log.Fatalf("error opening database: %v\n", err) log.Fatalf("error opening database: %v\n", err)
@@ -63,14 +76,15 @@ func main() {
*proxyPrefix = proxyPrefixExpr.ReplaceAllString(*proxyPrefix, `/$1`) *proxyPrefix = proxyPrefixExpr.ReplaceAllString(*proxyPrefix, `/$1`)
// //
server := server.New(server.Options{ server := server.New(server.Options{
DB: db, DB: db,
MusicPath: *musicPath, MusicPath: *musicPath,
CachePath: *cachePath, CachePath: *cachePath,
ProxyPrefix: *proxyPrefix, CoverCachePath: coverCachePath,
ProxyPrefix: *proxyPrefix,
}) })
var g run.Group var g run.Group
g.Add(server.StartHTTP(*listenAddr)) g.Add(server.StartHTTP(*listenAddr))
g.Add(server.StartSessionClean(10 * time.Minute)) g.Add(server.StartSessionClean(cleanTimeDuration))
if *scanInterval > 0 { if *scanInterval > 0 {
tickerDur := time.Duration(*scanInterval) * time.Minute tickerDur := time.Duration(*scanInterval) * time.Minute
g.Add(server.StartScanTicker(tickerDur)) g.Add(server.StartScanTicker(tickerDur))

View File

@@ -25,8 +25,9 @@ const (
type Controller struct { type Controller struct {
*ctrlbase.Controller *ctrlbase.Controller
CachePath string CachePath string
Jukebox *jukebox.Jukebox CoverCachePath string
Jukebox *jukebox.Jukebox
} }
type metaResponse struct { type metaResponse struct {

View File

@@ -106,7 +106,7 @@ func (c *Controller) ServeGetCoverArt(w http.ResponseWriter, r *http.Request) *s
} }
size := params.GetOrInt("size", coverDefaultSize) size := params.GetOrInt("size", coverDefaultSize)
cachePath := path.Join( cachePath := path.Join(
c.CachePath, c.CoverCachePath,
fmt.Sprintf("%s-%d.%s", id.String(), size, coverCacheFormat), fmt.Sprintf("%s-%d.%s", id.String(), size, coverCacheFormat),
) )
_, err = os.Stat(cachePath) _, err = os.Stat(cachePath)

View File

@@ -21,10 +21,11 @@ import (
) )
type Options struct { type Options struct {
DB *db.DB DB *db.DB
MusicPath string MusicPath string
CachePath string CachePath string
ProxyPrefix string CoverCachePath string
ProxyPrefix string
} }
type Server struct { type Server struct {
@@ -61,9 +62,10 @@ func New(opts Options) *Server {
// //
ctrlAdmin := ctrladmin.New(base, sessDB) ctrlAdmin := ctrladmin.New(base, sessDB)
ctrlSubsonic := &ctrlsubsonic.Controller{ ctrlSubsonic := &ctrlsubsonic.Controller{
Controller: base, Controller: base,
CachePath: opts.CachePath, CachePath: opts.CachePath,
Jukebox: jukebox, CoverCachePath: opts.CoverCachePath,
Jukebox: jukebox,
} }
setupMisc(r, base) setupMisc(r, base)
setupAdmin(r.PathPrefix("/admin").Subrouter(), ctrlAdmin) setupAdmin(r.PathPrefix("/admin").Subrouter(), ctrlAdmin)