diff --git a/rest/controllers/api/vcard_qr.go b/rest/controllers/api/vcard_qr.go index 8ccd536..98765f3 100644 --- a/rest/controllers/api/vcard_qr.go +++ b/rest/controllers/api/vcard_qr.go @@ -8,6 +8,7 @@ import ( "image/png" "log" "os" + "strings" "github.com/chai2010/webp" "github.com/gofiber/fiber/v2" @@ -107,18 +108,27 @@ func CreateQr(c *fiber.Ctx) error { "error": "Error al consultar la base de datos", }) } - } else { - // Ya existe, actualizar - existing.Qr = url - existing.UsuarioID = data.FirstName - existing.Email = data.Email - - if err := models.UpdateQrVcard(&existing); err != nil { - return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ - "error": "Error al actualizar el QR en la base de datos", - }) + } else { + // Ya existe, eliminar el QR anterior en OSS + oldFilePath := strings.Replace(existing.Qr, fmt.Sprintf("https://%s.%s/", ossService.BucketName, ossService.Endpoint), "", 1) + + if err := ossService.DeleteFile(oldFilePath); err != nil { + log.Printf("Error al eliminar QR anterior en OSS: %v", err) + // No retornamos error, porque queremos continuar de todas formas + } + + // Actualizar el registro + existing.Qr = url + existing.UsuarioID = data.FirstName + existing.Email = data.Email + + if err := models.UpdateQrVcard(&existing); err != nil { + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ + "error": "Error al actualizar el QR en la base de datos", + }) + } } - } + // 8. Retornar la URL return c.JSON(fiber.Map{