up
This commit is contained in:
parent
0f21d346a5
commit
a9bf4d7403
@ -1,38 +1,81 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"canguidev/shelfy/internal/models"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"gorm.io/gorm"
|
"path/filepath"
|
||||||
"canguidev/shelfy/query"
|
|
||||||
|
|
||||||
|
"gorm.io/gorm"
|
||||||
|
|
||||||
|
"canguidev/shelfy/internal/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateDefaultFolder(db *gorm.DB) {
|
func CreateDefaultFolder(db *gorm.DB) {
|
||||||
folders := []string{"Film", "Série", "Manga","Magazine"}
|
type item struct {
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
defs := []item{
|
||||||
|
{Name: "Film"},
|
||||||
|
{Name: "Série"},
|
||||||
|
{Name: "Manga"},
|
||||||
|
{Name: "Magazine"},
|
||||||
|
}
|
||||||
|
|
||||||
for _, name := range folders {
|
for _, it := range defs {
|
||||||
path := "upload/" + name
|
path := filepath.Join("upload", it.Name)
|
||||||
|
|
||||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
// 1) Dossier : on s'assure qu'il existe
|
||||||
err := os.MkdirAll(path, 0755) // MkdirAll au cas où des dossiers parents manquent
|
if err := os.MkdirAll(path, 0o755); err != nil {
|
||||||
if err != nil {
|
log.Printf("[FOLDER] Erreur création '%s' : %v", path, err)
|
||||||
fmt.Printf("Erreur lors de la création du dossier %s : %v\n", path, err)
|
continue
|
||||||
} else {
|
}
|
||||||
pathDownload := models.PathDownload{
|
|
||||||
Path: path,
|
// 2) Base : chercher une ligne existante par path OU path_name
|
||||||
PathName: name,
|
var existing models.PathDownload
|
||||||
}
|
err := db.Where("path = ? OR path_name = ?", path, it.Name).First(&existing).Error
|
||||||
q := query.Use(db)
|
|
||||||
if err := q.PathDownload.Create(&pathDownload); err != nil {
|
switch {
|
||||||
fmt.Printf(`{"error": "Failed to create path %%s\n"}`, err)
|
case errors.Is(err, gorm.ErrRecordNotFound):
|
||||||
return
|
// 2a) Pas trouvé -> on crée
|
||||||
}
|
row := models.PathDownload{
|
||||||
fmt.Printf("Dossier créé : %s\n", path)
|
Path: path,
|
||||||
|
PathName: it.Name,
|
||||||
}
|
}
|
||||||
} else {
|
if err := db.Create(&row).Error; err != nil {
|
||||||
fmt.Printf("Dossier déjà existant : %s\n", path)
|
log.Printf("[DB] Échec création PathDownload(%s, %s) : %v", path, it.Name, err)
|
||||||
|
} else {
|
||||||
|
log.Printf("[DB] Ligne créée PathDownload id=%v (%s | %s)", row.ID, row.PathName, row.Path)
|
||||||
|
}
|
||||||
|
|
||||||
|
case err != nil:
|
||||||
|
// 2b) Erreur DB
|
||||||
|
log.Printf("[DB] Erreur recherche PathDownload(%s | %s) : %v", path, it.Name, err)
|
||||||
|
|
||||||
|
default:
|
||||||
|
// 2c) Trouvé -> on normalise si besoin
|
||||||
|
updates := map[string]interface{}{}
|
||||||
|
if existing.Path != path {
|
||||||
|
updates["path"] = path
|
||||||
|
}
|
||||||
|
if existing.PathName != it.Name {
|
||||||
|
updates["path_name"] = it.Name
|
||||||
|
}
|
||||||
|
if len(updates) > 0 {
|
||||||
|
if err := db.Model(&existing).Updates(updates).Error; err != nil {
|
||||||
|
log.Printf("[DB] Échec mise à jour PathDownload id=%v : %v", existing.ID, err)
|
||||||
|
} else {
|
||||||
|
log.Printf("[DB] Mis à jour PathDownload id=%v -> %#v", existing.ID, updates)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.Printf("[DB] OK PathDownload id=%v déjà synchro (%s | %s)", existing.ID, existing.PathName, existing.Path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3) (Facultatif) log côté fichiers
|
||||||
|
if fi, err := os.Stat(path); err == nil && fi.IsDir() {
|
||||||
|
fmt.Printf("[FOLDER] OK : %s\n", path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user