From 5d251c365908df8497b7deb66081dd9cf94d04ce Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Sun, 19 Jan 2025 08:59:36 +0100 Subject: [PATCH] Make all files and directories in the config absolute --- cmd/backend/config.go | 32 ++++++++++++++++++++++++++++++++ cmd/calls/atom.go | 11 +---------- cmd/calls/images.go | 10 +--------- cmd/frontend/docx.go | 26 ++++++-------------------- cmd/frontend/pdf.go | 12 +++--------- 5 files changed, 43 insertions(+), 48 deletions(-) diff --git a/cmd/backend/config.go b/cmd/backend/config.go index 5fcb0f3..26a4218 100644 --- a/cmd/backend/config.go +++ b/cmd/backend/config.go @@ -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) diff --git a/cmd/calls/atom.go b/cmd/calls/atom.go index 6110d4f..d205795 100644 --- a/cmd/calls/atom.go +++ b/cmd/calls/atom.go @@ -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) } } diff --git a/cmd/calls/images.go b/cmd/calls/images.go index 7dae6a4..e8edab2 100644 --- a/cmd/calls/images.go +++ b/cmd/calls/images.go @@ -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"))) } } diff --git a/cmd/frontend/docx.go b/cmd/frontend/docx.go index 9cf4712..98dbf48 100644 --- a/cmd/frontend/docx.go +++ b/cmd/frontend/docx.go @@ -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 diff --git a/cmd/frontend/pdf.go b/cmd/frontend/pdf.go index 5e4c399..3648d03 100644 --- a/cmd/frontend/pdf.go +++ b/cmd/frontend/pdf.go @@ -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