Make all files and directories in the config absolute

This commit is contained in:
Jason Streifling 2025-01-19 08:59:36 +01:00
parent 5552e6d2eb
commit 5d251c3659
5 changed files with 43 additions and 48 deletions

View File

@ -158,6 +158,10 @@ func (c *Config) setupConfig(cliConfig *Config) error {
if cliConfig.AESKeyFile != defaultConfig.AESKeyFile {
c.AESKeyFile = cliConfig.AESKeyFile
}
c.AESKeyFile, err = filepath.Abs(c.AESKeyFile)
if err != nil {
return fmt.Errorf("error setting absolute filepath for AESKeyFile: %v", err)
}
c.AESKeyFile, err = mkFile(c.AESKeyFile, 0600, 0700)
if err != nil {
return fmt.Errorf("error setting up file: %v", err)
@ -166,6 +170,10 @@ func (c *Config) setupConfig(cliConfig *Config) error {
if cliConfig.ArticleDir != defaultConfig.ArticleDir {
c.ArticleDir = cliConfig.ArticleDir
}
c.ArticleDir, err = filepath.Abs(c.ArticleDir)
if err != nil {
return fmt.Errorf("error setting absolute filepath for ArticleDir: %v", err)
}
c.ArticleDir, err = mkDir(c.ArticleDir, 0700)
if err != nil {
return fmt.Errorf("error setting up directory: %v", err)
@ -174,6 +182,10 @@ func (c *Config) setupConfig(cliConfig *Config) error {
if cliConfig.AtomFile != defaultConfig.AtomFile {
c.AtomFile = cliConfig.AtomFile
}
c.AtomFile, err = filepath.Abs(c.AtomFile)
if err != nil {
return fmt.Errorf("error setting absolute filepath for AtomFile: %v", err)
}
c.AtomFile, err = mkFile(c.AtomFile, 0644, 0744)
if err != nil {
return fmt.Errorf("error setting up file: %v", err)
@ -202,6 +214,10 @@ func (c *Config) setupConfig(cliConfig *Config) error {
if cliConfig.FirebaseKey != defaultConfig.FirebaseKey {
c.FirebaseKey = cliConfig.FirebaseKey
}
c.FirebaseKey, err = filepath.Abs(c.FirebaseKey)
if err != nil {
return fmt.Errorf("error setting absolute filepath for FirebaseKey: %v", err)
}
c.FirebaseKey, err = mkFile(c.FirebaseKey, 0600, 0700)
if err != nil {
return fmt.Errorf("error setting up file: %v", err)
@ -214,6 +230,10 @@ func (c *Config) setupConfig(cliConfig *Config) error {
if cliConfig.LogFile != defaultConfig.LogFile {
c.LogFile = cliConfig.LogFile
}
c.LogFile, err = filepath.Abs(c.LogFile)
if err != nil {
return fmt.Errorf("error setting absolute filepath for LogFile: %v", err)
}
c.LogFile, err = mkFile(c.LogFile, 0600, 0700)
if err != nil {
return fmt.Errorf("error setting up file: %v", err)
@ -238,6 +258,10 @@ func (c *Config) setupConfig(cliConfig *Config) error {
if cliConfig.PDFDir != defaultConfig.PDFDir {
c.PDFDir = cliConfig.PDFDir
}
c.PDFDir, err = filepath.Abs(c.PDFDir)
if err != nil {
return fmt.Errorf("error setting absolute filepath for PDFDir: %v", err)
}
c.PDFDir, err = mkDir(c.PDFDir, 0700)
if err != nil {
return fmt.Errorf("error setting up directory: %v", err)
@ -246,6 +270,10 @@ func (c *Config) setupConfig(cliConfig *Config) error {
if cliConfig.PicsDir != defaultConfig.PicsDir {
c.PicsDir = cliConfig.PicsDir
}
c.PicsDir, err = filepath.Abs(c.PicsDir)
if err != nil {
return fmt.Errorf("error setting absolute filepath for PicsDir: %v", err)
}
c.PicsDir, err = mkDir(c.PicsDir, 0700)
if err != nil {
return fmt.Errorf("error setting up directory: %v", err)
@ -262,6 +290,10 @@ func (c *Config) setupConfig(cliConfig *Config) error {
if cliConfig.WebDir != defaultConfig.WebDir {
c.WebDir = cliConfig.WebDir
}
c.WebDir, err = filepath.Abs(c.WebDir)
if err != nil {
return fmt.Errorf("error setting absolute filepath for WebDir: %v", err)
}
c.WebDir, err = mkDir(c.WebDir, 0700)
if err != nil {
return fmt.Errorf("error setting up directory: %v", err)

View File

@ -1,9 +1,7 @@
package calls
import (
"log"
"net/http"
"path/filepath"
b "streifling.com/jason/cpolis/cmd/backend"
)
@ -14,13 +12,6 @@ func ServeAtomFeed(c *b.Config) http.HandlerFunc {
return
}
absFilepath, err := filepath.Abs(c.AtomFile)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.ServeFile(w, r, absFilepath)
http.ServeFile(w, r, c.AtomFile)
}
}

View File

@ -1,7 +1,6 @@
package calls
import (
"log"
"net/http"
"path/filepath"
@ -17,13 +16,6 @@ func ServeImage(c *b.Config, s map[string]*f.Session) http.HandlerFunc {
}
}
absFilepath, err := filepath.Abs(c.PicsDir)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.ServeFile(w, r, absFilepath+"/"+r.PathValue("pic"))
http.ServeFile(w, r, filepath.Join(c.PicsDir, r.PathValue("pic")))
}
}

View File

@ -44,22 +44,15 @@ func UploadDocx(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc {
return
}
docxFilename := fmt.Sprint(uuid.New(), ".docx")
absDocxFilepath, err := filepath.Abs("/tmp/" + docxFilename)
if err != nil {
docxFilepath := filepath.Join(os.TempDir(), fmt.Sprint(uuid.New(), ".docx"))
if err = os.WriteFile(docxFilepath, buf.Bytes(), 0644); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer os.Remove(docxFilepath)
if err = os.WriteFile(absDocxFilepath, buf.Bytes(), 0644); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer os.Remove(absDocxFilepath)
mdString, err := b.ConvertToMarkdown(c, absDocxFilepath)
mdString, err := b.ConvertToMarkdown(c, docxFilepath)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
@ -67,15 +60,8 @@ func UploadDocx(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc {
}
uuidName := uuid.New()
mdFilename := fmt.Sprint(uuidName, ".md")
absMdFilepath, err := filepath.Abs(c.ArticleDir + "/" + mdFilename)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err = os.WriteFile(absMdFilepath, mdString, 0644); err != nil {
mdFilepath := filepath.Join(c.ArticleDir, fmt.Sprint(uuidName, ".md"))
if err = os.WriteFile(mdFilepath, mdString, 0644); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return

View File

@ -44,17 +44,11 @@ func UploadPDF(c *b.Config, s map[string]*Session) http.HandlerFunc {
return
}
oldFilename := header.Filename
oldFilename = strings.Join(strings.Split(oldFilename, ".")[:len(oldFilename)-1], ".")
oldFilename := strings.Join(strings.Split(header.Filename, ".")[:len(header.Filename)-1], ".")
filename := fmt.Sprint(oldFilename, ".", uuid.New(), ".pdf")
absFilepath, err := filepath.Abs(c.PDFDir + "/" + filename)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
filepath := filepath.Join(c.PDFDir, filename)
if err = b.WriteFile(absFilepath, file); err != nil {
if err = b.WriteFile(filepath, file); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return