Skip to content

A self-hosted media server and file management system

fix: subtitle path does not rely on a single point

aminnausin46f74d521465
diff --git a/app/Models/Subtitle.php b/app/Models/Subtitle.php
index d7127887..eb23a127 100644
--- a/app/Models/Subtitle.php
+++ b/app/Models/Subtitle.php
@@ -2,6 +2,7 @@
namespace App\Models;
+use App\Services\Subtitles\SubtitlePath;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
@@ -17,4 +18,12 @@ class Subtitle extends Model {
public function metadata(): BelongsTo {
return $this->belongsTo(Metadata::class, 'uuid', 'metadata_uuid');
}
+
+ public function getDirectoryPath(): string {
+ return SubtitlePath::directory($this->metadata_uuid);
+ }
+
+ public function getFilePath(string $format): string {
+ return SubtitlePath::file($this->metadata_uuid, $this->track_id, $format);
+ }
}
diff --git a/app/Services/Subtitles/SubtitleExtractor.php b/app/Services/Subtitles/SubtitleExtractor.php
index f2e18f0a..5540878c 100644
--- a/app/Services/Subtitles/SubtitleExtractor.php
+++ b/app/Services/Subtitles/SubtitleExtractor.php
@@ -6,6 +6,7 @@ use App\Models\Metadata;
use App\Models\Subtitle;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
+use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
class SubtitleExtractor {
@@ -20,18 +21,14 @@ class SubtitleExtractor {
'ffmpeg',
'-y',
'-i',
- Storage::disk('public')->path($mediaPath), // Media is on public disk for now
+ $mediaPath, // Media is on public disk for now does not need storage disk, path already has storage in it?
'-map',
"0:$subtitle->track_id",
Storage::disk('local')->path($outputPath),
];
$process = new Process($command);
- $process->run();
-
- if (! $process->isSuccessful()) {
- throw new \Exception('Subtitles Failed: "' . implode(' ', $command) . '"');
- }
+ $process->mustRun();
$subtitle->update([
'path' => $outputPath,
@@ -39,20 +36,26 @@ class SubtitleExtractor {
]);
return $outputPath;
- } catch (\Throwable $th) {
- Log::error('Unable to get file subtitles', ['error' => $th->getMessage()]);
+ } catch (ProcessFailedException $th) {
+ Log::error('Subtitle extraction failed', [
+ 'subtitle_id' => $subtitle->id,
+ 'track_id' => $subtitle->track_id,
+ 'metadata_uuid' => $subtitle->metadata_uuid,
+ 'command' => $th->getProcess()->getCommandLine(),
+ 'exit_code' => $th->getProcess()->getExitCode(),
+ 'error' => $th->getProcess()->getErrorOutput(),
+ ]);
throw $th;
}
}
/**
- * Get the relative output path given the subtitle and file extention
+ * Given a Subtitle row and file extention, ensure the relative output directory exists and get the relative output path.
*/
private function getOutputPath(Subtitle $subtitle, string $ext) {
- $relativeOutputDir = "data/media/{$subtitle->metadata_uuid}/subtitles";
- Storage::disk('local')->makeDirectory($relativeOutputDir); // Ensure directory exists
+ Storage::disk('local')->makeDirectory($subtitle->getDirectoryPath()); // Ensure directory exists
- return "{$relativeOutputDir}/{$subtitle->track_id}.{$ext}"; // get final output address
+ return $subtitle->getFilePath($ext);
}
private function getExtentionFromCodec(string $codec): string {
@@ -61,8 +64,6 @@ class SubtitleExtractor {
'ass' => 'ass',
'ssa' => 'ssa',
'webvtt' => 'vtt',
- 'hdmv_pgs_subtitle' => 'sup',
- 'dvd_subtitle' => 'sub',
default => 'bin',
};
}
diff --git a/app/Services/Subtitles/SubtitlePath.php b/app/Services/Subtitles/SubtitlePath.php
new file mode 100644
index 00000000..4991a6a9
--- /dev/null
+++ b/app/Services/Subtitles/SubtitlePath.php
@@ -0,0 +1,17 @@
+<?php
+
+namespace App\Services\Subtitles;
+
+class SubtitlePath {
+ private const BASE_PATH = 'metadata/media';
+
+ public static function directory(string $metadataUuid): string {
+ $shard = substr($metadataUuid, 0, 2);
+
+ return self::BASE_PATH . "/{$shard}/{$metadataUuid}/subtitles";
+ }
+
+ public static function file(string $metadataUuid, int $trackId, string $format): string {
+ return self::directory($metadataUuid) . "/{$trackId}.{$format}";
+ }
+}
diff --git a/app/Services/Subtitles/SubtitleResolver.php b/app/Services/Subtitles/SubtitleResolver.php
index 74651cc7..6a626a68 100644
--- a/app/Services/Subtitles/SubtitleResolver.php
+++ b/app/Services/Subtitles/SubtitleResolver.php
@@ -3,6 +3,7 @@
namespace App\Services\Subtitles;
use App\Models\Metadata;
+use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
@@ -11,36 +12,41 @@ class SubtitleResolver {
public function __construct(protected SubtitleExtractor $extractor, protected SubtitleFormatter $formatter) {}
public function resolveSubtitles(Metadata $metadata, int $track, string $format): BinaryFileResponse {
- $subtitle = $metadata->subtitles()->where('track_id', $track)->firstOrFail(); // 404 on no result
+ try {
+ $subtitle = $metadata->subtitles()->where('track_id', $track)->firstOrFail(); // 404 on no result
+
+ $format = ltrim(strtolower($format), '.');
+ $requestedPath = $subtitle->getFilePath($format);
+
+ if ($this->fileExists($requestedPath)) {
+ return Response::file(
+ Storage::disk('local')->path($requestedPath)
+ );
+ }
+
+ if (! $subtitle->path || ! $this->fileExists($subtitle->path)) {
+ $this->extractor->extractStream($metadata, $subtitle);
+ $subtitle->refresh();
+ }
+
+ $nativePath = $subtitle->path;
+ $convertedPath = $this->formatter->convert(
+ $nativePath,
+ $requestedPath,
+ $format
+ );
- $format = ltrim(strtolower($format), '.');
- $requestedPath = "data/media/{$subtitle->metadata_uuid}/subtitles/{$track}.{$format}";
+ if (! $this->fileExists($convertedPath)) {
+ abort(500);
+ }
- if ($this->fileExists($requestedPath)) {
return Response::file(
- Storage::disk('local')->path($requestedPath)
+ Storage::disk('local')->path($convertedPath)
);
+ } catch (\Throwable $th) {
+ Log::error('Subtitle Resolver Failed', ['error' => $th->getMessage()]);
+ throw $th;
}
-
- if (! $subtitle->path || ! $this->fileExists($subtitle->path)) {
- $this->extractor->extractStream($metadata, $subtitle); // Native format subtitle file (usually srt)
- $subtitle->refresh();
- }
-
- $nativePath = $subtitle->path;
- $convertedPath = $this->formatter->convert(
- $nativePath,
- $requestedPath,
- $format
- );
-
- if (! $this->fileExists($convertedPath)) {
- abort(500);
- }
-
- return Response::file(
- Storage::disk('local')->path($convertedPath)
- );
}
private function fileExists(string $path): bool {
diff --git a/app/Services/Subtitles/SubtitleScanner.php b/app/Services/Subtitles/SubtitleScanner.php
index 2b72be5f..12515466 100644
--- a/app/Services/Subtitles/SubtitleScanner.php
+++ b/app/Services/Subtitles/SubtitleScanner.php
@@ -2,7 +2,6 @@
namespace App\Services\Subtitles;
-use App\Models\Metadata;
use Illuminate\Support\Facades\Log;
// Per Metadata Row