From 4d944ef65aa8f0caeee56c3b6e41868cbadb9f29 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Sat, 28 Sep 2024 13:12:46 +0200 Subject: [PATCH 1/5] Delete unused SaveArticle() --- cmd/frontend/articles.go | 91 ---------------------------------------- cmd/main.go | 1 - 2 files changed, 92 deletions(-) diff --git a/cmd/frontend/articles.go b/cmd/frontend/articles.go index 525675c..799d2c5 100644 --- a/cmd/frontend/articles.go +++ b/cmd/frontend/articles.go @@ -762,94 +762,3 @@ func EditArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc { template.Must(tmpl, err).ExecuteTemplate(w, "page-content", data) } } - -func SaveArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - session, err := getSession(w, r, c, s) - if err != nil { - return - } - - session.Values["article"] = nil - if err = session.Save(r, w); err != nil { - log.Println(err) - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - article := &b.Article{ - Title: r.PostFormValue("article-title"), - Description: r.PostFormValue("article-description"), - Published: false, - Rejected: false, - AuthorID: session.Values["id"].(int64), - IsInIssue: r.PostFormValue("issue") == "on", - AutoGenerated: false, - } - - oldID, err := strconv.ParseInt(r.PathValue("id"), 10, 64) - if err != nil { - log.Println(err) - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - fmt.Println(oldID) - if oldID != 0 { - if err = db.DeleteArticle(oldID); err != nil { - log.Println(err) - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - if err = os.Remove(fmt.Sprint(c.ArticleDir, "/", oldID, ".md")); err != nil { - log.Println(err) - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - } - - article.ID, err = db.AddArticle(article) - if err != nil { - log.Println(err) - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - articleAbsName := fmt.Sprint(c.ArticleDir, "/", article.ID, ".md") - if err = os.WriteFile(articleAbsName, []byte(r.PostFormValue("article-content")), 0644); err != nil { - log.Println(err) - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - article.Link = fmt.Sprint(c.Domain, "/article/serve/", article.ID) - if err = db.UpdateAttributes(&b.Attribute{Table: "articles", ID: article.ID, AttName: "link", Value: article.Link}); err != nil { - log.Println(err) - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - r.ParseForm() - tags := make([]int64, 0) - for _, tag := range r.Form["tags"] { - tagID, err := strconv.ParseInt(tag, 10, 64) - if err != nil { - log.Println(err) - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - tags = append(tags, tagID) - } - if err = db.WriteArticleTags(article.ID, tags); err != nil { - log.Println(err) - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html") - tmpl = template.Must(tmpl, err) - tmpl.ExecuteTemplate(w, "page-content", session.Values["role"]) - } -} diff --git a/cmd/main.go b/cmd/main.go index 81cd90c..ff97803 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -62,7 +62,6 @@ func main() { mux.HandleFunc("GET /article/review-edit/{id}", f.ReviewArticle(config, db, store, "allow-edit", "Artikel bearbeiten", "Bearbeiten erlauben")) mux.HandleFunc("GET /article/review-rejected/{id}", f.ReviewRejectedArticle(config, db, store)) mux.HandleFunc("GET /article/review-unpublished/{id}", f.ReviewArticle(config, db, store, "publish", "Artikel veröffentlichen", "Veröffentlichen")) - mux.HandleFunc("GET /article/save/{id}", f.SaveArticle(config, db, store)) mux.HandleFunc("GET /article/serve/{id}", c.ServeArticle(config, db)) mux.HandleFunc("GET /article/write", f.WriteArticle(config, db, store)) mux.HandleFunc("GET /hub", f.ShowHub(config, db, store)) From a9bef63174ac04721c5f216de302725dc5b2ba9e Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Sat, 28 Sep 2024 13:15:00 +0200 Subject: [PATCH 2/5] Change version number --- web/templates/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web/templates/index.html b/web/templates/index.html index 1afc3c4..95c394a 100644 --- a/web/templates/index.html +++ b/web/templates/index.html @@ -35,7 +35,7 @@

© 2024 Jason Streifling. Alle Rechte vorbehalten.

-

v0.11.0 - Alpha: Drastische Änderungen und Fehler vorbehalten.

+

v0.11.1 - Alpha: Drastische Änderungen und Fehler vorbehalten.

From afa1b65563df31161af30a78f2c4702d00acfc6d Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Sat, 28 Sep 2024 13:22:53 +0200 Subject: [PATCH 3/5] Moved getSession to frontend/sessions.go --- cmd/frontend/sessions.go | 21 +++++++++++++++++++++ cmd/frontend/verification.go | 29 ----------------------------- 2 files changed, 21 insertions(+), 29 deletions(-) delete mode 100644 cmd/frontend/verification.go diff --git a/cmd/frontend/sessions.go b/cmd/frontend/sessions.go index 3a53a28..d88e595 100644 --- a/cmd/frontend/sessions.go +++ b/cmd/frontend/sessions.go @@ -1,11 +1,13 @@ package frontend import ( + "errors" "fmt" "html/template" "log" "net/http" + "github.com/gorilla/sessions" b "streifling.com/jason/cpolis/cmd/backend" ) @@ -26,6 +28,25 @@ func saveSession(w http.ResponseWriter, r *http.Request, s *b.CookieStore, u *b. return nil } +// getSession is used for verifying that the user is logged in and returns their session and an error. +func getSession(w http.ResponseWriter, r *http.Request, c *b.Config, s *b.CookieStore) (*sessions.Session, error) { + msg := "Keine gültige Session. Bitte erneut anmelden." + tmpl, tmplErr := template.ParseFiles(c.WebDir+"/templates/index.html", c.WebDir+"/templates/login.html") + + session, err := s.Get(r, "cookie") + if err != nil { + template.Must(tmpl, tmplErr).ExecuteTemplate(w, "page-content", msg) + return nil, err + } + + if session.IsNew { + template.Must(tmpl, tmplErr).ExecuteTemplate(w, "page-content", msg) + return session, errors.New("error: no existing session") + } + + return session, nil +} + func HomePage(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { numRows, err := db.CountEntries("users") diff --git a/cmd/frontend/verification.go b/cmd/frontend/verification.go deleted file mode 100644 index 9a62da5..0000000 --- a/cmd/frontend/verification.go +++ /dev/null @@ -1,29 +0,0 @@ -package frontend - -import ( - "errors" - "html/template" - "net/http" - - "github.com/gorilla/sessions" - b "streifling.com/jason/cpolis/cmd/backend" -) - -// getSession is used for verifying that the user is logged in and returns their session and an error. -func getSession(w http.ResponseWriter, r *http.Request, c *b.Config, s *b.CookieStore) (*sessions.Session, error) { - msg := "Keine gültige Session. Bitte erneut anmelden." - tmpl, tmplErr := template.ParseFiles(c.WebDir+"/templates/index.html", c.WebDir+"/templates/login.html") - - session, err := s.Get(r, "cookie") - if err != nil { - template.Must(tmpl, tmplErr).ExecuteTemplate(w, "page-content", msg) - return nil, err - } - - if session.IsNew { - template.Must(tmpl, tmplErr).ExecuteTemplate(w, "page-content", msg) - return session, errors.New("error: no existing session") - } - - return session, nil -} From e4bef7006c45d9a281f1d61add434049a9836c5c Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Sat, 28 Sep 2024 13:44:25 +0200 Subject: [PATCH 4/5] Abstract sessions.Session with b.session --- cmd/backend/sessions.go | 5 ++++- cmd/frontend/sessions.go | 6 +++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/cmd/backend/sessions.go b/cmd/backend/sessions.go index eb55dfc..9fbf3f9 100644 --- a/cmd/backend/sessions.go +++ b/cmd/backend/sessions.go @@ -10,7 +10,10 @@ import ( "github.com/gorilla/sessions" ) -type CookieStore struct{ sessions.CookieStore } +type ( + CookieStore struct{ sessions.CookieStore } + Session struct{ sessions.Session } +) func NewKey() ([]byte, error) { key := make([]byte, 32) diff --git a/cmd/frontend/sessions.go b/cmd/frontend/sessions.go index d88e595..b2f94de 100644 --- a/cmd/frontend/sessions.go +++ b/cmd/frontend/sessions.go @@ -7,7 +7,6 @@ import ( "log" "net/http" - "github.com/gorilla/sessions" b "streifling.com/jason/cpolis/cmd/backend" ) @@ -29,16 +28,17 @@ func saveSession(w http.ResponseWriter, r *http.Request, s *b.CookieStore, u *b. } // getSession is used for verifying that the user is logged in and returns their session and an error. -func getSession(w http.ResponseWriter, r *http.Request, c *b.Config, s *b.CookieStore) (*sessions.Session, error) { +func getSession(w http.ResponseWriter, r *http.Request, c *b.Config, s *b.CookieStore) (*b.Session, error) { msg := "Keine gültige Session. Bitte erneut anmelden." tmpl, tmplErr := template.ParseFiles(c.WebDir+"/templates/index.html", c.WebDir+"/templates/login.html") - session, err := s.Get(r, "cookie") + tmpSession, err := s.Get(r, "cookie") if err != nil { template.Must(tmpl, tmplErr).ExecuteTemplate(w, "page-content", msg) return nil, err } + session := &b.Session{Session: *tmpSession} if session.IsNew { template.Must(tmpl, tmplErr).ExecuteTemplate(w, "page-content", msg) return session, errors.New("error: no existing session") From 1368593c75e46293dfbd957c5ab1b4c0b65bd291 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Sat, 28 Sep 2024 13:55:25 +0200 Subject: [PATCH 5/5] Move file handling to backend/files.go --- cmd/backend/files.go | 28 ++++++++++++++++++++++++++++ cmd/frontend/articles.go | 28 ++-------------------------- 2 files changed, 30 insertions(+), 26 deletions(-) create mode 100644 cmd/backend/files.go diff --git a/cmd/backend/files.go b/cmd/backend/files.go new file mode 100644 index 0000000..2ccbb37 --- /dev/null +++ b/cmd/backend/files.go @@ -0,0 +1,28 @@ +package backend + +import ( + "fmt" + "io" + "os" +) + +func CopyFile(src, dst string) error { + srcFile, err := os.Open(src) + if err != nil { + return fmt.Errorf("error opening source file: %v", err) + } + defer srcFile.Close() + + dstFile, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644) + if err != nil { + return fmt.Errorf("error opening destination file: %v", err) + } + defer dstFile.Close() + + _, err = io.Copy(dstFile, srcFile) + if err != nil { + return fmt.Errorf("error copying file: %v", err) + } + + return dstFile.Sync() +} diff --git a/cmd/frontend/articles.go b/cmd/frontend/articles.go index 799d2c5..befb441 100644 --- a/cmd/frontend/articles.go +++ b/cmd/frontend/articles.go @@ -33,27 +33,6 @@ type EditorHTMLData struct { Tags []*b.Tag } -func copyFile(src, dst string) error { - srcFile, err := os.Open(src) - if err != nil { - return fmt.Errorf("error opening source file: %v", err) - } - defer srcFile.Close() - - dstFile, err := os.Create(dst) - if err != nil { - return fmt.Errorf("error opening destination file: %v", err) - } - defer dstFile.Close() - - _, err = io.Copy(dstFile, srcFile) - if err != nil { - return fmt.Errorf("error copying file: %v", err) - } - - return dstFile.Sync() -} - func WriteArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { session, err := getSession(w, r, c, s) @@ -61,14 +40,13 @@ func WriteArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc { return } - data := new(EditorHTMLData) + data := &EditorHTMLData{Action: "submit"} if session.Values["article"] == nil { data = &EditorHTMLData{Article: new(b.Article)} } else { data = session.Values["article"].(*EditorHTMLData) } - // data.Mode = EditMode data.Tags, err = db.GetTagList() if err != nil { @@ -77,8 +55,6 @@ func WriteArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc { return } - data.Action = "submit" - tmpl, err := template.ParseFiles(c.WebDir + "/templates/editor.html") template.Must(tmpl, err).ExecuteTemplate(w, "page-content", data) } @@ -696,7 +672,7 @@ func AllowEditArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc return } - if err = copyFile(fmt.Sprint(c.ArticleDir, "/", oldID, ".md"), fmt.Sprint(c.ArticleDir, "/", newID, ".md")); err != nil { + if err = b.CopyFile(fmt.Sprint(c.ArticleDir, "/", oldID, ".md"), fmt.Sprint(c.ArticleDir, "/", newID, ".md")); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return