74 lines
2.5 KiB
Go
74 lines
2.5 KiB
Go
package handlers
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
// GenerateBarcode godoc
|
|
//
|
|
// @Summary Generate barcodes
|
|
// @Description Generate barcodes based on the provided data
|
|
// @Tags barcodes
|
|
// @Param type path string true "Barcode type" Enums(ean13, code128)
|
|
// @Param content path string true "Barcode content" MinLength(1)
|
|
// @Param width query int false "Barcode width" Minimum(1) Maximum(10000) default(1000)
|
|
// @Param height query int false "Barcode height" Minimum(1) Maximum(10000) default(1000)
|
|
// @Param padding query int false "Padding around the barcode (included in image size)" Minimum(0) Maximum(100) default(10)
|
|
// @Produce image/png
|
|
// @Router /v1/barcodes/{type}/{content} [get]
|
|
func (h *DefaultHandler) GenerateBarcode(c *fiber.Ctx) error {
|
|
|
|
logger := h.Logger.Named("GenerateBarcode")
|
|
|
|
// Get the type and content from the URL
|
|
barcodeType := c.Params("type")
|
|
barcodeContent := c.Params("content")
|
|
|
|
// Get the width and height from the query parameters
|
|
widthStr := c.Query("width", "1000")
|
|
heightStr := c.Query("height", "1000")
|
|
paddingStr := c.Query("padding", "10")
|
|
|
|
// Convert width and height to integers
|
|
width, err := strconv.Atoi(widthStr)
|
|
if err != nil {
|
|
logger.Errorw("Invalid width parameter", "width", widthStr, "error", err)
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
|
"error": "Invalid width parameter",
|
|
})
|
|
}
|
|
|
|
height, err := strconv.Atoi(heightStr)
|
|
if err != nil {
|
|
logger.Errorw("Invalid height parameter", "height", heightStr, "error", err)
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
|
"error": "Invalid height parameter",
|
|
})
|
|
}
|
|
|
|
padding, err := strconv.Atoi(paddingStr)
|
|
if err != nil {
|
|
logger.Errorw("Invalid padding parameter", "padding", paddingStr, "error", err)
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
|
"error": "Invalid padding parameter",
|
|
})
|
|
}
|
|
logger = logger.With("type", barcodeType, "content", barcodeContent, "width", width, "height", height, "padding", padding)
|
|
|
|
// Generate the barcode
|
|
logger.Info("Generating barcode")
|
|
barcode, err := h.BarcodeService.GenerateBarcode(barcodeType, barcodeContent, width, height, padding)
|
|
if err != nil {
|
|
logger.Errorw("Failed to generate barcode", "error", err)
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
|
"error": err.Error(),
|
|
})
|
|
}
|
|
logger.Info("Barcode generated")
|
|
|
|
c.Set(fiber.HeaderContentType, "image/png")
|
|
return c.Send(barcode.Bytes())
|
|
}
|