Skip to content

Commit

Permalink
fix(import): don't reset PreviewURL
Browse files Browse the repository at this point in the history
...on validation failure which is quite annoying and cought me a few times in production.
This doesn't fix resetting of preview file though.
  • Loading branch information
astyled committed Nov 12, 2023
1 parent ba7152e commit 366b176
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
5 changes: 4 additions & 1 deletion handlers/style/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
15 changes: 9 additions & 6 deletions handlers/style/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
})
}

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion web/views/style/add.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
<input
type="url" name="previewURL" id="previewURL"
aria-describedby="previewURL-hint"
value="{{ .Style.Preview }}"
value="{{ .PreviewURL }}"
placeholder="Link to userstyle's preview image (jpg, jpeg, png, avif, and webp formats only)">

<label for="preview">Upload preview image</label>
Expand Down
2 changes: 1 addition & 1 deletion web/views/style/import.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
<input
type="url" name="previewURL" id="previewURL"
aria-describedby="previewURL-hint"
value="{{ .Style.Preview }}"
value="{{ .PreviewURL }}"
placeholder="Link to userstyle's preview image (jpg, jpeg, png, avif, and webp formats only)">

<label for="preview">Upload preview image</label>
Expand Down

0 comments on commit 366b176

Please sign in to comment.