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