diff --git a/server/common/config_state.go b/server/common/config_state.go index c9ac8b4a..442e7fe8 100644 --- a/server/common/config_state.go +++ b/server/common/config_state.go @@ -19,11 +19,10 @@ import ( "github.com/tidwall/sjson" "io/ioutil" "os" - "path/filepath" ) var ( - configPath string = filepath.Join(GetCurrentDir(), CONFIG_PATH+"config.json") + configPath string = GetAbsolutePath(CONFIG_PATH, "config.json") configKeysToEncrypt []string = []string{ "middleware.identity_provider.params", "middleware.attribute_mapping.params", @@ -34,7 +33,7 @@ func LoadConfig() ([]byte, error) { file, err := os.OpenFile(configPath, os.O_RDONLY, os.ModePerm) if err != nil { if os.IsNotExist(err) { - os.MkdirAll(filepath.Join(GetCurrentDir(), CONFIG_PATH), os.ModePerm) + os.MkdirAll(GetAbsolutePath(CONFIG_PATH), os.ModePerm) return []byte(""), nil } return nil, err diff --git a/server/common/files.go b/server/common/files.go index d93fcca7..10b3f20a 100644 --- a/server/common/files.go +++ b/server/common/files.go @@ -20,8 +20,15 @@ func GetCurrentDir() string { return filepath.Dir(ex) } -func GetAbsolutePath(p string) string { - return filepath.Join(GetCurrentDir(), p) +func GetAbsolutePath(base string, opts ...string) string { + fullPath := base + if strings.HasPrefix(base, "/") == false { // relative filepath are relative to the binary + fullPath = filepath.Join(GetCurrentDir(), base) + } + if len(opts) == 0 { + return fullPath + } + return filepath.Join(append([]string{fullPath}, opts...)...) } func IsDirectory(path string) bool { diff --git a/server/common/log.go b/server/common/log.go index a9a84686..b08c73f9 100644 --- a/server/common/log.go +++ b/server/common/log.go @@ -4,7 +4,6 @@ import ( "fmt" slog "log" "os" - "path/filepath" "strings" "time" ) @@ -18,8 +17,8 @@ var logfile *os.File func init() { var err error - logPath := filepath.Join(GetCurrentDir(), LOG_PATH) - logfile, err = os.OpenFile(filepath.Join(logPath, "access.log"), os.O_APPEND|os.O_WRONLY|os.O_CREATE, os.ModePerm) + logPath := GetAbsolutePath(LOG_PATH) + logfile, err = os.OpenFile(GetAbsolutePath(logPath, "access.log"), os.O_APPEND|os.O_WRONLY|os.O_CREATE, os.ModePerm) if err != nil { slog.Printf("ERROR log file: %+v", err) return diff --git a/server/common/ssl/index.go b/server/common/ssl/index.go index 6977f787..382b8a43 100644 --- a/server/common/ssl/index.go +++ b/server/common/ssl/index.go @@ -3,14 +3,13 @@ package ssl import ( . "github.com/mickael-kerjean/filestash/server/common" "os" - "path/filepath" ) -var keyPEMPath string = filepath.Join(GetCurrentDir(), CERT_PATH, "key.pem") -var certPEMPath string = filepath.Join(GetCurrentDir(), CERT_PATH, "cert.pem") +var keyPEMPath string = GetAbsolutePath(CERT_PATH, "key.pem") +var certPEMPath string = GetAbsolutePath(CERT_PATH, "cert.pem") func init() { - os.MkdirAll(filepath.Join(GetCurrentDir(), CERT_PATH), os.ModePerm) + os.MkdirAll(GetAbsolutePath(CERT_PATH), os.ModePerm) } func Clear() { diff --git a/server/ctrl/admin.go b/server/ctrl/admin.go index c6d2c7a6..ed266130 100644 --- a/server/ctrl/admin.go +++ b/server/ctrl/admin.go @@ -8,12 +8,11 @@ import ( "io/ioutil" "net/http" "os" - "path/filepath" "strconv" "time" ) -var logpath = filepath.Join(GetCurrentDir(), LOG_PATH, "access.log") +var logpath = GetAbsolutePath(LOG_PATH, "access.log") func AdminSessionGet(ctx *App, res http.ResponseWriter, req *http.Request) { if admin := Config.Get("auth.admin").String(); admin == "" { diff --git a/server/ctrl/config.go b/server/ctrl/config.go index cd7f99b5..091da4c6 100644 --- a/server/ctrl/config.go +++ b/server/ctrl/config.go @@ -4,10 +4,9 @@ import ( . "github.com/mickael-kerjean/filestash/server/common" "io/ioutil" "net/http" - "path/filepath" ) -var configpath = filepath.Join(GetCurrentDir(), CONFIG_PATH, "config.json") +var configpath = GetAbsolutePath(CONFIG_PATH, "config.json") func PrivateConfigHandler(ctx *App, res http.ResponseWriter, req *http.Request) { SendSuccessResult(res, &Config) diff --git a/server/ctrl/files.go b/server/ctrl/files.go index 77bb1aa8..a16048fd 100644 --- a/server/ctrl/files.go +++ b/server/ctrl/files.go @@ -31,7 +31,7 @@ var ( func init() { FileCache = NewAppCache() - cachePath := filepath.Join(GetCurrentDir(), TMP_PATH) + cachePath := GetAbsolutePath(TMP_PATH) FileCache.OnEvict(func(key string, value interface{}) { os.RemoveAll(filepath.Join(cachePath, key)) }) @@ -235,7 +235,7 @@ func FileCat(ctx *App, res http.ResponseWriter, req *http.Request) { } } } else { - tmpPath := filepath.Join(GetCurrentDir(), TMP_PATH, "file_"+QuickString(20)+".dat") + tmpPath := GetAbsolutePath(TMP_PATH, "file_"+QuickString(20)+".dat") f, err := os.OpenFile(tmpPath, os.O_RDWR|os.O_CREATE, os.ModePerm) if err != nil { Log.Debug("cat::range0 '%s'", err.Error()) diff --git a/server/ctrl/report.go b/server/ctrl/report.go index eb0d3de2..aeeb6114 100644 --- a/server/ctrl/report.go +++ b/server/ctrl/report.go @@ -5,7 +5,6 @@ import ( . "github.com/mickael-kerjean/filestash/server/common" "net/http" "os" - "path/filepath" ) func ReportHandler(ctx *App, res http.ResponseWriter, req *http.Request) { @@ -24,7 +23,7 @@ func HealthHandler(ctx *App, res http.ResponseWriter, req *http.Request) { res.Header().Set("Access-Control-Allow-Origin", "*") // CHECK 1: open the config file file, err := os.OpenFile( - filepath.Join(GetCurrentDir(), CONFIG_PATH, "config.json"), + GetAbsolutePath(CONFIG_PATH, "config.json"), os.O_RDWR, os.ModePerm, ) if err != nil { diff --git a/server/ctrl/static.go b/server/ctrl/static.go index 8ce0dce4..60439d88 100644 --- a/server/ctrl/static.go +++ b/server/ctrl/static.go @@ -9,7 +9,6 @@ import ( "net/http" URL "net/url" "os" - "path/filepath" "regexp" "strings" "text/template" @@ -155,8 +154,8 @@ func AboutHandler(ctx *App, res http.ResponseWriter, req *http.Request) { Version: fmt.Sprintf("Filestash %s.%s", APP_VERSION, BUILD_DATE), CommitHash: BUILD_REF, Checksum: []string{ - hashFileContent(filepath.Join(GetCurrentDir(), "/filestash"), 0), - hashFileContent(filepath.Join(GetCurrentDir(), CONFIG_PATH, "config.json"), 0), + hashFileContent(GetAbsolutePath("filestash"), 0), + hashFileContent(GetAbsolutePath(CONFIG_PATH, "config.json"), 0), }, License: strings.ToUpper(LICENSE), Plugins: []string{ diff --git a/server/model/index.go b/server/model/index.go index 2e36805a..b85eacfa 100644 --- a/server/model/index.go +++ b/server/model/index.go @@ -5,14 +5,13 @@ import ( . "github.com/mickael-kerjean/filestash/server/common" _ "modernc.org/sqlite" "os" - "path/filepath" "time" ) var DB *sql.DB func init() { - cachePath := filepath.Join(GetCurrentDir(), DB_PATH) + cachePath := GetAbsolutePath(DB_PATH) os.MkdirAll(cachePath, os.ModePerm) var err error if DB, err = sql.Open("sqlite", cachePath+"/share.sql?_fk=true"); err != nil { diff --git a/server/model/webdav.go b/server/model/webdav.go index e0ddac01..2bfdeabb 100644 --- a/server/model/webdav.go +++ b/server/model/webdav.go @@ -28,7 +28,7 @@ var ( ) func init() { - cachePath = filepath.Join(GetCurrentDir(), DAVCachePath) + "/" + cachePath = GetAbsolutePath(DAVCachePath) + "/" os.RemoveAll(cachePath) os.MkdirAll(cachePath, os.ModePerm) diff --git a/server/plugin/plg_backend_backblaze/index.go b/server/plugin/plg_backend_backblaze/index.go index cc99b033..40ce1eb9 100644 --- a/server/plugin/plg_backend_backblaze/index.go +++ b/server/plugin/plg_backend_backblaze/index.go @@ -12,7 +12,6 @@ import ( "net/http" "net/url" "os" - "path/filepath" "strconv" "strings" "time" @@ -42,7 +41,7 @@ type BackblazeError struct { func init() { Backend.Register("backblaze", Backblaze{}) BackblazeCache = NewAppCache() - cachePath := filepath.Join(GetCurrentDir(), BackblazeCachePath) + cachePath := GetAbsolutePath(BackblazeCachePath) os.RemoveAll(cachePath) os.MkdirAll(cachePath, os.ModePerm) } diff --git a/server/plugin/plg_backend_git/index.go b/server/plugin/plg_backend_git/index.go index 6bfdb8a7..572694a4 100644 --- a/server/plugin/plg_backend_git/index.go +++ b/server/plugin/plg_backend_git/index.go @@ -29,7 +29,7 @@ func init() { Backend.Register("git", Git{}) GitCache = NewAppCache() - cachePath := filepath.Join(GetCurrentDir(), GitCachePath) + cachePath := GetAbsolutePath(GitCachePath) os.RemoveAll(cachePath) os.MkdirAll(cachePath, os.ModePerm) GitCache.OnEvict(func(key string, value interface{}) { diff --git a/server/plugin/plg_image_light/index.go b/server/plugin/plg_image_light/index.go index 71af0608..dd90a061 100644 --- a/server/plugin/plg_image_light/index.go +++ b/server/plugin/plg_image_light/index.go @@ -6,7 +6,6 @@ import ( "io" "net/http" "os" - "path/filepath" "strconv" "strings" ) @@ -108,7 +107,7 @@ func init() { } image_caching() - cachePath := filepath.Join(GetCurrentDir(), ImageCachePath) + cachePath := GetAbsolutePath(ImageCachePath) os.RemoveAll(cachePath) os.MkdirAll(cachePath, os.ModePerm) diff --git a/server/plugin/plg_search_sqlitefts/spider.go b/server/plugin/plg_search_sqlitefts/spider.go index 6a7de285..c7ccca42 100644 --- a/server/plugin/plg_search_sqlitefts/spider.go +++ b/server/plugin/plg_search_sqlitefts/spider.go @@ -30,7 +30,7 @@ type SearchIndexer struct { func NewSearchIndexer(id string, b IBackend) SearchIndexer { s := SearchIndexer{ - DBPath: filepath.Join(GetCurrentDir(), FTS_PATH, "fts_"+id+".sql"), + DBPath: GetAbsolutePath(FTS_PATH, "fts_"+id+".sql"), Id: id, Backend: b, FoldersUnknown: make(HeapDoc, 0, 1), diff --git a/server/plugin/plg_starter_http2/index.go b/server/plugin/plg_starter_http2/index.go index eee7434a..3c9c0347 100644 --- a/server/plugin/plg_starter_http2/index.go +++ b/server/plugin/plg_starter_http2/index.go @@ -19,7 +19,7 @@ import ( "time" ) -var SSL_PATH string = filepath.Join(GetCurrentDir(), CERT_PATH, "ssl") +var SSL_PATH string = GetAbsolutePath(CERT_PATH, "ssl") func init() { os.MkdirAll(SSL_PATH, os.ModePerm) diff --git a/server/plugin/plg_starter_tor/index.go b/server/plugin/plg_starter_tor/index.go index 6b6e409c..b1193a74 100644 --- a/server/plugin/plg_starter_tor/index.go +++ b/server/plugin/plg_starter_tor/index.go @@ -11,7 +11,7 @@ import ( "time" ) -var TOR_PATH string = filepath.Join(GetCurrentDir(), CERT_PATH, "tor") +var TOR_PATH string = GetAbsolutePath(CERT_PATH, "tor") func init() { os.MkdirAll(TOR_PATH, os.ModePerm) diff --git a/server/plugin/plg_video_transcoder/index.go b/server/plugin/plg_video_transcoder/index.go index b142b0c4..d04d88cf 100644 --- a/server/plugin/plg_video_transcoder/index.go +++ b/server/plugin/plg_video_transcoder/index.go @@ -12,7 +12,6 @@ import ( "net/http" "os" "os/exec" - "path/filepath" "strconv" "strings" "time" @@ -78,7 +77,7 @@ func init() { return } - cachePath := filepath.Join(GetCurrentDir(), VideoCachePath) + cachePath := GetAbsolutePath(VideoCachePath) os.RemoveAll(cachePath) os.MkdirAll(cachePath, os.ModePerm) @@ -124,8 +123,7 @@ func hls_playlist(reader io.ReadCloser, ctx *App, res *http.ResponseWriter, req } cacheName := "vid_" + GenerateID(ctx) + "_" + QuickHash(path, 10) + ".dat" - cachePath := filepath.Join( - GetCurrentDir(), + cachePath := GetAbsolutePath( VideoCachePath, cacheName, ) @@ -172,8 +170,7 @@ func hls_transcode(ctx *App, res http.ResponseWriter, req *http.Request) { return } startTime := segmentNumber * HLS_SEGMENT_LENGTH - cachePath := filepath.Join( - GetCurrentDir(), + cachePath := GetAbsolutePath( VideoCachePath, req.URL.Query().Get("path"), )