infoalloggi-monorepo/apps/backend/image_handler.go
Marco Pedone 1ac2d4b3ab Refactor storage handlers to use centralized configuration
- Updated `fs.go` and `minio.go` to replace hardcoded storage paths with values from the new config package.
- Introduced a new `config` package to manage application configuration, including database and storage settings.
- Removed the `models.go` file from `typedefs` as it was no longer needed.
- Added a new `config.go` file to handle loading environment variables and application settings.
- Created a new `types.go` file in `typedefs` to define storage strategies.
- Updated `storage_handler.go` to utilize the new configuration structure.
2025-08-22 12:57:33 +02:00

100 lines
1.9 KiB
Go

package main
import (
"backend/config"
"backend/queries"
"backend/typesdefs"
"fmt"
"log"
"os"
"path/filepath"
)
type ImageRefManager struct {
}
func NewImageRefManager() *ImageRefManager {
return &ImageRefManager{}
}
func (i *ImageRefManager) InsertImagesRef(annunci *typesdefs.AnnunciXML) error {
return queries.ImagesBatchInsert(annunci)
}
func (i *ImageRefManager) FindIsUpdate(annunci *typesdefs.AnnunciXML) ([]string, error) {
r, err := queries.ImagesGetAll()
if err != nil {
return nil, err
}
refMap := make(map[string][]string) // Convert slice to map for faster lookup
for _, ref := range r {
if ref.Foto != nil {
refMap[ref.Codice] = *ref.Foto
}
}
var result []string
for _, annuncio := range annunci.Annuncio {
if annuncio.Codice != nil && annuncio.Foto != nil {
refFoto, exists := refMap[*annuncio.Codice]
if !exists {
fmt.Println("new codice", *annuncio.Codice)
result = append(result, *annuncio.Codice)
continue
}
if len(*annuncio.Foto) != len(refFoto) {
fmt.Println("Different length", *annuncio.Foto, refFoto)
result = append(result, *annuncio.Codice)
continue
}
for i, foto := range *annuncio.Foto {
if foto != refFoto[i] {
fmt.Println("Different content", *annuncio.Foto, refFoto)
result = append(result, *annuncio.Codice)
break
}
}
}
}
return result, nil
}
func (i *ImageRefManager) ClearImages(to_clear []string) error {
for _, codice := range to_clear {
folder := filepath.Join(config.Cfg.Images.Path, codice)
err := os.RemoveAll(folder)
if err != nil {
return err
}
}
return nil
}
func (i *ImageRefManager) Reset() error {
err := queries.ImagesClear()
if err != nil {
return err
}
err = queries.AnnunciResetImages()
if err != nil {
return err
}
err = os.RemoveAll(config.Cfg.Images.Path)
if err != nil {
log.Fatal(err)
}
err = os.Mkdir(config.Cfg.Images.Path, 0755)
if err != nil {
log.Fatal(err)
}
return nil
}