Simplify resources code, enabling any resource to be overridden (not just translations)

This commit is contained in:
Deluan 2021-10-28 10:25:25 -04:00
parent 9072412812
commit fa3471f527
6 changed files with 30 additions and 20 deletions

View file

@ -10,6 +10,7 @@ import (
"github.com/navidrome/navidrome/consts" "github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/db" "github.com/navidrome/navidrome/db"
"github.com/navidrome/navidrome/log" "github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/resources"
"github.com/navidrome/navidrome/scheduler" "github.com/navidrome/navidrome/scheduler"
"github.com/oklog/run" "github.com/oklog/run"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -45,7 +46,7 @@ func Execute() {
func preRun() { func preRun() {
if !noBanner { if !noBanner {
println(consts.Banner()) println(resources.Banner())
} }
conf.Load() conf.Load()
} }

View file

@ -44,7 +44,7 @@ const (
I18nFolder = "i18n" I18nFolder = "i18n"
SkipScanFile = ".ndignore" SkipScanFile = ".ndignore"
PlaceholderAlbumArt = "navidrome-600x600.png" PlaceholderAlbumArt = "placeholder.png"
PlaceholderAvatar = "logo-192x192.png" PlaceholderAvatar = "logo-192x192.png"
DefaultHttpClientTimeOut = 10 * time.Second DefaultHttpClientTimeOut = 10 * time.Second

View file

@ -1,19 +1,19 @@
package consts package resources
import ( import (
"fmt" "fmt"
"strings" "strings"
"unicode" "unicode"
"github.com/navidrome/navidrome/resources" "github.com/navidrome/navidrome/consts"
) )
func loadBanner() string { func loadBanner() string {
data, _ := resources.Asset("banner.txt") data, _ := Asset("banner.txt")
return strings.TrimRightFunc(string(data), unicode.IsSpace) return strings.TrimRightFunc(string(data), unicode.IsSpace)
} }
func Banner() string { func Banner() string {
version := "Version: " + Version() version := "Version: " + consts.Version()
return fmt.Sprintf("%s\n%52s\n", loadBanner(), version) return fmt.Sprintf("%s\n%52s\n", loadBanner(), version)
} }

View file

@ -3,10 +3,19 @@ package resources
import ( import (
"embed" "embed"
"io" "io"
"io/fs"
"os"
"path"
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/utils"
) )
//go:embed * var (
var FS embed.FS //go:embed *
fsys embed.FS
FS fs.FS
)
func Asset(path string) ([]byte, error) { func Asset(path string) ([]byte, error) {
f, err := FS.Open(path) f, err := FS.Open(path)
@ -15,3 +24,10 @@ func Asset(path string) ([]byte, error) {
} }
return io.ReadAll(f) return io.ReadAll(f)
} }
func init() {
FS = utils.MergeFS{
Base: fsys,
Overlay: os.DirFS(path.Join(conf.Server.DataFolder, "resources")),
}
}

View file

Before

Width:  |  Height:  |  Size: 297 KiB

After

Width:  |  Height:  |  Size: 297 KiB

Before After
Before After

View file

@ -6,17 +6,14 @@ import (
"encoding/json" "encoding/json"
"io" "io"
"io/fs" "io/fs"
"os"
"path" "path"
"strings" "strings"
"sync" "sync"
"github.com/deluan/rest" "github.com/deluan/rest"
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/consts" "github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/log" "github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/resources" "github.com/navidrome/navidrome/resources"
"github.com/navidrome/navidrome/utils"
) )
type translation struct { type translation struct {
@ -31,11 +28,7 @@ var (
) )
func newTranslationRepository(context.Context) rest.Repository { func newTranslationRepository(context.Context) rest.Repository {
dir := utils.MergeFS{ if err := loadTranslations(resources.FS); err != nil {
Base: resources.FS,
Overlay: os.DirFS(path.Join(conf.Server.DataFolder, "resources")),
}
if err := loadTranslations(dir); err != nil {
log.Error("Error loading translation files", err) log.Error("Error loading translation files", err)
} }
return &translationRepository{} return &translationRepository{}
@ -50,13 +43,13 @@ func (r *translationRepository) Read(id string) (interface{}, error) {
return nil, rest.ErrNotFound return nil, rest.ErrNotFound
} }
// Simple Count implementation. Does not support any `options` // Count simple implementation, does not support any `options`
func (r *translationRepository) Count(options ...rest.QueryOptions) (int64, error) { func (r *translationRepository) Count(...rest.QueryOptions) (int64, error) {
return int64(len(translations)), nil return int64(len(translations)), nil
} }
// Simple ReadAll implementation, only returns IDs. Does not support any `options` // ReadAll simple implementation, only returns IDs. Does not support any `options`
func (r *translationRepository) ReadAll(options ...rest.QueryOptions) (interface{}, error) { func (r *translationRepository) ReadAll(...rest.QueryOptions) (interface{}, error) {
var result []translation var result []translation
for _, t := range translations { for _, t := range translations {
t.Data = "" t.Data = ""