Update vcard_qr.go
This commit is contained in:
@@ -173,6 +173,7 @@ func CreateQrTmp(c *fiber.Ctx) error {
|
||||
func CreateUrlQr(c *fiber.Ctx) error {
|
||||
var data struct {
|
||||
URL string `json:"url"`
|
||||
Nombre string `json:"unico"`
|
||||
}
|
||||
if err := json.Unmarshal(c.Body(), &data); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
@@ -202,6 +203,85 @@ func CreateUrlQr(c *fiber.Ctx) error {
|
||||
})
|
||||
}
|
||||
|
||||
// 3. Convertir a WebP
|
||||
var webpBuf bytes.Buffer
|
||||
if err := webp.Encode(&webpBuf, img, nil); err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"error": "Error al convertir QR a WebP",
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
fileName := fmt.Sprintf("qrs/url/qr-%s.webp", strings.ReplaceAll(data.Nombre, " ", "_") )
|
||||
tmpPath := fmt.Sprintf("/tmp/%s.webp", data.Nombre)
|
||||
|
||||
// 4. Guardar archivo temporal
|
||||
if err := os.WriteFile(tmpPath, webpBuf.Bytes(), 0644); err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"error": "No se pudo guardar el QR temporalmente",
|
||||
})
|
||||
}
|
||||
defer os.Remove(tmpPath)
|
||||
|
||||
// 5. Subir a OSS
|
||||
ossService, err := services.NewOSSServiceFromDB()
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"error": "Error de conexión con OSS",
|
||||
})
|
||||
}
|
||||
|
||||
if err := ossService.UploadFile(fileName, tmpPath); err != nil {
|
||||
log.Printf("Error subiendo archivo a OSS: %v", err)
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"error": "Error subiendo archivo a OSS",
|
||||
})
|
||||
}
|
||||
|
||||
// 6. URL del archivo
|
||||
url := fmt.Sprintf("https://%s.%s/%s", ossService.BucketName, ossService.Endpoint, fileName)
|
||||
|
||||
|
||||
|
||||
// 8. Retornar la URL
|
||||
return c.JSON(fiber.Map{
|
||||
"url": url,
|
||||
})
|
||||
}
|
||||
|
||||
func createCorreoQr(c *fiber.Ctx) error {
|
||||
var data struct {
|
||||
Correo string `json:"correo"`
|
||||
}
|
||||
if err := json.Unmarshal(c.Body(), &data); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
"error": "Datos inválidos",
|
||||
})
|
||||
}
|
||||
|
||||
qrBytes, err := services.GenerateCorreoQR(data.Correo)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"error": "No se pudo generar el QR",
|
||||
})
|
||||
}
|
||||
|
||||
img, err := png.Decode(bytes.NewReader(qrBytes))
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"error": "No se pudo procesar la imagen",
|
||||
})
|
||||
}
|
||||
|
||||
// Codificar a WebP
|
||||
var webpBuffer bytes.Buffer
|
||||
if err := webp.Encode(&webpBuffer, img, &webp.Options{Lossless: true}); err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"error": "No se pudo convertir a WebP",
|
||||
})
|
||||
}
|
||||
|
||||
c.Type("webp")
|
||||
return c.Send(webpBuffer.Bytes())
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user