From fa214f4fdf0019d7e6190305f68c72e866e8f66b Mon Sep 17 00:00:00 2001 From: kalipso Date: Wed, 5 Mar 2025 15:06:28 +0100 Subject: [PATCH] add pdf upload --- controllers/shopItemController.go | 19 +++++++++++++++---- models/shopItem.go | 1 + views/additem.html | 24 +++++++++++++++++++++++- views/shopitem.html | 8 ++++++-- views/shopitems.html | 3 ++- 5 files changed, 47 insertions(+), 8 deletions(-) diff --git a/controllers/shopItemController.go b/controllers/shopItemController.go index 7504558..354c334 100644 --- a/controllers/shopItemController.go +++ b/controllers/shopItemController.go @@ -74,15 +74,25 @@ func (rc *shopItemController) NewShopItemFromForm(ctx *gin.Context) (models.Shop variantValues := ctx.PostFormArray("variant-value[]") tagIds := ctx.PostFormArray("tags[]") image, err := ctx.FormFile("image") - dst := "static/img/zine.jpg" + dstImage := "static/img/zine.jpg" if err == nil { - dst = filepath.Join("static/uploads", image.Filename) - if err := ctx.SaveUploadedFile(image, dst); err != nil { + dstImage = filepath.Join("static/uploads", image.Filename) + if err := ctx.SaveUploadedFile(image, dstImage); err != nil { return models.ShopItem{}, fmt.Errorf("Could not save image") } } + dstPdf := "" + pdf, err := ctx.FormFile("pdf") + + if err == nil { + dstPdf = filepath.Join("static/uploads", pdf.Filename) + if err := ctx.SaveUploadedFile(pdf, dstPdf); err != nil { + return models.ShopItem{}, fmt.Errorf("Could not save PDF") + } + } + if name == "" || description == "" { return models.ShopItem{}, fmt.Errorf("Name or description empty") } @@ -124,7 +134,8 @@ func (rc *shopItemController) NewShopItemFromForm(ctx *gin.Context) (models.Shop Description: description, Category: category, IsPublic: true, - Image: dst, + Image: dstImage, + Pdf: dstPdf, Variants: variants, } diff --git a/models/shopItem.go b/models/shopItem.go index 8537b33..5303535 100644 --- a/models/shopItem.go +++ b/models/shopItem.go @@ -48,4 +48,5 @@ type ShopItem struct { IsPublic bool `json:"isPublic" gorm:"default:true"` Tags []Tag `gorm:"many2many:item_tags;"` Image string + Pdf string } diff --git a/views/additem.html b/views/additem.html index 2f2bac3..e5e0c3a 100644 --- a/views/additem.html +++ b/views/additem.html @@ -77,7 +77,6 @@ -
+
+ +
+
+ +
+ +

or drag and drop

+
+

+ PDF up to 50MB +

+
+
+
+

{{ .data.error }}

diff --git a/views/shopitem.html b/views/shopitem.html index 04b401f..48eacd8 100644 --- a/views/shopitem.html +++ b/views/shopitem.html @@ -15,13 +15,17 @@ +
+ +
+ {{ if .loggedIn }}
- +
- +
{{ end }} diff --git a/views/shopitems.html b/views/shopitems.html index 4a4f083..00dc8cc 100644 --- a/views/shopitems.html +++ b/views/shopitems.html @@ -15,7 +15,8 @@ {{ .Name }} - +

{{ .Abstract }}

+

{{ range .Tags }}{{ .Name }} {{ end }}

{{ .BasePrice }}€