From 366b1768e4da65cfef5672786bc07611838f77cd Mon Sep 17 00:00:00 2001
From: a0eoc <42794343+a0eoc@users.noreply.github.com>
Date: Sun, 12 Nov 2023 19:52:30 +0500
Subject: [PATCH] fix(import): don't reset PreviewURL
...on validation failure which is quite annoying and cought me a few times in production.
This doesn't fix resetting of preview file though.
---
handlers/style/add.go | 5 ++++-
handlers/style/import.go | 15 +++++++++------
web/views/style/add.tmpl | 2 +-
web/views/style/import.tmpl | 2 +-
4 files changed, 15 insertions(+), 9 deletions(-)
diff --git a/handlers/style/add.go b/handlers/style/add.go
index 576f039f..e57786c8 100644
--- a/handlers/style/add.go
+++ b/handlers/style/add.go
@@ -53,6 +53,10 @@ func CreatePost(c *fiber.Ctx) error {
}
c.Locals("Style", s)
+ // Get previewURL
+ preview := c.FormValue("previewURL")
+ c.Locals("PreviewURL", preview)
+
m, err := s.Validate(validator.V, true)
if err != nil {
c.Locals("err", m)
@@ -82,7 +86,6 @@ func CreatePost(c *fiber.Ctx) error {
// Check preview image.
file, _ := c.FormFile("preview")
- preview := c.FormValue("previewURL")
styleID := strconv.FormatUint(uint64(s.ID), 10)
if file != nil || preview != "" {
if err = images.Generate(file, styleID, "0", "", preview); err != nil {
diff --git a/handlers/style/import.go b/handlers/style/import.go
index 558a67c8..3363a8e3 100644
--- a/handlers/style/import.go
+++ b/handlers/style/import.go
@@ -102,14 +102,18 @@ func ImportPost(c *fiber.Ctx) error {
s.MirrorCode = c.FormValue("mirrorCode") == "on"
s.MirrorMeta = c.FormValue("mirrorMeta") == "on"
+ // Get previewURL
+ preview := c.FormValue("previewURL", s.Preview)
+
m, err := s.Validate(validator.V, true)
if err != nil {
return c.Render("style/import", fiber.Map{
- "Title": "Import userstyle",
- "User": u,
- "Style": s,
- "err": m,
- "Error": "Incorrect userstyle data was entered. Please review the fields bellow.",
+ "Title": "Import userstyle",
+ "User": u,
+ "Style": s,
+ "PreviewURL": preview,
+ "err": m,
+ "Error": "Incorrect userstyle data was entered. Please review the fields bellow.",
})
}
@@ -140,7 +144,6 @@ func ImportPost(c *fiber.Ctx) error {
// Check preview image.
file, _ := c.FormFile("preview")
- preview := c.FormValue("previewURL", s.Preview)
styleID := strconv.FormatUint(uint64(s.ID), 10)
if file != nil || preview != "" {
if err := images.Generate(file, styleID, "0", "", preview); err != nil {
diff --git a/web/views/style/add.tmpl b/web/views/style/add.tmpl
index 19a4e165..1b04dca8 100644
--- a/web/views/style/add.tmpl
+++ b/web/views/style/add.tmpl
@@ -73,7 +73,7 @@
diff --git a/web/views/style/import.tmpl b/web/views/style/import.tmpl
index 9f25b50f..379462d4 100644
--- a/web/views/style/import.tmpl
+++ b/web/views/style/import.tmpl
@@ -69,7 +69,7 @@