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

View File

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

View File

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

View File

@ -3,10 +3,19 @@ package resources
import (
"embed"
"io"
"io/fs"
"os"
"path"
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/utils"
)
//go:embed *
var FS embed.FS
var (
//go:embed *
fsys embed.FS
FS fs.FS
)
func Asset(path string) ([]byte, error) {
f, err := FS.Open(path)
@ -15,3 +24,10 @@ func Asset(path string) ([]byte, error) {
}
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

View File

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