From fdb11bc57c1bd92ed6983300f7fd3d350787ccbc Mon Sep 17 00:00:00 2001 From: kalipso Date: Thu, 10 Apr 2025 15:44:59 +0200 Subject: [PATCH] add batchupload --- controllers/shopItemController.go | 90 +++++++++++++++++++++++++++++++ main.go | 2 + models/printer.go | 2 + views/batchupload.html | 53 ++++++++++++++++++ views/header.html | 4 ++ views/printvariant.html | 11 ++-- views/shopitem.html | 21 -------- 7 files changed, 159 insertions(+), 24 deletions(-) create mode 100644 views/batchupload.html diff --git a/controllers/shopItemController.go b/controllers/shopItemController.go index 8db096f..a26d511 100644 --- a/controllers/shopItemController.go +++ b/controllers/shopItemController.go @@ -27,6 +27,8 @@ type ShopItemController interface { ShopItemView(*gin.Context) AddItemView(*gin.Context) AddItemHandler(*gin.Context) + AddItemsView(*gin.Context) + AddItemsHandler(*gin.Context) CreateTag(*gin.Context) GetAllTags(*gin.Context) EditItemView(*gin.Context) @@ -316,6 +318,94 @@ func (rc *shopItemController) AddItemHandler(c *gin.Context) { c.HTML(http.StatusOK, "additem.html", data) } +func (rc *shopItemController) AddItemsView(c *gin.Context) { + data := CreateSessionData(c, gin.H{}) + + c.HTML(http.StatusOK, "batchupload.html", data) +} + +func (rc *shopItemController) AddItemsHandler(c *gin.Context) { + errorHandler := func(err error) { + data := CreateSessionData(c, gin.H{ + "error": err, + }) + c.HTML(http.StatusBadRequest, "batchupload.html", data) + } + + form, err := c.MultipartForm() + + if err != nil { + errorHandler(err) + return + } + + files := form.File["pdf"] + + var shopItems []models.ShopItem + for _, file := range files { + dstPdf := filepath.Join("static/uploads", file.Filename) + + if err := c.SaveUploadedFile(file, dstPdf); err != nil { + errorHandler(err) + return + } + + dstImage := dstPdf + ".preview.png" + cmd := exec.Command("pdftoppm", "-png", "-singlefile", dstPdf, dstPdf+".preview") + _, err := cmd.Output() + + if err != nil { + fmt.Println("Error during pdftoppm: ", err.Error()) + } + + category, err := models.ParseCategory("Zine") + if err != nil { + errorHandler(err) + return + } + + variants := []models.ItemVariant{ + { + Name: "B/W", + Price: 1.0, + }, + } + + shopItem := models.ShopItem{ + Name: file.Filename, + Abstract: file.Filename, + Description: file.Filename, + Category: category, + IsPublic: true, + BasePrice: rc.GetBasePrice(variants), + Image: dstImage, + Pdf: dstPdf, + Variants: variants, + } + + _, err = repositories.ShopItems.Create(shopItem) + if err != nil { + errorHandler(err) + return + } + + shopItems = append(shopItems, shopItem) + } + + msg := "The Following items were registered:\n" + + for _, item := range shopItems { + msg += fmt.Sprintf("%s\n", item.Name) + } + + data := CreateSessionData(c, gin.H{ + "error": "", + "success": msg, + }) + + c.HTML(http.StatusOK, "batchupload.html", data) +} + func (rc *shopItemController) EditItemView(c *gin.Context) { shopItem, err := repositories.ShopItems.GetById(c.Param("id")) tags, err := repositories.Tags.GetAll() diff --git a/main.go b/main.go index b8e7b8e..141a577 100644 --- a/main.go +++ b/main.go @@ -104,9 +104,11 @@ func main() { viewRoutes.GET("/register", userController.RegisterView) viewRoutes.GET("/passwordreset", userController.ResetView) viewRoutes.GET("/additem", authValidator.RequireAuth, shopItemController.AddItemView) + viewRoutes.GET("/batchupload", authValidator.RequireAuth, shopItemController.AddItemsView) viewRoutes.POST("/login", userController.LoginHandler) viewRoutes.POST("/register", userController.RegisterHandler) viewRoutes.POST("/additem", authValidator.RequireAuth, shopItemController.AddItemHandler) + viewRoutes.POST("/batchupload", authValidator.RequireAuth, shopItemController.AddItemsHandler) viewRoutes.POST("/passwordreset", userController.ResetHandler) } diff --git a/models/printer.go b/models/printer.go index d307cb5..be8e6bb 100644 --- a/models/printer.go +++ b/models/printer.go @@ -50,6 +50,8 @@ func (p *PrintJob) Execute() error { baseCommand += fmt.Sprintf(" %v ", option) } + baseCommand += fmt.Sprintf(" -- %s", p.Pdf) + fmt.Println(baseCommand) return nil } diff --git a/views/batchupload.html b/views/batchupload.html new file mode 100644 index 0000000..1ddff0d --- /dev/null +++ b/views/batchupload.html @@ -0,0 +1,53 @@ +{{ template "header.html" . }} + +
+
+ Your Company +

Batch Upload

+ Select multiple files on the upload. For each a new Shop Item will be created with the Filename as Name, Abstract + and Description.
+ Afterwards you should edit every item and set the proper Values for that, price and so on. +
+ + +
+
+
+ +
+
+ +
+ +

or drag and drop

+
+

+ PDF up to 50MB +

+
+
+
+ +

+ {{ .data.error }} +

+

+ {{ .data.success }} +

+ + +
+ +
+
+
+
+ +{{ template "footer.html" . }} diff --git a/views/header.html b/views/header.html index ec062ca..6c9bb34 100644 --- a/views/header.html +++ b/views/header.html @@ -29,6 +29,10 @@
Add Item Tags + Print + Cart Logout
{{ else }} diff --git a/views/printvariant.html b/views/printvariant.html index f5bb36a..aea5173 100644 --- a/views/printvariant.html +++ b/views/printvariant.html @@ -4,10 +4,15 @@
+

Zineshop Print + Service +

+

+ Pressing Print will automatically print the given Zines for you.
+ Add Zines for printing simply by adding them to the Cart +

+

- Welcome to the Zineshop Printservice.
- Pressing Print will automatically print the given Zines for you. - All you have to do is set an Amount for each Zine you want to print.

CoverPage: If selected, the Printer will take Paper from the BypassTray for the first page. For example you can put colored paper there to have a nice looking front page, and the rest will be normal paper. Makue sure you put paper in that tray when selecting this option.

diff --git a/views/shopitem.html b/views/shopitem.html index fe9def4..3f90f00 100644 --- a/views/shopitem.html +++ b/views/shopitem.html @@ -77,25 +77,4 @@

- {{ template "footer.html" . }}