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"
"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

View File

@ -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 {

View File

@ -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

View File

@ -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() {

View File

@ -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 == "" {

View File

@ -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)

View File

@ -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())

View File

@ -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 {

View File

@ -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{

View File

@ -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 {

View File

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

View File

@ -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)
}

View File

@ -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{}) {

View File

@ -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)

View File

@ -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),

View File

@ -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)

View File

@ -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)

View File

@ -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"),
)