This commit is contained in:
cangui 2025-06-20 21:03:02 +02:00
parent e98ef798d3
commit 5492ca3453

View File

@ -710,16 +710,17 @@ 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 // HandleAddJobsMultiple gère le débridage de plusieurs liens, auto-création de sous-dossier, et enregistrement
func HandleAddJobsMultiple(db *gorm.DB) http.HandlerFunc { func HandleAddJobsMultiple(db *gorm.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
// 1. Parsing form
if err := r.ParseForm(); err != nil { if err := r.ParseForm(); err != nil {
http.Error(w, "Requête invalide", http.StatusBadRequest) http.Error(w, "Requête invalide", http.StatusBadRequest)
return return
} }
// Récupère les liens // 2. Récupération des liens
raw := r.FormValue("links") raw := r.FormValue("links")
lines := strings.Split(raw, "\n") lines := strings.Split(raw, "\n")
// Récupère le dossier principal // 3. Récupération du dossier principal
idStr := r.FormValue("path_id") idStr := r.FormValue("path_id")
baseID, err := strconv.ParseInt(idStr, 10, 64) baseID, err := strconv.ParseInt(idStr, 10, 64)
if err != nil { if err != nil {
@ -732,7 +733,7 @@ func HandleAddJobsMultiple(db *gorm.DB) http.HandlerFunc {
return return
} }
// Prépare client DebridLink // 4. Préparation client DebridLink
ctx := context.Background() ctx := context.Background()
client := debridlink.NewClient(db) client := debridlink.NewClient(db)
account := download.GetFirstActiveAccount(client) account := download.GetFirstActiveAccount(client)
@ -742,13 +743,14 @@ func HandleAddJobsMultiple(db *gorm.DB) http.HandlerFunc {
} }
client.SetAccount(account) client.SetAccount(account)
// Boucle sur chaque lien // 5. Itération sur chaque lien à débrider
for _, link := range lines { for _, link := range lines {
link = strings.TrimSpace(link) link = strings.TrimSpace(link)
if link == "" { if link == "" {
continue continue
} }
// Débridage link -> liens réels
links, err := client.AddLink(ctx, link) links, err := client.AddLink(ctx, link)
if err != nil { if err != nil {
log.Printf("Échec débridage de %s: %v", link, err) log.Printf("Échec débridage de %s: %v", link, err)
@ -756,47 +758,56 @@ func HandleAddJobsMultiple(db *gorm.DB) http.HandlerFunc {
} }
for _, l := range links { for _, l := range links {
// Détermine le nom de la série // 5a. Détermination automatique du nom de série
name := sanitizeFileName(l.Name) clean := sanitizeFileName(l.Name)
series := "" series := clean
if m := seriesRegex.FindStringSubmatch(name); len(m) == 2 { if m := seriesRegex.FindStringSubmatch(clean); len(m) == 2 {
series = m[1] series = m[1]
} }
// Définit dossier cible et ID // 5b. Assignation du PathID
assignPathID := basePath.ID assignID := int(basePath.ID)
finalDir := basePath.Path
if series != "" { if series != "" {
dirPath := filepath.Join(basePath.Path, series) dirPath := filepath.Join(basePath.Path, series)
if err := os.MkdirAll(dirPath, os.ModePerm); err != nil { if err := os.MkdirAll(dirPath, os.ModePerm); err != nil {
log.Printf("Erreur création dossier %s: %v", dirPath, err) log.Printf("Erreur création dossier %s: %v", dirPath, err)
} }
// Cherche ou crée en base
// Cherche ou crée l'enregistrement PathDownload var sub models.PathDownload
var subPath models.PathDownload if err := db.Where("path = ?", dirPath).First(&sub).Error; err != nil {
if err := db.Where("path = ?", dirPath).First(&subPath).Error; err != nil {
if err == gorm.ErrRecordNotFound { if err == gorm.ErrRecordNotFound {
subPath = models.PathDownload{Path: dirPath, PathName: series} sub = models.PathDownload{Path: dirPath, PathName: series}
if err := db.Create(&subPath).Error; err != nil { if err := db.Create(&sub).Error; err != nil {
log.Printf("Erreur création PathDownload: %v", err) log.Printf("Erreur création PathDownload: %v", err)
} }
} else { } else {
log.Printf("Erreur lecture PathDownload: %v", err) log.Printf("Erreur lecture PathDownload: %v", err)
} }
} }
assignPathID = subPath.ID assignID = int(sub.ID)
finalDir = dirPath
} }
// Enregistrement du job // 5c. Transcodage éventuel
streamInfo, err := client.CreateTranscode(ctx, l.ID)
if err != nil {
log.Printf("Erreur transcode pour %s: %v", l.ID, err)
}
// 5d. Enregistrement du job
job := &download.DownloadJob{ job := &download.DownloadJob{
ID: fmt.Sprintf("job-%d", time.Now().UnixNano()), ID: fmt.Sprintf("job-%d", time.Now().UnixNano()),
Link: l.DownloadURL, Link: l.DownloadURL,
Name: l.Name, Name: l.Name,
Status: "waiting", Status: "waiting",
PathID: assignPathID, PathID: assignID,
Directory: finalDir, Size: l.Size,
Host: l.Host,
Progress: 0,
StreamURL: "",
}
if streamInfo != nil {
job.StreamURL = streamInfo.StreamURL
} }
if err := download.RegisterJobWithDB(job, db); err != nil { if err := download.RegisterJobWithDB(job, db); err != nil {
log.Printf("Erreur enregistrement job: %v", err) log.Printf("Erreur enregistrement job: %v", err)
@ -805,7 +816,7 @@ func HandleAddJobsMultiple(db *gorm.DB) http.HandlerFunc {
} }
} }
// Notifie le front // 6. Notification au frontend
download.Broadcast() download.Broadcast()
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
} }