generate preview image from pdf if no image given

This commit is contained in:
2025-03-21 13:27:13 +01:00
parent 6cbd9f5a3b
commit 69420fab6c

View File

@@ -5,6 +5,7 @@ import (
"net/http"
"strconv"
"path/filepath"
"os/exec"
"github.com/gin-gonic/gin"
@@ -66,6 +67,7 @@ func (rc *shopItemController) GetById(c *gin.Context) {
}
func (rc *shopItemController) NewShopItemFromForm(ctx *gin.Context) (models.ShopItem, error) {
defaultImagePath := "static/img/zine.jpg"
name := ctx.PostForm("name")
abstract := ctx.PostForm("abstract")
description := ctx.PostForm("description")
@@ -74,7 +76,7 @@ func (rc *shopItemController) NewShopItemFromForm(ctx *gin.Context) (models.Shop
variantValues := ctx.PostFormArray("variant-value[]")
tagIds := ctx.PostFormArray("tags[]")
image, err := ctx.FormFile("image")
dstImage := "static/img/zine.jpg"
dstImage := defaultImagePath
if err == nil {
dstImage = filepath.Join("static/uploads", image.Filename)
@@ -91,6 +93,16 @@ func (rc *shopItemController) NewShopItemFromForm(ctx *gin.Context) (models.Shop
if err := ctx.SaveUploadedFile(pdf, dstPdf); err != nil {
return models.ShopItem{}, fmt.Errorf("Could not save PDF")
}
if dstImage == defaultImagePath {
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())
}
}
}
if name == "" || description == "" {