From 55e635918ff9e73ff888dae94b383b9365f02b47 Mon Sep 17 00:00:00 2001 From: kekskurse Date: Fri, 11 Feb 2022 17:47:50 +0100 Subject: [PATCH] #4 Allow Change main Image --- gallery.go | 52 ++++++++++++++++++ main.go | 2 + templates/gallery_mainImage.tmpl | 90 ++++++++++++++++++++++++++++++++ 3 files changed, 144 insertions(+) create mode 100644 templates/gallery_mainImage.tmpl diff --git a/gallery.go b/gallery.go index 1e4e60e..93982ce 100644 --- a/gallery.go +++ b/gallery.go @@ -176,4 +176,56 @@ func galleryEdit(t template.Template,data template.Data, c flamego.Context) { t.HTML(http.StatusOK, "gallery_edit") +} + +func galleryEditMainImage(t template.Template, data template.Data, c flamego.Context) { + data["id"] = c.Param("id") + t.HTML(http.StatusOK, "gallery_mainImage") +} + +func galleryEditMainImageChange(s session.Session, c flamego.Context) { + log.Debug().Msg("Change Gallery Main Image") + c.Request().ParseMultipartForm(9001) + + + //Upload Croped Image + file, _, err := c.Request().FormFile("cropImage") + if err != nil { + panic(err) + } + defer file.Close() + + cropedImage, _ , err := image.Decode(file) + if err != nil { + panic(err) + } + path := fmt.Sprintf("/gallery/%v/%v/main/%v-%v", s.Get("user_id"), c.Param("id"), time.Now().UnixNano(), "crop.png") + uploadImageToS3(cropedImage, path) + + //Upload Original Image + fileOrginal, fileHeaderOrginal, err := c.Request().FormFile("orginal") + if err != nil { + panic(err) + } + defer fileOrginal.Close() + + orignalImage, _ , err := image.Decode(fileOrginal) + if err != nil { + panic(err) + } + pathOrignal := fmt.Sprintf("/gallery/%v/%v/main/%v-orginal-%v", s.Get("user_id"), c.Param("id"), time.Now().UnixNano(), fileHeaderOrginal.Filename) + uploadImageToS3(orignalImage, pathOrignal) + + + _, err = sqlConnection.NamedExec("UPDATE gallery SET `mainImage` = :uploadPath, mainImageOriginal = :uploadPathOriginal WHERE `id` = :id LIMIT 1", map[string]interface{}{ + "uploadPath": path, + "uploadPathOriginal": pathOrignal, + "id": c.Param("id"), + }) + + if err != nil { + log.Fatal().Err(err).Msg("Cant update Main Gallery") + } + + c.ResponseWriter().Write([]byte("{\"status\": \"ok\"}")) } \ No newline at end of file diff --git a/main.go b/main.go index 06b5bba..73c4327 100644 --- a/main.go +++ b/main.go @@ -185,6 +185,8 @@ func main() { f.Get("/gallery/edit", galleryEditList) f.Get("/gallery/edit/{id}", galleryEdit) f.Post("/gallery/edit/{id}", galleryEditSave) + f.Get("/gallery/edit/{id}/image", galleryEditMainImage) + f.Post("/gallery/edit/{id}/image", galleryEditMainImageChange) f.Get("/file/upload", uploadImageForm) f.Post("/file/upload", uploadImage) diff --git a/templates/gallery_mainImage.tmpl b/templates/gallery_mainImage.tmpl new file mode 100644 index 0000000..3c77d6a --- /dev/null +++ b/templates/gallery_mainImage.tmpl @@ -0,0 +1,90 @@ +{{template "top" .}} + + +
+
+
+

Change Gallery Preview Image

+
+
+ +
+
+ + +
+ + +
+
+ +
+
+
+{{template "bottom" .}} \ No newline at end of file