From 754088169597e6543f07d3a6ecf91acca24c464b Mon Sep 17 00:00:00 2001 From: Deluan Date: Tue, 20 Jul 2021 18:43:15 -0400 Subject: [PATCH] Small refactorings --- consts/banner.go | 5 ++--- core/artwork.go | 2 +- resources/embed.go | 9 ++------- server/nativeapi/translations.go | 2 +- server/nativeapi/translations_test.go | 2 +- server/server.go | 6 +++--- server/subsonic/media_retrieval.go | 2 +- ui/embed.go | 4 ++-- 8 files changed, 13 insertions(+), 19 deletions(-) diff --git a/consts/banner.go b/consts/banner.go index 15436fa3f..296dd8f48 100644 --- a/consts/banner.go +++ b/consts/banner.go @@ -8,13 +8,12 @@ import ( "github.com/navidrome/navidrome/resources" ) -func getBanner() string { +func loadBanner() string { data, _ := resources.Asset("banner.txt") return strings.TrimRightFunc(string(data), unicode.IsSpace) } func Banner() string { version := "Version: " + Version() - padding := strings.Repeat(" ", 52-len(version)) - return fmt.Sprintf("%s\n%s%s\n", getBanner(), padding, version) + return fmt.Sprintf("%s\n%52s\n", loadBanner(), version) } diff --git a/core/artwork.go b/core/artwork.go index 08e39cb1f..665c6df08 100644 --- a/core/artwork.go +++ b/core/artwork.go @@ -127,7 +127,7 @@ func (a *artwork) getArtwork(ctx context.Context, id string, path string, size i defer func() { if err != nil { log.Warn(ctx, "Error extracting image", "path", path, "size", size, err) - reader, err = resources.Assets().Open(consts.PlaceholderAlbumArt) + reader, err = resources.FS.Open(consts.PlaceholderAlbumArt) } }() diff --git a/resources/embed.go b/resources/embed.go index f2e42be6a..13c5d2588 100644 --- a/resources/embed.go +++ b/resources/embed.go @@ -2,21 +2,16 @@ package resources import ( "embed" - "io/fs" "io/ioutil" ) //go:embed * -var filesystem embed.FS +var FS embed.FS func Asset(path string) ([]byte, error) { - f, err := filesystem.Open(path) + f, err := FS.Open(path) if err != nil { return nil, err } return ioutil.ReadAll(f) } - -func Assets() fs.FS { - return filesystem -} diff --git a/server/nativeapi/translations.go b/server/nativeapi/translations.go index fcd82460b..176fa6ac5 100644 --- a/server/nativeapi/translations.go +++ b/server/nativeapi/translations.go @@ -31,7 +31,7 @@ var ( func newTranslationRepository(context.Context) rest.Repository { dir := utils.NewMergeFS( - http.FS(resources.Assets()), + http.FS(resources.FS), http.Dir(filepath.Join(conf.Server.DataFolder, "resources")), ) if err := loadTranslations(dir); err != nil { diff --git a/server/nativeapi/translations_test.go b/server/nativeapi/translations_test.go index 732d312f9..c10a44999 100644 --- a/server/nativeapi/translations_test.go +++ b/server/nativeapi/translations_test.go @@ -16,7 +16,7 @@ var _ = Describe("Translations", func() { Describe("I18n files", func() { var fs http.FileSystem BeforeEach(func() { - fs = http.FS(resources.Assets()) + fs = http.FS(resources.FS) }) It("contains only valid json language files", func() { dir, _ := fs.Open(consts.I18nFolder) diff --git a/server/server.go b/server/server.go index a602d969a..cfa7d5d27 100644 --- a/server/server.go +++ b/server/server.go @@ -65,7 +65,7 @@ func (s *Server) initRoutes() { r.Use(clientUniqueIdAdder) r.Use(loggerInjector) r.Use(requestLogger) - r.Use(robotsTXT(ui.Assets())) + r.Use(robotsTXT(ui.BuildAssets())) r.Use(authHeaderMapper) r.Use(jwtVerifier) @@ -99,7 +99,7 @@ func (s *Server) initRoutes() { func (s *Server) frontendAssetsHandler() http.Handler { r := chi.NewRouter() - r.Handle("/", serveIndex(s.ds, ui.Assets())) - r.Handle("/*", http.StripPrefix(s.appRoot, http.FileServer(http.FS(ui.Assets())))) + r.Handle("/", serveIndex(s.ds, ui.BuildAssets())) + r.Handle("/*", http.StripPrefix(s.appRoot, http.FileServer(http.FS(ui.BuildAssets())))) return r } diff --git a/server/subsonic/media_retrieval.go b/server/subsonic/media_retrieval.go index 86dda2fe1..55f9c2407 100644 --- a/server/subsonic/media_retrieval.go +++ b/server/subsonic/media_retrieval.go @@ -46,7 +46,7 @@ func (c *MediaRetrievalController) GetAvatar(w http.ResponseWriter, r *http.Requ } func (c *MediaRetrievalController) getPlaceHolderAvatar(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) { - f, err := resources.Assets().Open(consts.PlaceholderAvatar) + f, err := resources.FS.Open(consts.PlaceholderAvatar) if err != nil { log.Error(r, "Image not found", err) return nil, newError(responses.ErrorDataNotFound, "Avatar image not found") diff --git a/ui/embed.go b/ui/embed.go index f97f40e29..31ce8d87b 100644 --- a/ui/embed.go +++ b/ui/embed.go @@ -5,10 +5,10 @@ import ( "io/fs" ) -//go:embed build/* +//go:embed build var filesystem embed.FS -func Assets() fs.FS { +func BuildAssets() fs.FS { build, _ := fs.Sub(filesystem, "build") return build }