From d4446c1f24edc12e3d5463b234fffea95f793a70 Mon Sep 17 00:00:00 2001 From: Balakrishnan Balasubramanian Date: Thu, 1 Aug 2024 22:33:29 -0400 Subject: [PATCH] upload support for new images --- README.md | 2 ++ main.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ web/index.css | 29 +++++++++++++++++++++++++++++ web/index.html | 14 ++++++++++++++ web/upload.html | 12 ++++++++++++ 5 files changed, 102 insertions(+) create mode 100644 README.md create mode 100644 web/upload.html diff --git a/README.md b/README.md new file mode 100644 index 0000000..367c14e --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ + +# Collage Maker diff --git a/main.go b/main.go index 7442ec0..42927eb 100644 --- a/main.go +++ b/main.go @@ -34,6 +34,11 @@ var ( webFS embed.FS ) +const ( + kb = 1024 + mb = 1024 * kb +) + func main() { flag.StringVar(&imagesDir, "images-dir", "images", "Sets the images dir") flag.StringVar(&collageDir, "collages-dir", "collages", "Sets the collages dir") @@ -110,6 +115,46 @@ func main() { slog.Error("Failed to write collageFile", "error", err) } }) + http.HandleFunc("/upload", func(w http.ResponseWriter, r *http.Request) { + err := func() error { + err := r.ParseMultipartForm(500 * mb) + if err != nil { + return err + } + files, ok := r.MultipartForm.File["photos"] + if !ok { + return fmt.Errorf("photos not found in request") + } + for i, f := range files { + imgFile, err := f.Open() + if err != nil { + return err + } + defer imgFile.Close() + imagePath := path.Join(imagesDir, fmt.Sprintf("img%d.jpg", i+1)) + os.Remove(imagePath) + if err != nil { + return err + } + op, err := os.Create(imagePath) + if err != nil { + return err + } + defer op.Close() + _, err = io.Copy(op, imgFile) + if err != nil { + return err + } + } + return nil + }() + if err != nil { + slog.Error("Image upload failed", "error", err) + w.WriteHeader(http.StatusBadRequest) + return + } + http.Redirect(w, r, "/", http.StatusSeeOther) + }) addrType, server, done, err := anyhttp.Serve(listenAddr, idle.WrapHandler(nil)) if err != nil { slog.Error("anyhttp Serve failed", "error", err) diff --git a/web/index.css b/web/index.css index 0eb633b..febfdba 100644 --- a/web/index.css +++ b/web/index.css @@ -10,7 +10,36 @@ justify-content: space-around; flex-direction: column; flex: 25%; + overflow: scroll; } + +.img-container { + /** margin: 30px; **/ + display: flex; + gap: 1rem; + flex-wrap: wrap; + justify-content: center; +} +.img-container img { + width: 100px; + height: 100px; + border: 5px solid #999; +} +.img-container p { + text-align: center; + font-size: 2rem; +} +.toolbar { + height: 30px; + display: flex; + gap: 1rem; + justify-content: center; + align-items: flex-start; +} +.selected-img { + border: 5px solid lightgreen !important; +} + .imagebox { padding: 2rem; flex: 75%; diff --git a/web/index.html b/web/index.html index 8a17185..870f3f6 100644 --- a/web/index.html +++ b/web/index.html @@ -24,6 +24,20 @@ +
+
+ + +
+
+
+
+
+
+
+
+
+