infoalloggi-monorepo/apps/backend/utils/utils.go
Marco Pedone 208deeac28 Refactor image handling in announcement components
- Updated CardAnnuncio and related components to use a new images structure instead of url_immagini.
- Removed references to url_immagini and replaced them with images array containing img and thumb properties.
- Adjusted API responses to include images in the new format.
- Modified middleware and storage service to accommodate new image handling.
- Cleaned up unused code and schemas related to old image references.
- Updated Docker configuration for storage service.
2025-10-27 17:58:42 +01:00

84 lines
1.5 KiB
Go

package utils
import (
"encoding/xml"
"fmt"
"io"
"log"
"net/http"
"os"
"reflect"
"strings"
)
func Ptr[T any](v T) *T { return &v }
func MakeUppercase(s string) string {
if s != "" && reflect.TypeOf(s).Kind() == reflect.String && len(s) > 0 {
return strings.ToUpper(string(s[0])) + s[1:]
}
return ""
}
func fetchXML(url string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return []byte{}, fmt.Errorf("GET error: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return []byte{}, fmt.Errorf("status error: %v", resp.StatusCode)
}
data, err := io.ReadAll(resp.Body)
if err != nil {
return []byte{}, fmt.Errorf("read body: %v", err)
}
return data, nil
}
func GetDataXML[T any](url string) T {
xmlBytes, err := fetchXML(url)
if err != nil {
log.Fatalf("Failed to get XML: %v", err)
panic(err)
}
var result T
err = xml.Unmarshal(xmlBytes, &result)
if err != nil {
log.Fatalf("Failed to unmarshal XML: %v", err)
panic(err)
}
return result
}
func SafeRemoveAll(path string) error {
err := os.RemoveAll(path)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
return nil
}
func GetStringValue(strPtr *string) string {
if strPtr != nil {
return *strPtr
}
return ""
}
func RemoveWhitespace(str string) string {
return strings.ReplaceAll(str, " ", "")
}
func CommaToDot(str *string) *string {
if str != nil {
result := strings.ReplaceAll(*str, ",", ".")
return &result
}
return nil
}