This commit is contained in:
cangui 2025-06-20 20:20:48 +02:00
parent ebb2db9e39
commit 037d44ed0e
2 changed files with 52 additions and 22 deletions

View File

@ -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)
}

View File

@ -23,9 +23,7 @@
<!-- Tableau des jobs -->
<div id="downloads-table">
{{ template "downloads_table" . }}
</div>
{{ end }}
<div
hx-ext="sse"
sse-connect="/api/download/stream"