Update vcf_service.go

This commit is contained in:
Lizandro Guarnizo
2025-04-28 20:57:32 -05:00
parent 98d77f4b9a
commit e9f63bead3
+28 -2
View File
@@ -1,7 +1,11 @@
package services
import (
"encoding/base64"
"fmt"
"io"
"log"
"net/http"
"strings"
"time"
)
@@ -36,7 +40,6 @@ type ContactDataVcf struct {
VcardID string `json:"vcard_id"`
}
// GenerateVCardVcf genera el contenido de una vCard extendida como archivo .vcf (sin QR)
func GenerateVCardVcf(data ContactDataVcf) ([]byte, error) {
now := time.Now().UTC().Format("2006-01-02T15:04:05Z")
@@ -70,8 +73,12 @@ func GenerateVCardVcf(data ContactDataVcf) ([]byte, error) {
vcardBuilder.WriteString(fmt.Sprintf("URL:%s\n", data.Website))
}
if data.PhotoURL != "" {
vcardBuilder.WriteString(fmt.Sprintf("PHOTO;VALUE=URI:%s\n", data.PhotoURL))
err := agregarFotoWebp(&vcardBuilder, data.PhotoURL)
if err != nil {
log.Println("Error agregando la foto:", err)
}
}
if data.Address != "" {
vcardBuilder.WriteString(fmt.Sprintf("ADR:%s\n", data.Address))
}
@@ -117,3 +124,22 @@ func GenerateVCardVcf(data ContactDataVcf) ([]byte, error) {
return []byte(vcardBuilder.String()), nil
}
func agregarFotoWebp(vcardBuilder *strings.Builder, photoURL string) error {
resp, err := http.Get(photoURL)
if err != nil {
return fmt.Errorf("error descargando imagen: %w", err)
}
defer resp.Body.Close()
imageBytes, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("error leyendo imagen: %w", err)
}
base64Image := base64.StdEncoding.EncodeToString(imageBytes)
// Opcional: insertar saltos de línea cada 75 caracteres si quieres seguir la RFC, pero no es obligatorio
vcardBuilder.WriteString(fmt.Sprintf("PHOTO;ENCODING=b;TYPE=WEBP:%s\n", base64Image))
return nil
}