From adbb5b9d38013d6aaf5997c53d57897faee0880b Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Thu, 18 Feb 2021 12:58:30 +0000 Subject: [PATCH] Fix filepath separator and external files on windows For some reason, '/' is used instead of '\' on windows when loading lang. FSJoin will now use whatever already exists in the path. app.GetPath now creates a DirFS from the containing directory instead of app.systemFS, which fixes loading on windows. --- config.go | 4 +++- embed/external.go | 15 ++++++++++++++- main.go | 2 -- package-lock.json | 6 +++--- package.json | 2 +- 5 files changed, 21 insertions(+), 8 deletions(-) diff --git a/config.go b/config.go index 7ccc36b..0317f40 100644 --- a/config.go +++ b/config.go @@ -3,6 +3,7 @@ package main import ( "fmt" "io/fs" + "os" "path/filepath" "strconv" "strings" @@ -17,7 +18,8 @@ func (app *appContext) GetPath(sect, key string) (fs.FS, string) { if strings.HasPrefix(val, "jfa-go:") { return localFS, strings.TrimPrefix(val, "jfa-go:") } - return app.systemFS, strings.TrimPrefix(val, "/") + dir, file := filepath.Split(val) + return os.DirFS(dir), file } func (app *appContext) loadConfig() error { diff --git a/embed/external.go b/embed/external.go index 611f4b0..f599a4e 100644 --- a/embed/external.go +++ b/embed/external.go @@ -5,12 +5,25 @@ import ( "log" "os" "path/filepath" + "strings" ) var localFS fs.FS var langFS fs.FS -func FSJoin(elem ...string) string { return filepath.Join(elem...) } +// When using os.DirFS, even on Windows the separator seems to be '/'. +// func FSJoin(elem ...string) string { return filepath.Join(elem...) } +func FSJoin(elem ...string) string { + sep := "/" + if strings.Contains(elem[0], "\\") { + sep = "\\" + } + path := "" + for _, el := range elem { + path += el + sep + } + return strings.TrimSuffix(path, sep) +} func loadFilesystems() { log.Println("Using external storage") diff --git a/main.go b/main.go index 5c62827..8801a51 100644 --- a/main.go +++ b/main.go @@ -65,7 +65,6 @@ type appContext struct { configBasePath string configBase settings dataPath string - systemFS fs.FS webFS httpFS cssClass string jellyfinLogin bool @@ -141,7 +140,6 @@ func start(asDaemon, firstCall bool) { userConfigDir, _ := os.UserConfigDir() app.dataPath = filepath.Join(userConfigDir, "jfa-go") app.configPath = filepath.Join(app.dataPath, "config.ini") - app.systemFS = os.DirFS("/") // gin-static doesn't just take a plain http.FileSystem, so we implement it's ServeFileSystem. See static.go. app.webFS = httpFS{ hfs: http.FS(localFS), diff --git a/package-lock.json b/package-lock.json index 5b9e0a0..082c759 100644 --- a/package-lock.json +++ b/package-lock.json @@ -228,9 +228,9 @@ "integrity": "sha1-vfpzUplmTfr9NFKe1PhSKidf6lY=" }, "esbuild": { - "version": "0.8.46", - "resolved": "https://registry.npm.taobao.org/esbuild/download/esbuild-0.8.46.tgz", - "integrity": "sha1-j8cjDOMBmxLiVTOZ8MA4dacpwms=" + "version": "0.8.47", + "resolved": "https://registry.npm.taobao.org/esbuild/download/esbuild-0.8.47.tgz", + "integrity": "sha1-XVxZt9y4og3632WpheXlynsk7/I=" }, "escalade": { "version": "3.1.1", diff --git a/package.json b/package.json index b9425a4..d1ca228 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "homepage": "https://github.com/hrfee/jfa-go#readme", "dependencies": { "a17t": "^0.4.0", - "esbuild": "^0.8.46", + "esbuild": "^0.8.47", "lodash": "^4.17.19", "mjml": "^4.8.0", "remixicon": "^2.5.0",