generate preview image from pdf if no image given
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"os/exec"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"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) {
|
func (rc *shopItemController) NewShopItemFromForm(ctx *gin.Context) (models.ShopItem, error) {
|
||||||
|
defaultImagePath := "static/img/zine.jpg"
|
||||||
name := ctx.PostForm("name")
|
name := ctx.PostForm("name")
|
||||||
abstract := ctx.PostForm("abstract")
|
abstract := ctx.PostForm("abstract")
|
||||||
description := ctx.PostForm("description")
|
description := ctx.PostForm("description")
|
||||||
@@ -74,7 +76,7 @@ func (rc *shopItemController) NewShopItemFromForm(ctx *gin.Context) (models.Shop
|
|||||||
variantValues := ctx.PostFormArray("variant-value[]")
|
variantValues := ctx.PostFormArray("variant-value[]")
|
||||||
tagIds := ctx.PostFormArray("tags[]")
|
tagIds := ctx.PostFormArray("tags[]")
|
||||||
image, err := ctx.FormFile("image")
|
image, err := ctx.FormFile("image")
|
||||||
dstImage := "static/img/zine.jpg"
|
dstImage := defaultImagePath
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
dstImage = filepath.Join("static/uploads", image.Filename)
|
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 {
|
if err := ctx.SaveUploadedFile(pdf, dstPdf); err != nil {
|
||||||
return models.ShopItem{}, fmt.Errorf("Could not save PDF")
|
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 == "" {
|
if name == "" || description == "" {
|
||||||
|
|||||||
Reference in New Issue
Block a user