From 037d44ed0e26c71c3ceba3e1adae0cf8da0c883d Mon Sep 17 00:00:00 2001 From: cangui Date: Fri, 20 Jun 2025 20:20:48 +0200 Subject: [PATCH] up --- renders/renders.go | 72 ++++++++++++++++------ templates/godownloader_download.pages.tmpl | 2 - 2 files changed, 52 insertions(+), 22 deletions(-) diff --git a/renders/renders.go b/renders/renders.go index d3f87c1..08ae83e 100644 --- a/renders/renders.go +++ b/renders/renders.go @@ -704,6 +704,9 @@ func DetailHandler(w http.ResponseWriter, r *http.Request) { }) } +var seriesRegex = regexp.MustCompile(`^(.+?)\.S\d{2}E\d{2}`) + +// HandleAddJobsMultiple gère le débridage de plusieurs liens, auto-création de sous-dossier, et enregistrement func HandleAddJobsMultiple(db *gorm.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if err := r.ParseForm(); err != nil { @@ -728,18 +731,6 @@ func HandleAddJobsMultiple(db *gorm.DB) http.HandlerFunc { return } - // Création d'un sous-dossier si renseigné - sub := strings.TrimSpace(r.FormValue("new_subfolder")) - finalDir := basePath.Path - if sub != "" { - finalDir = filepath.Join(basePath.Path, sanitizeFileName(sub)) - if err := os.MkdirAll(finalDir, os.ModePerm); err != nil { - log.Printf("Erreur création dossier: %v", err) - http.Error(w, "Impossible de créer le sous-dossier", http.StatusInternalServerError) - return - } - } - client := debridlink.NewClient(db) account := download.GetFirstActiveAccount(client) if account == nil { @@ -748,31 +739,69 @@ func HandleAddJobsMultiple(db *gorm.DB) http.HandlerFunc { } client.SetAccount(account) + // Itérer sur chaque lien à débrider for _, link := range lines { link = strings.TrimSpace(link) if link == "" { continue } - // Débride chaque lien + + // Débride le lien links, err := client.AddLink(context.Background(), link) if err != nil { - log.Printf("Echec débridage pour %s: %v", link, err) + log.Printf("Échec débridage pour %s: %v", link, err) continue } for _, l := range links { - // Création transcode si nécessaire + // Détermination automatique du sous-dossier via regex + name := sanitizeFileName(l.Name) + series := "" + if m := seriesRegex.FindStringSubmatch(name); len(m) == 2 { + series = m[1] + } + + // FinalDir selon détection ou création manuelle + finalDir := basePath.Path + var assignPathID uint = basePath.ID + + if series != "" { + // créer sous-dossier pour cette série + dirName := sanitizeFileName(series) + dirPath := filepath.Join(basePath.Path, dirName) + if err := os.MkdirAll(dirPath, os.ModePerm); err != nil { + log.Printf("Erreur création dossier série %s: %v", dirName, err) + } else { + // vérifier/ajouter en base PathDownload + var pathRec models.PathDownload + if err := db.Where("path = ?", dirPath).First(&pathRec).Error; err != nil { + if err == gorm.ErrRecordNotFound { + pathRec = models.PathDownload{Name: series, Path: dirPath} + if err := db.Create(&pathRec).Error; err != nil { + log.Printf("Erreur création PathDownload en base: %v", err) + } + } else { + log.Printf("Erreur lecture PathDownload: %v", err) + } + } + assignPathID = pathRec.ID + finalDir = dirPath + } + } + + // Création transcode si besoin streamInfo, err := client.CreateTranscode(context.Background(), l.ID) if err != nil { log.Println("Erreur transcode:", err) } + // Enregistrement du job job := &download.DownloadJob{ ID: l.ID, Link: l.DownloadURL, Name: l.Name, Status: "waiting", - PathID: id, + PathID: assignPathID, Size: l.Size, Host: l.Host, Progress: 0, @@ -793,15 +822,18 @@ func HandleAddJobsMultiple(db *gorm.DB) http.HandlerFunc { renderPartial(w, "downloads_table", data) } } + +// sanitizeFileName utilise la même logique que download.SanitizeFileName +func sanitizeFileName(name string) string { + return download.SanitizeFileName(name) +} + +// getAllPaths renvoie tous les PathDownload func getAllPaths(db *gorm.DB) []*models.PathDownload { var paths []*models.PathDownload db.Order("name").Find(&paths) return paths } -func sanitizeFileName(name string) string { - // même regex que download.sanitizeFileName - return download.SanitizeFileName(name) -} diff --git a/templates/godownloader_download.pages.tmpl b/templates/godownloader_download.pages.tmpl index 5a3a60d..81d7ee4 100644 --- a/templates/godownloader_download.pages.tmpl +++ b/templates/godownloader_download.pages.tmpl @@ -23,9 +23,7 @@
- {{ template "downloads_table" . }}
-{{ end }}