From a33e7f9896e705cf927d0d544d166a956dd57ec3 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Fri, 30 Aug 2024 16:06:19 +0200 Subject: [PATCH] Move images.go from frontend to calls --- cmd/calls/images.go | 24 ++++++++++++++++++++++++ cmd/frontend/images.go | 26 -------------------------- cmd/main.go | 4 ++-- web/templates/editor.html | 2 +- web/templates/rework-article.html | 2 +- 5 files changed, 28 insertions(+), 30 deletions(-) create mode 100644 cmd/calls/images.go delete mode 100644 cmd/frontend/images.go diff --git a/cmd/calls/images.go b/cmd/calls/images.go new file mode 100644 index 0000000..42a2d19 --- /dev/null +++ b/cmd/calls/images.go @@ -0,0 +1,24 @@ +package calls + +import ( + "log" + "net/http" + "path/filepath" + + b "streifling.com/jason/cpolis/cmd/backend" +) + +func ServeImage(c *b.Config, s *b.CookieStore) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + if tokenIsVerified(w, r, c) { + 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")) + } + } +} diff --git a/cmd/frontend/images.go b/cmd/frontend/images.go deleted file mode 100644 index 5718ee5..0000000 --- a/cmd/frontend/images.go +++ /dev/null @@ -1,26 +0,0 @@ -package frontend - -import ( - "log" - "net/http" - "path/filepath" - - b "streifling.com/jason/cpolis/cmd/backend" -) - -func ServeImage(c *b.Config, s *b.CookieStore) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - if _, err := getSession(w, r, c, s); err != nil { - return - } - - 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")) - } -} diff --git a/cmd/main.go b/cmd/main.go index 0b60d29..157fba7 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -61,12 +61,12 @@ func main() { 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)) + mux.HandleFunc("GET /image/serve/{pic}", c.ServeImage(config, store)) mux.HandleFunc("GET /issue/publish", f.PublishLatestIssue(config, db, store)) mux.HandleFunc("GET /issue/this", f.ShowCurrentArticles(config, db, store)) mux.HandleFunc("GET /logout", f.Logout(config, store)) mux.HandleFunc("GET /pdf/get-list", c.ServePDFList(config)) mux.HandleFunc("GET /pdf/serve/{id}", c.ServePDF(config)) - mux.HandleFunc("GET /pic/serve/{pic}", f.ServeImage(config, store)) mux.HandleFunc("GET /rss/serve", c.ServeRSS(config)) mux.HandleFunc("GET /tag/create", f.CreateTag(config, store)) mux.HandleFunc("GET /user/create", f.CreateUser(config, store)) @@ -78,8 +78,8 @@ func main() { mux.HandleFunc("POST /article/resubmit/{id}", f.ResubmitArticle(config, db, store)) mux.HandleFunc("POST /article/submit", f.SubmitArticle(config, db, store)) + mux.HandleFunc("POST /image/upload", f.UploadImage(config, store)) mux.HandleFunc("POST /login", f.Login(config, db, store)) - mux.HandleFunc("POST /pic/upload", f.UploadImage(config, store)) mux.HandleFunc("POST /tag/add", f.AddTag(config, db, store)) mux.HandleFunc("POST /user/add", f.AddUser(config, db, store)) mux.HandleFunc("POST /user/add-first", f.AddFirstUser(config, db, store)) diff --git a/web/templates/editor.html b/web/templates/editor.html index 043fec8..4d95b0b 100644 --- a/web/templates/editor.html +++ b/web/templates/editor.html @@ -46,7 +46,7 @@ var formData = new FormData(); formData.append('article-image', file); - fetch('/pic/upload', { + fetch('/image/upload', { method: 'POST', body: formData }) diff --git a/web/templates/rework-article.html b/web/templates/rework-article.html index 70d0252..e7d18a9 100644 --- a/web/templates/rework-article.html +++ b/web/templates/rework-article.html @@ -48,7 +48,7 @@ var formData = new FormData(); formData.append('article-image', file); - fetch('/pic/upload', { + fetch('/image/upload', { method: 'POST', body: formData })