Skip to content

Commit f5fa393

Browse files
committed
Enhance: Update TasksHandler to support directory scanning for video files and integrate fileService for improved task creation.
1 parent ccdf217 commit f5fa393

4 files changed

Lines changed: 86 additions & 14 deletions

File tree

app.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ func (a *App) startHTTPServer() error {
277277

278278
// Initialize API handlers
279279
filesHandler := api.NewFilesHandler(fileService, ffmpegService)
280-
tasksHandler := api.NewTasksHandler(a.db, a.workerPool)
280+
tasksHandler := api.NewTasksHandler(a.db, a.workerPool, fileService)
281281
presetsHandler := api.NewPresetsHandler(a.db)
282282
hardwareHandler := api.NewHardwareHandler(hardwareService)
283283
settingsHandler := api.NewSettingsHandler(a.db.Conn())

cmd/server/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func main() {
5656

5757
// Initialize API handlers
5858
filesHandler := api.NewFilesHandler(fileService, ffmpegService)
59-
tasksHandler := api.NewTasksHandler(db, workerPool)
59+
tasksHandler := api.NewTasksHandler(db, workerPool, fileService)
6060
presetsHandler := api.NewPresetsHandler(db)
6161
hardwareHandler := api.NewHardwareHandler(hardwareService)
6262
settingsHandler := api.NewSettingsHandler(db.Conn())
@@ -122,7 +122,7 @@ func main() {
122122
router.Static("/assets", "./web/assets")
123123
router.StaticFile("/logo.png", "./web/logo.png")
124124
router.StaticFile("/logo.svg", "./web/logo.svg")
125-
125+
126126
// Serve index.html for root and handle SPA routing
127127
router.StaticFile("/", "./web/index.html")
128128
router.NoRoute(func(c *gin.Context) {

internal/api/tasks.go

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package api
33
import (
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
2021
type 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
3437
type 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-

internal/service/file_service.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,50 @@ func isVideoFile(filename string) bool {
207207

208208
return false
209209
}
210+
211+
// IsDirectory checks if a path is a directory
212+
func (fs *FileService) IsDirectory(path string) bool {
213+
info, err := os.Stat(path)
214+
if err != nil {
215+
return false
216+
}
217+
return info.IsDir()
218+
}
219+
220+
// ScanVideoFilesInDirectory recursively scans a directory for video files
221+
// Returns absolute paths to all video files found
222+
func (fs *FileService) ScanVideoFilesInDirectory(dirPath string) ([]string, error) {
223+
var videoFiles []string
224+
225+
// Walk the directory tree
226+
err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
227+
if err != nil {
228+
return err // Skip files/dirs we can't access
229+
}
230+
231+
// Skip hidden files and directories
232+
if strings.HasPrefix(info.Name(), ".") {
233+
if info.IsDir() {
234+
return filepath.SkipDir
235+
}
236+
return nil
237+
}
238+
239+
// Add video files to the list
240+
if !info.IsDir() && isVideoFile(info.Name()) {
241+
absPath, err := filepath.Abs(path)
242+
if err != nil {
243+
absPath = path
244+
}
245+
videoFiles = append(videoFiles, absPath)
246+
}
247+
248+
return nil
249+
})
250+
251+
if err != nil {
252+
return nil, fmt.Errorf("failed to scan directory: %w", err)
253+
}
254+
255+
return videoFiles, nil
256+
}

0 commit comments

Comments
 (0)