Преглед на файлове

feat: allow requeueing files that haven't been processed before

- Remove requirement for existing file record in requeue endpoint
- Requeue now works for any file in a configured dataset
- Automatically determines output path based on dataset configuration
- Creates file record if it doesn't exist
- Enables requeueing files regardless of previous processing status
Timothy Pomeroy преди 1 месец
родител
ревизия
6c803d28ae
променени са 1 файла, в които са добавени 68 реда и са изтрити 23 реда
  1. 68 23
      apps/service/src/app.controller.ts

+ 68 - 23
apps/service/src/app.controller.ts

@@ -7,6 +7,7 @@ import {
   Post,
   Query,
 } from '@nestjs/common';
+import * as path from 'path';
 import { AppService } from './app.service';
 import { EventsGateway } from './events.gateway';
 
@@ -130,31 +131,74 @@ export class AppController {
     @Param('file') file: string,
     @Body('preset') preset?: string,
   ) {
-    const fileRecord = this.appService.findFile(dataset, file) as FileRecord;
-    if (!fileRecord) {
-      throw new Error(`File not found: ${dataset}/${file}`);
-    }
-
-    // Get dataset configuration to find the preset for this dataset
+    // Get dataset configuration to find the preset and output configuration
     const datasetConfig = this.appService.getDatasetConfig();
     let processingPreset = preset;
+    let output: string;
+
+    // Try to find existing file record first
+    const fileRecord = this.appService.findFile(dataset, file) as FileRecord;
+
+    if (fileRecord) {
+      // Use existing file record
+      output = fileRecord.output;
+    } else {
+      // No existing file record - determine output path from dataset config
+      let destination: string | undefined;
+      let ext = '.mkv'; // Default extension
+
+      if (datasetConfig[dataset]) {
+        const datasetObj = datasetConfig[dataset];
+
+        // Find the path configuration that matches this file
+        for (const pathKey of Object.keys(datasetObj)) {
+          if (pathKey !== 'enabled' && file.startsWith(pathKey)) {
+            const pathConfig = datasetObj[pathKey];
+            if (pathConfig) {
+              if (pathConfig.preset && !processingPreset) {
+                processingPreset = pathConfig.preset;
+              }
+              if (pathConfig.destination) {
+                destination = pathConfig.destination;
+              }
+              if (pathConfig.ext) {
+                ext = pathConfig.ext.startsWith('.')
+                  ? pathConfig.ext
+                  : '.' + pathConfig.ext;
+              }
+            }
+            break;
+          }
+        }
 
-    if (!processingPreset && datasetConfig[dataset]) {
-      // Find the preset for this dataset by looking at the first path's preset
-      const datasetObj = datasetConfig[dataset];
-      for (const pathKey of Object.keys(datasetObj)) {
-        if (
-          pathKey !== 'enabled' &&
-          datasetObj[pathKey] &&
-          datasetObj[pathKey].preset
-        ) {
-          processingPreset = datasetObj[pathKey].preset;
-          break;
+        // Fallback to old format
+        if (!processingPreset && datasetObj.preset) {
+          processingPreset = datasetObj.preset;
+        }
+        if (!destination && datasetObj.destination) {
+          destination = datasetObj.destination;
         }
+        if (ext === '.mkv' && datasetObj.ext) {
+          ext = datasetObj.ext.startsWith('.')
+            ? datasetObj.ext
+            : '.' + datasetObj.ext;
+        }
+      }
+
+      // Determine output path
+      if (destination) {
+        const fileName = path.basename(file, path.extname(file));
+        output = path.join(destination, fileName + ext);
+      } else {
+        // Default: same directory with new extension
+        output = path.join(
+          path.dirname(file),
+          path.basename(file, path.extname(file)) + ext,
+        );
       }
     }
 
-    // Fallback to default preset if no dataset-specific preset found
+    // Fallback to default preset if no preset found
     if (!processingPreset) {
       processingPreset = 'Fast 1080p30';
     }
@@ -162,17 +206,18 @@ export class AppController {
     // Create a task for processing
     const task = this.appService.createTask({
       dataset,
-      input: fileRecord.input,
-      output: fileRecord.output,
+      input: file,
+      output: output,
       preset: processingPreset,
       priority: 1, // Higher priority for manual requeues
       status: 'requeued', // Mark as manually requeued
     });
 
-    // Update file status to pending
+    // Update file status to pending (this will create the file record if it doesn't exist)
     this.appService.setFile(dataset, file, {
       status: 'pending',
       date: new Date().toISOString(),
+      output: output,
     });
 
     // Emit file update
@@ -188,8 +233,8 @@ export class AppController {
       type: 'created',
       taskId: task.id,
       task: 'handbrake',
-      input: fileRecord.input,
-      output: fileRecord.output,
+      input: file,
+      output: output,
       preset: processingPreset,
     });