Skip to content

Commit

Permalink
fix(images): resolve an edge-case for invalid URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
vednoc committed Sep 29, 2021
1 parent ddce618 commit 65e4554
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
5 changes: 3 additions & 2 deletions handlers/style/add.go
Expand Up @@ -118,13 +118,14 @@ func CreatePost(c *fiber.Ctx) error {
if err != nil {
log.Warn.Printf("Failed to generate images for %d: %s\n", s.ID, err.Error())
s.Preview = ""
} else {
s.Preview = config.BaseURL + "/api/style/preview/" + styleID + ".jpeg"
}
s.Preview = config.BaseURL + "/api/style/preview/" + styleID + ".jpeg"
}

// TODO: Remove during rewrite of images module. The name-schema shouldn't
// require a style id; hashing username+time.Now() should be sufficient. #77
if err = models.UpdateStyle(s); err != nil {
if err = s.UpdateColumn("preview", s.Preview); err != nil {
log.Warn.Printf("Failed to update style %s: %s\n", styleID, err.Error())
}

Expand Down
9 changes: 5 additions & 4 deletions handlers/style/edit.go
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/userstyles-world/fiber/v2"
"gorm.io/gorm"

"userstyles.world/handlers/jwt"
"userstyles.world/models"
Expand Down Expand Up @@ -71,6 +72,7 @@ func EditPost(c *fiber.Ctx) error {
}

q := models.Style{
Model: gorm.Model{ID: s.ID},
Name: c.FormValue("name"),
Description: c.FormValue("description"),
Notes: c.FormValue("notes"),
Expand Down Expand Up @@ -119,13 +121,12 @@ func EditPost(c *fiber.Ctx) error {
if err != nil {
log.Warn.Printf("Failed to generate images for %d: %s\n", s.ID, err.Error())
q.Preview = ""
} else {
q.Preview = config.BaseURL + "/api/style/preview/" + styleID + ".jpeg"
}

q.Preview = config.BaseURL + "/api/style/preview/" + styleID + ".jpeg"
}

err = database.Conn.Model(q).Where("id", styleID).Updates(q).Error
if err != nil {
if err = q.UpdateColumn("preview", q.Preview); err != nil {
log.Warn.Printf("Failed to update preview image for %s: %s\n", styleID, err.Error())
return c.Render("err", fiber.Map{
"Title": "Failed to update preview image",
Expand Down
5 changes: 3 additions & 2 deletions handlers/style/import.go
Expand Up @@ -110,13 +110,14 @@ func ImportPost(c *fiber.Ctx) error {
if err != nil {
log.Warn.Printf("Failed to generate images for %d: %s\n", s.ID, err.Error())
s.Preview = ""
} else {
s.Preview = config.BaseURL + "/api/style/preview/" + styleID + ".jpeg"
}
s.Preview = config.BaseURL + "/api/style/preview/" + styleID + ".jpeg"
}

// TODO: Remove during rewrite of images module. The name-schema shouldn't
// require a style id; hashing username+time.Now() should be sufficient. #77
if err = models.UpdateStyle(s); err != nil {
if err = s.UpdateColumn("preview", s.Preview); err != nil {
log.Warn.Printf("Failed to update style %s: %s\n", styleID, err.Error())
}

Expand Down
4 changes: 4 additions & 0 deletions models/style.go
Expand Up @@ -413,3 +413,7 @@ func (*Style) MirrorStyle(f map[string]interface{}) error {

return nil
}

func (s *Style) UpdateColumn(col string, val interface{}) error {
return db().Model(modelStyle).Where("id", s.ID).UpdateColumn(col, val).Error
}

0 comments on commit 65e4554

Please sign in to comment.