up
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/sujit-baniya/fiber-boilerplate/pkg/models"
|
||||
"github.com/sujit-baniya/fiber-boilerplate/pkg/services"
|
||||
)
|
||||
|
||||
// getOSSFromBody creates an OSSProvider from the oss_api_id in the request body.
|
||||
// If oss_api_id is missing, it falls back to the last active config (legacy behavior).
|
||||
func getOSSFromBody(c *fiber.Ctx) (services.OSSProvider, error) {
|
||||
var req struct {
|
||||
OssAPIID *uint `json:"oss_api_id"`
|
||||
}
|
||||
if err := json.Unmarshal(c.Body(), &req); err != nil {
|
||||
req.OssAPIID = nil
|
||||
}
|
||||
|
||||
if req.OssAPIID != nil {
|
||||
return services.NewOSSProviderByID(*req.OssAPIID)
|
||||
}
|
||||
return services.NewOSSProviderFromDB()
|
||||
}
|
||||
|
||||
// getOSSFromQuery creates an OSSProvider from the oss_api_id query param.
|
||||
// If missing, falls back to last active config.
|
||||
func getOSSFromQuery(c *fiber.Ctx) (services.OSSProvider, error) {
|
||||
id := c.QueryInt("oss_api_id", 0)
|
||||
if id > 0 {
|
||||
return services.NewOSSProviderByID(uint(id))
|
||||
}
|
||||
|
||||
lastActive, err := models.GetLastActiveOssApi()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return services.NewOSSProvider(lastActive)
|
||||
}
|
||||
@@ -65,14 +65,14 @@ func CreateQr(c *fiber.Ctx) error {
|
||||
defer os.Remove(tmpPath)
|
||||
|
||||
// 5. Subir a OSS
|
||||
ossService, err := services.NewOSSServiceFromDB()
|
||||
ossProvider, err := getOSSFromBody(c)
|
||||
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 {
|
||||
if err := ossProvider.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",
|
||||
@@ -80,7 +80,7 @@ func CreateQr(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
// 6. URL del archivo
|
||||
url := fmt.Sprintf("https://%s.%s/%s", ossService.BucketName, ossService.Endpoint, fileName)
|
||||
url := ossProvider.PublicURL(fileName)
|
||||
|
||||
// 7. Buscar si ya existe un registro con ese vcard_id
|
||||
var existing models.QrVcard
|
||||
@@ -110,9 +110,9 @@ func CreateQr(c *fiber.Ctx) error {
|
||||
}
|
||||
} else {
|
||||
// Ya existe, eliminar el QR anterior en OSS
|
||||
oldFilePath := strings.Replace(existing.Qr, fmt.Sprintf("https://%s.%s/", ossService.BucketName, ossService.Endpoint), "", 1)
|
||||
oldFilePath := strings.Replace(existing.Qr, ossProvider.PublicURL(""), "", 1)
|
||||
|
||||
if err := ossService.DeleteFile(oldFilePath); err != nil {
|
||||
if err := ossProvider.DeleteFile(oldFilePath); err != nil {
|
||||
log.Printf("Error al eliminar QR anterior en OSS: %v", err)
|
||||
// No retornamos error, porque queremos continuar de todas formas
|
||||
}
|
||||
@@ -224,14 +224,14 @@ func CreateUrlQr(c *fiber.Ctx) error {
|
||||
defer os.Remove(tmpPath)
|
||||
|
||||
// 5. Subir a OSS
|
||||
ossService, err := services.NewOSSServiceFromDB()
|
||||
ossProvider, err := getOSSFromBody(c)
|
||||
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 {
|
||||
if err := ossProvider.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",
|
||||
@@ -239,8 +239,7 @@ func CreateUrlQr(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
// 6. URL del archivo
|
||||
url := fmt.Sprintf("https://%s.%s/%s", ossService.BucketName, ossService.Endpoint, fileName)
|
||||
|
||||
url := ossProvider.PublicURL(fileName)
|
||||
|
||||
|
||||
// 8. Retornar la URL
|
||||
|
||||
@@ -46,14 +46,14 @@ func CreateVcf(c *fiber.Ctx) error {
|
||||
defer os.Remove(tmpPath)
|
||||
|
||||
// 3. Subir a OSS
|
||||
ossService, err := services.NewOSSServiceFromDB()
|
||||
ossProvider, err := getOSSFromBody(c)
|
||||
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 {
|
||||
if err := ossProvider.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",
|
||||
@@ -61,7 +61,7 @@ func CreateVcf(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
// 4. URL del archivo
|
||||
url := fmt.Sprintf("https://%s.%s/%s", ossService.BucketName, ossService.Endpoint, fileName)
|
||||
url := ossProvider.PublicURL(fileName)
|
||||
|
||||
// 5. Buscar si ya existe un registro con ese vcard_id
|
||||
var existing models.VcfVcard
|
||||
|
||||
Reference in New Issue
Block a user