@@ -3,6 +3,7 @@ package api
33import (
44 "ffmpeg-web/internal/database"
55 "ffmpeg-web/internal/model"
6+ "ffmpeg-web/internal/service"
67 "net/http"
78 "time"
89
@@ -18,22 +19,24 @@ type WorkerPool interface {
1819
1920// TasksHandler handles task-related API requests
2021type TasksHandler struct {
21- db * database.DB
22- pool WorkerPool
22+ db * database.DB
23+ pool WorkerPool
24+ fileService * service.FileService
2325}
2426
2527// NewTasksHandler creates a new tasks handler
26- func NewTasksHandler (db * database.DB , pool WorkerPool ) * TasksHandler {
28+ func NewTasksHandler (db * database.DB , pool WorkerPool , fileService * service. FileService ) * TasksHandler {
2729 return & TasksHandler {
28- db : db ,
29- pool : pool ,
30+ db : db ,
31+ pool : pool ,
32+ fileService : fileService ,
3033 }
3134}
3235
3336// CreateTaskRequest represents a request to create a new task
3437type CreateTaskRequest struct {
35- SourceFiles []string `json:"sourceFiles" binding:"required"`
36- Preset string `json:"preset,omitempty"`
38+ SourceFiles []string `json:"sourceFiles" binding:"required"`
39+ Preset string `json:"preset,omitempty"`
3740 Config * model.TranscodeConfig `json:"config,omitempty"`
3841}
3942
@@ -67,9 +70,32 @@ func (h *TasksHandler) CreateTask(c *gin.Context) {
6770 return
6871 }
6972
73+ // Expand directories to video files
74+ // This allows selecting folders and having all videos within transcoded
75+ var allSourceFiles []string
76+ for _ , path := range req .SourceFiles {
77+ if h .fileService .IsDirectory (path ) {
78+ // Scan directory for video files
79+ videoFiles , err := h .fileService .ScanVideoFilesInDirectory (path )
80+ if err != nil {
81+ c .JSON (http .StatusBadRequest , gin.H {"error" : "failed to scan directory: " + path })
82+ return
83+ }
84+ allSourceFiles = append (allSourceFiles , videoFiles ... )
85+ } else {
86+ allSourceFiles = append (allSourceFiles , path )
87+ }
88+ }
89+
90+ // Check if any files were found
91+ if len (allSourceFiles ) == 0 {
92+ c .JSON (http .StatusBadRequest , gin.H {"error" : "no video files found in selected paths" })
93+ return
94+ }
95+
7096 // Create tasks for each source file
71- tasks := make ([]* model.Task , 0 , len (req . SourceFiles ))
72- for _ , sourceFile := range req . SourceFiles {
97+ tasks := make ([]* model.Task , 0 , len (allSourceFiles ))
98+ for _ , sourceFile := range allSourceFiles {
7399 task := & model.Task {
74100 ID : uuid .New ().String (),
75101 SourceFile : sourceFile ,
@@ -184,7 +210,7 @@ func (h *TasksHandler) CancelTask(c *gin.Context) {
184210 task .Status = model .TaskStatusCancelled
185211 task .Error = "Task cancelled (not running in worker pool)"
186212 task .CompletedAt = & now
187-
213+
188214 if updateErr := h .db .UpdateTask (task ); updateErr != nil {
189215 c .JSON (http .StatusInternalServerError , gin.H {"error" : "failed to cancel task" })
190216 return
@@ -193,4 +219,3 @@ func (h *TasksHandler) CancelTask(c *gin.Context) {
193219
194220 c .JSON (http .StatusOK , gin.H {"message" : "task cancelled" })
195221}
196-
0 commit comments