up
This commit is contained in:
parent
0f21d346a5
commit
a9bf4d7403
@ -1,38 +1,81 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"canguidev/shelfy/internal/models"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"gorm.io/gorm"
|
||||
"canguidev/shelfy/query"
|
||||
"path/filepath"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"canguidev/shelfy/internal/models"
|
||||
)
|
||||
|
||||
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 {
|
||||
path := "upload/" + name
|
||||
for _, it := range defs {
|
||||
path := filepath.Join("upload", it.Name)
|
||||
|
||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||
err := os.MkdirAll(path, 0755) // MkdirAll au cas où des dossiers parents manquent
|
||||
if err != nil {
|
||||
fmt.Printf("Erreur lors de la création du dossier %s : %v\n", path, err)
|
||||
} else {
|
||||
pathDownload := models.PathDownload{
|
||||
// 1) Dossier : on s'assure qu'il existe
|
||||
if err := os.MkdirAll(path, 0o755); err != nil {
|
||||
log.Printf("[FOLDER] Erreur création '%s' : %v", path, err)
|
||||
continue
|
||||
}
|
||||
|
||||
// 2) Base : chercher une ligne existante par path OU path_name
|
||||
var existing models.PathDownload
|
||||
err := db.Where("path = ? OR path_name = ?", path, it.Name).First(&existing).Error
|
||||
|
||||
switch {
|
||||
case errors.Is(err, gorm.ErrRecordNotFound):
|
||||
// 2a) Pas trouvé -> on crée
|
||||
row := models.PathDownload{
|
||||
Path: path,
|
||||
PathName: name,
|
||||
PathName: it.Name,
|
||||
}
|
||||
q := query.Use(db)
|
||||
if err := q.PathDownload.Create(&pathDownload); err != nil {
|
||||
fmt.Printf(`{"error": "Failed to create path %%s\n"}`, err)
|
||||
return
|
||||
if err := db.Create(&row).Error; err != nil {
|
||||
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)
|
||||
}
|
||||
fmt.Printf("Dossier créé : %s\n", 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 {
|
||||
fmt.Printf("Dossier déjà existant : %s\n", path)
|
||||
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