2025-08-04 17:45:44 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2025-08-22 10:11:07 +02:00
|
|
|
"backend/queries"
|
2025-08-21 12:46:02 +02:00
|
|
|
"backend/typedefs"
|
2025-08-04 17:45:44 +02:00
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type ImageRefManager struct {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewImageRefManager() *ImageRefManager {
|
|
|
|
|
return &ImageRefManager{}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-22 10:11:07 +02:00
|
|
|
// func (i *ImageRefManager) GetImagesRef() ([]typedefs.MiogestImagesRef_Table, error) {
|
|
|
|
|
// return queries.ImagesGetAll()
|
|
|
|
|
// }
|
2025-08-04 17:45:44 +02:00
|
|
|
|
2025-08-21 12:46:02 +02:00
|
|
|
func (i *ImageRefManager) InsertImagesRef(annunci *typedefs.AnnunciXML) error {
|
2025-08-04 17:45:44 +02:00
|
|
|
|
2025-08-22 10:11:07 +02:00
|
|
|
return queries.ImagesBatchInsert(annunci)
|
2025-08-04 17:45:44 +02:00
|
|
|
}
|
|
|
|
|
|
2025-08-21 12:46:02 +02:00
|
|
|
func (i *ImageRefManager) FindIsUpdate(annunci *typedefs.AnnunciXML) ([]string, error) {
|
2025-08-22 10:11:07 +02:00
|
|
|
r, err := queries.ImagesGetAll()
|
2025-08-04 17:45:44 +02:00
|
|
|
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(imgbasepath, codice)
|
|
|
|
|
err := os.RemoveAll(folder)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (i *ImageRefManager) Reset() error {
|
2025-08-22 10:11:07 +02:00
|
|
|
err := queries.ImagesClear()
|
2025-08-04 17:45:44 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-22 10:11:07 +02:00
|
|
|
err = queries.AnnunciResetImages()
|
2025-08-04 17:45:44 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = os.RemoveAll(imgbasepath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
err = os.Mkdir(imgbasepath, 0755)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|