maintain (path): absolute path

getting things ready to have config coming as a plugin to handle
various distributions
This commit is contained in:
Mickael Kerjean
2023-03-02 20:15:56 +11:00
parent 8059730720
commit 8ca7a0e3f9
18 changed files with 34 additions and 40 deletions

View File

@ -19,11 +19,10 @@ import (
"github.com/tidwall/sjson" "github.com/tidwall/sjson"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath"
) )
var ( var (
configPath string = filepath.Join(GetCurrentDir(), CONFIG_PATH+"config.json") configPath string = GetAbsolutePath(CONFIG_PATH, "config.json")
configKeysToEncrypt []string = []string{ configKeysToEncrypt []string = []string{
"middleware.identity_provider.params", "middleware.identity_provider.params",
"middleware.attribute_mapping.params", "middleware.attribute_mapping.params",
@ -34,7 +33,7 @@ func LoadConfig() ([]byte, error) {
file, err := os.OpenFile(configPath, os.O_RDONLY, os.ModePerm) file, err := os.OpenFile(configPath, os.O_RDONLY, os.ModePerm)
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
os.MkdirAll(filepath.Join(GetCurrentDir(), CONFIG_PATH), os.ModePerm) os.MkdirAll(GetAbsolutePath(CONFIG_PATH), os.ModePerm)
return []byte(""), nil return []byte(""), nil
} }
return nil, err return nil, err

View File

@ -20,8 +20,15 @@ func GetCurrentDir() string {
return filepath.Dir(ex) return filepath.Dir(ex)
} }
func GetAbsolutePath(p string) string { func GetAbsolutePath(base string, opts ...string) string {
return filepath.Join(GetCurrentDir(), p) 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 { func IsDirectory(path string) bool {

View File

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
slog "log" slog "log"
"os" "os"
"path/filepath"
"strings" "strings"
"time" "time"
) )
@ -18,8 +17,8 @@ var logfile *os.File
func init() { func init() {
var err error var err error
logPath := filepath.Join(GetCurrentDir(), LOG_PATH) logPath := GetAbsolutePath(LOG_PATH)
logfile, err = os.OpenFile(filepath.Join(logPath, "access.log"), os.O_APPEND|os.O_WRONLY|os.O_CREATE, os.ModePerm) logfile, err = os.OpenFile(GetAbsolutePath(logPath, "access.log"), os.O_APPEND|os.O_WRONLY|os.O_CREATE, os.ModePerm)
if err != nil { if err != nil {
slog.Printf("ERROR log file: %+v", err) slog.Printf("ERROR log file: %+v", err)
return return

View File

@ -3,14 +3,13 @@ package ssl
import ( import (
. "github.com/mickael-kerjean/filestash/server/common" . "github.com/mickael-kerjean/filestash/server/common"
"os" "os"
"path/filepath"
) )
var keyPEMPath string = filepath.Join(GetCurrentDir(), CERT_PATH, "key.pem") var keyPEMPath string = GetAbsolutePath(CERT_PATH, "key.pem")
var certPEMPath string = filepath.Join(GetCurrentDir(), CERT_PATH, "cert.pem") var certPEMPath string = GetAbsolutePath(CERT_PATH, "cert.pem")
func init() { func init() {
os.MkdirAll(filepath.Join(GetCurrentDir(), CERT_PATH), os.ModePerm) os.MkdirAll(GetAbsolutePath(CERT_PATH), os.ModePerm)
} }
func Clear() { func Clear() {

View File

@ -8,12 +8,11 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
"path/filepath"
"strconv" "strconv"
"time" "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) { func AdminSessionGet(ctx *App, res http.ResponseWriter, req *http.Request) {
if admin := Config.Get("auth.admin").String(); admin == "" { if admin := Config.Get("auth.admin").String(); admin == "" {

View File

@ -4,10 +4,9 @@ import (
. "github.com/mickael-kerjean/filestash/server/common" . "github.com/mickael-kerjean/filestash/server/common"
"io/ioutil" "io/ioutil"
"net/http" "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) { func PrivateConfigHandler(ctx *App, res http.ResponseWriter, req *http.Request) {
SendSuccessResult(res, &Config) SendSuccessResult(res, &Config)

View File

@ -31,7 +31,7 @@ var (
func init() { func init() {
FileCache = NewAppCache() FileCache = NewAppCache()
cachePath := filepath.Join(GetCurrentDir(), TMP_PATH) cachePath := GetAbsolutePath(TMP_PATH)
FileCache.OnEvict(func(key string, value interface{}) { FileCache.OnEvict(func(key string, value interface{}) {
os.RemoveAll(filepath.Join(cachePath, key)) os.RemoveAll(filepath.Join(cachePath, key))
}) })
@ -235,7 +235,7 @@ func FileCat(ctx *App, res http.ResponseWriter, req *http.Request) {
} }
} }
} else { } 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) f, err := os.OpenFile(tmpPath, os.O_RDWR|os.O_CREATE, os.ModePerm)
if err != nil { if err != nil {
Log.Debug("cat::range0 '%s'", err.Error()) Log.Debug("cat::range0 '%s'", err.Error())

View File

@ -5,7 +5,6 @@ import (
. "github.com/mickael-kerjean/filestash/server/common" . "github.com/mickael-kerjean/filestash/server/common"
"net/http" "net/http"
"os" "os"
"path/filepath"
) )
func ReportHandler(ctx *App, res http.ResponseWriter, req *http.Request) { 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", "*") res.Header().Set("Access-Control-Allow-Origin", "*")
// CHECK 1: open the config file // CHECK 1: open the config file
file, err := os.OpenFile( file, err := os.OpenFile(
filepath.Join(GetCurrentDir(), CONFIG_PATH, "config.json"), GetAbsolutePath(CONFIG_PATH, "config.json"),
os.O_RDWR, os.ModePerm, os.O_RDWR, os.ModePerm,
) )
if err != nil { if err != nil {

View File

@ -9,7 +9,6 @@ import (
"net/http" "net/http"
URL "net/url" URL "net/url"
"os" "os"
"path/filepath"
"regexp" "regexp"
"strings" "strings"
"text/template" "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), Version: fmt.Sprintf("Filestash %s.%s", APP_VERSION, BUILD_DATE),
CommitHash: BUILD_REF, CommitHash: BUILD_REF,
Checksum: []string{ Checksum: []string{
hashFileContent(filepath.Join(GetCurrentDir(), "/filestash"), 0), hashFileContent(GetAbsolutePath("filestash"), 0),
hashFileContent(filepath.Join(GetCurrentDir(), CONFIG_PATH, "config.json"), 0), hashFileContent(GetAbsolutePath(CONFIG_PATH, "config.json"), 0),
}, },
License: strings.ToUpper(LICENSE), License: strings.ToUpper(LICENSE),
Plugins: []string{ Plugins: []string{

View File

@ -5,14 +5,13 @@ import (
. "github.com/mickael-kerjean/filestash/server/common" . "github.com/mickael-kerjean/filestash/server/common"
_ "modernc.org/sqlite" _ "modernc.org/sqlite"
"os" "os"
"path/filepath"
"time" "time"
) )
var DB *sql.DB var DB *sql.DB
func init() { func init() {
cachePath := filepath.Join(GetCurrentDir(), DB_PATH) cachePath := GetAbsolutePath(DB_PATH)
os.MkdirAll(cachePath, os.ModePerm) os.MkdirAll(cachePath, os.ModePerm)
var err error var err error
if DB, err = sql.Open("sqlite", cachePath+"/share.sql?_fk=true"); err != nil { if DB, err = sql.Open("sqlite", cachePath+"/share.sql?_fk=true"); err != nil {

View File

@ -28,7 +28,7 @@ var (
) )
func init() { func init() {
cachePath = filepath.Join(GetCurrentDir(), DAVCachePath) + "/" cachePath = GetAbsolutePath(DAVCachePath) + "/"
os.RemoveAll(cachePath) os.RemoveAll(cachePath)
os.MkdirAll(cachePath, os.ModePerm) os.MkdirAll(cachePath, os.ModePerm)

View File

@ -12,7 +12,6 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"path/filepath"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -42,7 +41,7 @@ type BackblazeError struct {
func init() { func init() {
Backend.Register("backblaze", Backblaze{}) Backend.Register("backblaze", Backblaze{})
BackblazeCache = NewAppCache() BackblazeCache = NewAppCache()
cachePath := filepath.Join(GetCurrentDir(), BackblazeCachePath) cachePath := GetAbsolutePath(BackblazeCachePath)
os.RemoveAll(cachePath) os.RemoveAll(cachePath)
os.MkdirAll(cachePath, os.ModePerm) os.MkdirAll(cachePath, os.ModePerm)
} }

View File

@ -29,7 +29,7 @@ func init() {
Backend.Register("git", Git{}) Backend.Register("git", Git{})
GitCache = NewAppCache() GitCache = NewAppCache()
cachePath := filepath.Join(GetCurrentDir(), GitCachePath) cachePath := GetAbsolutePath(GitCachePath)
os.RemoveAll(cachePath) os.RemoveAll(cachePath)
os.MkdirAll(cachePath, os.ModePerm) os.MkdirAll(cachePath, os.ModePerm)
GitCache.OnEvict(func(key string, value interface{}) { GitCache.OnEvict(func(key string, value interface{}) {

View File

@ -6,7 +6,6 @@ import (
"io" "io"
"net/http" "net/http"
"os" "os"
"path/filepath"
"strconv" "strconv"
"strings" "strings"
) )
@ -108,7 +107,7 @@ func init() {
} }
image_caching() image_caching()
cachePath := filepath.Join(GetCurrentDir(), ImageCachePath) cachePath := GetAbsolutePath(ImageCachePath)
os.RemoveAll(cachePath) os.RemoveAll(cachePath)
os.MkdirAll(cachePath, os.ModePerm) os.MkdirAll(cachePath, os.ModePerm)

View File

@ -30,7 +30,7 @@ type SearchIndexer struct {
func NewSearchIndexer(id string, b IBackend) SearchIndexer { func NewSearchIndexer(id string, b IBackend) SearchIndexer {
s := SearchIndexer{ s := SearchIndexer{
DBPath: filepath.Join(GetCurrentDir(), FTS_PATH, "fts_"+id+".sql"), DBPath: GetAbsolutePath(FTS_PATH, "fts_"+id+".sql"),
Id: id, Id: id,
Backend: b, Backend: b,
FoldersUnknown: make(HeapDoc, 0, 1), FoldersUnknown: make(HeapDoc, 0, 1),

View File

@ -19,7 +19,7 @@ import (
"time" "time"
) )
var SSL_PATH string = filepath.Join(GetCurrentDir(), CERT_PATH, "ssl") var SSL_PATH string = GetAbsolutePath(CERT_PATH, "ssl")
func init() { func init() {
os.MkdirAll(SSL_PATH, os.ModePerm) os.MkdirAll(SSL_PATH, os.ModePerm)

View File

@ -11,7 +11,7 @@ import (
"time" "time"
) )
var TOR_PATH string = filepath.Join(GetCurrentDir(), CERT_PATH, "tor") var TOR_PATH string = GetAbsolutePath(CERT_PATH, "tor")
func init() { func init() {
os.MkdirAll(TOR_PATH, os.ModePerm) os.MkdirAll(TOR_PATH, os.ModePerm)

View File

@ -12,7 +12,6 @@ import (
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
"path/filepath"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -78,7 +77,7 @@ func init() {
return return
} }
cachePath := filepath.Join(GetCurrentDir(), VideoCachePath) cachePath := GetAbsolutePath(VideoCachePath)
os.RemoveAll(cachePath) os.RemoveAll(cachePath)
os.MkdirAll(cachePath, os.ModePerm) 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" cacheName := "vid_" + GenerateID(ctx) + "_" + QuickHash(path, 10) + ".dat"
cachePath := filepath.Join( cachePath := GetAbsolutePath(
GetCurrentDir(),
VideoCachePath, VideoCachePath,
cacheName, cacheName,
) )
@ -172,8 +170,7 @@ func hls_transcode(ctx *App, res http.ResponseWriter, req *http.Request) {
return return
} }
startTime := segmentNumber * HLS_SEGMENT_LENGTH startTime := segmentNumber * HLS_SEGMENT_LENGTH
cachePath := filepath.Join( cachePath := GetAbsolutePath(
GetCurrentDir(),
VideoCachePath, VideoCachePath,
req.URL.Query().Get("path"), req.URL.Query().Get("path"),
) )