From b8fd81a86db29fc8dd894a79b9478edc82646649 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Sun, 19 Jan 2025 09:31:19 +0100 Subject: [PATCH] Use proper filepaths --- cmd/backend/config.go | 4 ++-- cmd/frontend/articles.go | 29 +++++++++++++++-------------- cmd/frontend/homepage.go | 13 +++++++------ cmd/frontend/images.go | 3 ++- cmd/frontend/issues.go | 3 ++- cmd/frontend/sessions.go | 7 ++++--- cmd/frontend/tags.go | 5 +++-- cmd/frontend/users.go | 19 ++++++++++--------- 8 files changed, 45 insertions(+), 38 deletions(-) diff --git a/cmd/backend/config.go b/cmd/backend/config.go index 26a4218..a2cdb31 100644 --- a/cmd/backend/config.go +++ b/cmd/backend/config.go @@ -52,7 +52,7 @@ func newConfig() *Config { PDFDir: "/var/www/cpolis/pdfs", PicsDir: "/var/www/cpolis/pics", Port: ":8080", - Version: "v0.15.0", + Version: "v0.15.2", WebDir: "/var/www/cpolis/web", } } @@ -91,7 +91,7 @@ func mkFile(path string, filePerm, dirPerm fs.FileMode) (string, error) { } fileName := stringSlice[len(stringSlice)-1] - file, err := os.Create(dir + "/" + fileName) + file, err := os.Create(filepath.Join(dir, fileName)) if err != nil { return "", fmt.Errorf("error creating %v: %v", fileName, err) } diff --git a/cmd/frontend/articles.go b/cmd/frontend/articles.go index f112600..0f1b6a5 100644 --- a/cmd/frontend/articles.go +++ b/cmd/frontend/articles.go @@ -6,6 +6,7 @@ import ( "log" "net/http" "os" + "path/filepath" "strconv" "strings" "time" @@ -83,7 +84,7 @@ func WriteArticle(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc return } - tmpl, err := template.ParseFiles(c.WebDir + "/templates/editor.html") + tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "editor.html")) if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", data); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) @@ -205,7 +206,7 @@ func SubmitArticle(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFun data := new(struct{ Role int }) data.Role = session.User.Role - tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html") + tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "hub.html")) tmpl = template.Must(tmpl, err) if err = tmpl.ExecuteTemplate(w, "page-content", data); err != nil { log.Println(err) @@ -340,7 +341,7 @@ func ResubmitArticle(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerF data := new(struct{ Role int }) data.Role = session.User.Role - tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html") + tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "hub.html")) tmpl = template.Must(tmpl, err) if err = tmpl.ExecuteTemplate(w, "page-content", data); err != nil { log.Println(err) @@ -384,7 +385,7 @@ func ShowUnpublishedUnrejectedAndPublishedRejectedArticles(c *b.Config, db *b.DB } } - tmpl, err := template.ParseFiles(c.WebDir + "/templates/unpublished-articles.html") + tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "unpublished-articles.html")) if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", articles); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) @@ -420,7 +421,7 @@ func ShowRejectedArticles(c *b.Config, db *b.DB, s map[string]*Session) http.Han } } - tmpl, err := template.ParseFiles(c.WebDir + "/templates/rejected-articles.html") + tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "rejected-articles.html")) tmpl = template.Must(tmpl, err) if err = tmpl.ExecuteTemplate(w, "page-content", data); err != nil { log.Println(err) @@ -524,7 +525,7 @@ func ReviewRejectedArticle(c *b.Config, db *b.DB, s map[string]*Session) http.Ha data.Action = fmt.Sprint("resubmit/", data.Article.ID) - tmpl, err := template.ParseFiles(c.WebDir + "/templates/editor.html") + tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "editor.html")) tmpl = template.Must(tmpl, err) if err = tmpl.ExecuteTemplate(w, "page-content", data); err != nil { log.Println(err) @@ -614,7 +615,7 @@ func PublishArticle(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFu data := new(struct{ Role int }) data.Role = session.User.Role - tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html") + tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "hub.html")) tmpl = template.Must(tmpl, err) if err = tmpl.ExecuteTemplate(w, "page-content", data); err != nil { log.Println(err) @@ -648,7 +649,7 @@ func RejectArticle(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFun data := new(struct{ Role int }) data.Role = session.User.Role - tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html") + tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "hub.html")) tmpl = template.Must(tmpl, err) if err = tmpl.ExecuteTemplate(w, "page-content", data); err != nil { log.Println(err) @@ -672,7 +673,7 @@ func ShowCurrentIssue(c *b.Config, db *b.DB, s map[string]*Session) http.Handler return } - tmpl, err := template.ParseFiles(c.WebDir + "/templates/current-issue.html") + tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "current-issue.html")) if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", articles); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) @@ -707,7 +708,7 @@ func ShowPublishedArticles(c *b.Config, db *b.DB, s map[string]*Session, action } } - tmpl, err := template.ParseFiles(c.WebDir + "/templates/published-articles.html") + tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "published-articles.html")) tmpl = template.Must(tmpl, err) if err = tmpl.ExecuteTemplate(w, "page-content", data); err != nil { log.Println(err) @@ -802,7 +803,7 @@ func ReviewArticle(c *b.Config, db *b.DB, s map[string]*Session, action, title, return } - tmpl, err := template.ParseFiles(c.WebDir + "/templates/review-article.html") + tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "review-article.html")) if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", data); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) @@ -860,7 +861,7 @@ func DeleteArticle(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFun data := new(struct{ Role int }) data.Role = session.User.Role - tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html") + tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "hub.html")) tmpl = template.Must(tmpl, err) if err = tmpl.ExecuteTemplate(w, "page-content", data); err != nil { log.Println(err) @@ -953,7 +954,7 @@ func AllowEditArticle(c *b.Config, db *b.DB, s map[string]*Session) http.Handler data := new(struct{ Role int }) data.Role = session.User.Role - tmpl := template.Must(template.ParseFiles(c.WebDir + "/templates/hub.html")) + tmpl := template.Must(template.ParseFiles(filepath.Join(c.WebDir, "templates", "hub.html"))) if err = tmpl.ExecuteTemplate(w, "page-content", data); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) @@ -1015,7 +1016,7 @@ func EditArticle(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc data.Action = fmt.Sprint("save/", data.Article.ID) - tmpl, err := template.ParseFiles(c.WebDir + "/templates/editor.html") + tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "editor.html")) if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", data); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) diff --git a/cmd/frontend/homepage.go b/cmd/frontend/homepage.go index 3e41905..29e7a46 100644 --- a/cmd/frontend/homepage.go +++ b/cmd/frontend/homepage.go @@ -4,6 +4,7 @@ import ( "html/template" "log" "net/http" + "path/filepath" "time" b "streifling.com/jason/cpolis/cmd/backend" @@ -24,14 +25,14 @@ func HomePage(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc { data.Version = c.Version files := make([]string, 2) - files[0] = c.WebDir + "/templates/index.html" + files[0] = filepath.Join(c.WebDir, "templates", "index.html") if numRows == 0 { data.Role = b.NonExistent data.Title = "Erster Benutzer (Administrator)" data.ButtonText = "Anlegen" data.URL = "/user/add-first" - files[1] = c.WebDir + "/templates/edit-user.html" + files[1] = filepath.Join(c.WebDir, "templates", "edit-user.html") tmpl, err := template.ParseFiles(files...) if err = template.Must(tmpl, err).Execute(w, data); err != nil { log.Println(err) @@ -41,7 +42,7 @@ func HomePage(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc { } else { cookie, err := r.Cookie("cpolis_session") if err != nil { - files[1] = c.WebDir + "/templates/login.html" + files[1] = filepath.Join(c.WebDir, "templates", "login.html") tmpl, err := template.ParseFiles(files...) if err = template.Must(tmpl, err).Execute(w, data); err != nil { log.Println(err) @@ -56,7 +57,7 @@ func HomePage(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc { cookie.Expires = time.Now() http.SetCookie(w, cookie) - files[1] = c.WebDir + "/templates/login.html" + files[1] = filepath.Join(c.WebDir, "templates", "login.html") tmpl, err := template.ParseFiles(files...) if err = template.Must(tmpl, err).Execute(w, data); err != nil { log.Println(err) @@ -67,7 +68,7 @@ func HomePage(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc { } data.Role = session.User.Role - files[1] = c.WebDir + "/templates/hub.html" + files[1] = filepath.Join(c.WebDir, "templates", "hub.html") tmpl, err := template.ParseFiles(files...) if err = template.Must(tmpl, err).Execute(w, data); err != nil { log.Println(err) @@ -89,7 +90,7 @@ func ShowHub(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc { data := new(struct{ Role int }) data.Role = session.User.Role - tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html") + tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "hub.html")) if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", data); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) diff --git a/cmd/frontend/images.go b/cmd/frontend/images.go index 870b46a..fb785f0 100644 --- a/cmd/frontend/images.go +++ b/cmd/frontend/images.go @@ -5,6 +5,7 @@ import ( "html/template" "log" "net/http" + "path/filepath" b "streifling.com/jason/cpolis/cmd/backend" ) @@ -70,7 +71,7 @@ func UploadImage(c *b.Config, s map[string]*Session, fileKey, htmlFile, htmlTemp data := new(struct{ Image string }) data.Image = filename - tmpl, err := template.ParseFiles(c.WebDir + "/templates/" + htmlFile) + tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", htmlFile)) if err = template.Must(tmpl, err).ExecuteTemplate(w, htmlTemplate, data); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) diff --git a/cmd/frontend/issues.go b/cmd/frontend/issues.go index ee663af..7c41f75 100644 --- a/cmd/frontend/issues.go +++ b/cmd/frontend/issues.go @@ -6,6 +6,7 @@ import ( "log" "net/http" "os" + "path/filepath" "time" "github.com/google/uuid" @@ -91,7 +92,7 @@ func PublishLatestIssue(c *b.Config, db *b.DB, s map[string]*Session) http.Handl data := new(struct{ Role int }) data.Role = session.User.Role - tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html") + tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "hub.html")) tmpl = template.Must(tmpl, err) if err = tmpl.ExecuteTemplate(w, "page-content", data); err != nil { log.Println(err) diff --git a/cmd/frontend/sessions.go b/cmd/frontend/sessions.go index 4b1dcc8..2a4b59d 100644 --- a/cmd/frontend/sessions.go +++ b/cmd/frontend/sessions.go @@ -7,6 +7,7 @@ import ( "html/template" "log" "net/http" + "path/filepath" "time" "github.com/google/uuid" @@ -67,7 +68,7 @@ func StartSessions() (map[string]*Session, chan string) { // their session and an error. It also handles cases where the user is not // logged in. func ManageSession(w http.ResponseWriter, r *http.Request, c *b.Config, s map[string]*Session) (*Session, error) { - tmpl, tmplErr := template.ParseFiles(c.WebDir+"/templates/index.html", c.WebDir+"/templates/login.html") + tmpl, tmplErr := template.ParseFiles(filepath.Join(c.WebDir, "templates", "index.html"), filepath.Join(c.WebDir, "templates", "login.html")) cookie, err := r.Cookie("cpolis_session") if err != nil { @@ -124,7 +125,7 @@ func Login(c *b.Config, db *b.DB, s map[string]*Session, sessionExpiryChan chan s[session.cookie.Value] = session http.SetCookie(w, session.cookie) - tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html") + tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "hub.html")) if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", user); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) @@ -135,7 +136,7 @@ func Login(c *b.Config, db *b.DB, s map[string]*Session, sessionExpiryChan chan func Logout(c *b.Config, s map[string]*Session) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - tmpl, tmplErr := template.ParseFiles(c.WebDir + "/templates/login.html") + tmpl, tmplErr := template.ParseFiles(filepath.Join(c.WebDir, "templates", "login.html")) cookie, err := r.Cookie("cpolis_session") if err != nil { diff --git a/cmd/frontend/tags.go b/cmd/frontend/tags.go index aa2762a..1cdee8c 100644 --- a/cmd/frontend/tags.go +++ b/cmd/frontend/tags.go @@ -4,6 +4,7 @@ import ( "html/template" "log" "net/http" + "path/filepath" b "streifling.com/jason/cpolis/cmd/backend" ) @@ -15,7 +16,7 @@ func CreateTag(c *b.Config, s map[string]*Session) http.HandlerFunc { return } - tmpl, err := template.ParseFiles(c.WebDir + "/templates/add-tag.html") + tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "add-tag.html")) if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) @@ -42,7 +43,7 @@ func AddTag(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc { data := new(struct{ Role int }) data.Role = session.User.Role - tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html") + tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "hub.html")) tmpl = template.Must(tmpl, err) if err = tmpl.ExecuteTemplate(w, "page-content", data); err != nil { log.Println(err) diff --git a/cmd/frontend/users.go b/cmd/frontend/users.go index 12a1faf..158e57f 100644 --- a/cmd/frontend/users.go +++ b/cmd/frontend/users.go @@ -5,6 +5,7 @@ import ( "html/template" "log" "net/http" + "path/filepath" "sort" "strconv" @@ -57,7 +58,7 @@ func CreateUser(c *b.Config, s map[string]*Session) http.HandlerFunc { URL: "/user/add", } - tmpl, err := template.ParseFiles(c.WebDir + "/templates/edit-user.html") + tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "edit-user.html")) if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", data); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) @@ -134,7 +135,7 @@ func AddUser(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc { data := new(struct{ Role int }) data.Role = session.User.Role - tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html") + tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "hub.html")) tmpl = template.Must(tmpl, err) if err = tmpl.ExecuteTemplate(w, "page-content", data); err != nil { log.Println(err) @@ -167,7 +168,7 @@ func EditSelf(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc { Image: user.ProfilePicLink, } - tmpl, err := template.ParseFiles(c.WebDir + "/templates/edit-user.html") + tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "edit-user.html")) if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", data); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) @@ -242,7 +243,7 @@ func UpdateSelf(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc { data := new(struct{ Role int }) data.Role = session.User.Role - tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html") + tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "hub.html")) tmpl = template.Must(tmpl, err) if err = tmpl.ExecuteTemplate(w, "page-content", data); err != nil { log.Println(err) @@ -312,7 +313,7 @@ func AddFirstUser(c *b.Config, db *b.DB, s map[string]*Session, sessionExpiryCha data := new(struct{ Role int }) data.Role = user.Role - tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html") + tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "hub.html")) if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", data); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) @@ -343,7 +344,7 @@ func ShowAllUsers(c *b.Config, db *b.DB, s map[string]*Session, action string) h } delete(data.Users, session.User.ID) - tmpl, err := template.ParseFiles(c.WebDir + "/templates/show-all-users.html") + tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "show-all-users.html")) if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", data); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) @@ -381,7 +382,7 @@ func EditUser(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc { Image: user.ProfilePicLink, } - tmpl, err := template.ParseFiles(c.WebDir + "/templates/edit-user.html") + tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "edit-user.html")) if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", data); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) @@ -466,7 +467,7 @@ func UpdateUser(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc { data := new(struct{ Role int }) data.Role = session.User.Role - tmpl := template.Must(template.ParseFiles(c.WebDir + "/templates/hub.html")) + tmpl := template.Must(template.ParseFiles(filepath.Join(c.WebDir, "templates", "hub.html"))) if err = tmpl.ExecuteTemplate(w, "page-content", data); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) @@ -499,7 +500,7 @@ func DeleteUser(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc { data := new(struct{ Role int }) data.Role = session.User.Role - tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html") + tmpl, err := template.ParseFiles(filepath.Join(c.WebDir, "templates", "hub.html")) tmpl = template.Must(tmpl, err) if err = tmpl.ExecuteTemplate(w, "page-content", data); err != nil { log.Println(err)