A self-hosted media server and file management system
Vue ffmpegfile-serverjellyfinlaravelmediamedia-playermedia-servermediabrowsermediaservermusicmusic-librarymusic-playermusicplayerself-hostedvuejs
fix: missing comment headers
aminnausin87cfcd2ebf2a
diff --git a/app/Models/Subtitle.php b/app/Models/Subtitle.phpindex 1fa9bb70..24619f32 100644--- a/app/Models/Subtitle.php+++ b/app/Models/Subtitle.php@@ -25,11 +25,11 @@ class Subtitle extends Model {}public function getDirectoryPath(): string {- return SubtitlePath::directory($this->metadata_uuid);+ return SubtitlePath::buildDirectory($this->metadata_uuid);}public function getFilePath(string $format, ?string $language = null): string {- return SubtitlePath::file($this->metadata_uuid, $this->track_id, $format, $language);+ return SubtitlePath::buildFilePath($this->metadata_uuid, $this->track_id, $format, $language);}public static function getVisibleFields(): array {diff --git a/app/Services/Subtitles/SubtitleFormatter.php b/app/Services/Subtitles/SubtitleFormatter.phpindex 86b03b9f..6c235db0 100644--- a/app/Services/Subtitles/SubtitleFormatter.php+++ b/app/Services/Subtitles/SubtitleFormatter.php@@ -6,6 +6,13 @@ use App\Services\Subtitles\Formats\VttStrategy;use Illuminate\Support\Facades\Storage;class SubtitleFormatter {+ /**+ * Convert a local subtitle file to another format+ *+ * @param string $input Relative local disk path of the input file+ * @param string $output Relative local disk path of the output file+ * @param string $outputFormat Output file format+ */public function convert(string $input, string $output, string $outputFormat): string {if (! Storage::disk('local')->exists($input)) {throw new \RuntimeException("Input file not found: {$input}");@@ -22,6 +29,12 @@ class SubtitleFormatter {return $output;}+ /**+ * Matches a formatting strategy given a file format+ * (Only supports vtt)+ *+ * @param string $outputFormat File format+ */protected function matchStrategy(string $outputFormat) {return match ($outputFormat) {'vtt' => new VttStrategy,diff --git a/app/Services/Subtitles/SubtitlePath.php b/app/Services/Subtitles/SubtitlePath.phpindex acfd2812..5cdb079a 100644--- a/app/Services/Subtitles/SubtitlePath.php+++ b/app/Services/Subtitles/SubtitlePath.php@@ -5,15 +5,30 @@ namespace App\Services\Subtitles;class SubtitlePath {private const BASE_PATH = 'metadata/media';- public static function directory(string $metadataUuid): string {+ /**+ * Builds the ideal subtitle directory for a given metadata item+ *+ * @param string $metadataUuid metadata uuid+ * @return string The ideal subtitle directory in the format /{shard}/{uuid}/subtitles+ */+ public static function buildDirectory(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 $language = null): string {+ /**+ * Builds the ideal file path for a given subtitle track+ *+ * @param string $metadataUuid Related metadata uuid+ * @param int $trackId Track index+ * @param string $format File format+ * @param ?string $language Language name (ex/ en, fr, ru, jp)+ * @return string The ideal subtitle file path in the format /{shard}/{uuid}/subtitles/{trackId}.{language}.{format}+ */+ public static function buildFilePath(string $metadataUuid, int $trackId, string $format, ?string $language = null): string {$languageSlug = $language ? ".{$language}" : '';- return self::directory($metadataUuid) . "/{$trackId}{$languageSlug}.{$format}";+ return self::buildDirectory($metadataUuid) . "/{$trackId}{$languageSlug}.{$format}";}}diff --git a/app/Services/Subtitles/SubtitleResolver.php b/app/Services/Subtitles/SubtitleResolver.phpindex d37788e6..8b5e2d86 100644--- a/app/Services/Subtitles/SubtitleResolver.php+++ b/app/Services/Subtitles/SubtitleResolver.php@@ -3,10 +3,12 @@namespace App\Services\Subtitles;use App\Models\Metadata;+use App\Models\Subtitle;use Illuminate\Database\Eloquent\ModelNotFoundException;use Illuminate\Support\Facades\Log;use Illuminate\Support\Facades\Response;use Illuminate\Support\Facades\Storage;+use InvalidArgumentException;use Symfony\Component\HttpFoundation\BinaryFileResponse;class SubtitleResolver {@@ -17,6 +19,7 @@ class SubtitleResolver {$subtitle = $metadata->subtitles()->where('track_id', $track)->firstOrFail(); // 404 on no result$format = ltrim(strtolower($format), '.');+$requestedPath = $subtitle->getFilePath($format);if ($this->fileExists($requestedPath)) {@@ -29,6 +32,11 @@ class SubtitleResolver {abort(404);}+ if ($subtitle->stream === 0 && $subtitle->external_source_path && ! $subtitle->path) {+ $this->resolveExternalSubtitle($subtitle);+ $subtitle->refresh();+ }+if (! $subtitle->path || ! $this->fileExists($subtitle->path)) {$this->extractor->extractStream($metadata, $subtitle);$subtitle->refresh();@@ -56,6 +64,28 @@ class SubtitleResolver {}}+ /**+ * Copies an external subtitle file to the internal folder following the track.language.ext pattern+ */+ private function resolveExternalSubtitle(Subtitle $subtitle): void {+ $sourcePath = $subtitle->external_source_path;+ if (! $sourcePath) {+ throw new InvalidArgumentException;+ }+ $ext = pathinfo($sourcePath, PATHINFO_EXTENSION);+ $outputPath = $subtitle->getFilePath($ext, $subtitle->language);++ Storage::disk('local')->put(+ $outputPath,+ Storage::disk('public')->get("$sourcePath")+ );++ $subtitle->update([+ 'path' => $outputPath,+ 'format' => $ext,+ ]);+ }+private function fileExists(string $path): bool {return Storage::disk('local')->exists($path);}diff --git a/tests/Unit/Services/Subtitles/SubtitlePathTest.php b/tests/Unit/Services/Subtitles/SubtitlePathTest.phpindex 67d06474..691ceabb 100644--- a/tests/Unit/Services/Subtitles/SubtitlePathTest.php+++ b/tests/Unit/Services/Subtitles/SubtitlePathTest.php@@ -9,7 +9,7 @@ use Tests\TestCase;class SubtitlePathTest extends TestCase {public function test_get_file_path_returns_correct_path() {$uuid = Str::uuid()->toString();- $path = SubtitlePath::file($uuid, 2, 'vtt');+ $path = SubtitlePath::buildFilePath($uuid, 2, 'vtt');$this->assertEquals('metadata/media/' . substr($uuid, 0, 2) . "/{$uuid}/subtitles/2.vtt", $path);}@@ -19,7 +19,7 @@ class SubtitlePathTest extends TestCase {$this->assertEquals('metadata/media/' . substr($uuid, 0, 2) . "/{$uuid}/subtitles",- SubtitlePath::directory($uuid)+ SubtitlePath::buildDirectory($uuid));}}