Skip to content

A self-hosted media server and file management system

feat: reset/build fonts api

aminnausin3c7703dbcb43
diff --git a/app/Data/Fonts/ExtractedFontData.php b/app/Data/Fonts/ExtractedFontData.php
new file mode 100644
index 00000000..89fd5aa0
--- /dev/null
+++ b/app/Data/Fonts/ExtractedFontData.php
@@ -0,0 +1,15 @@
+<?php
+
+namespace App\Data\Fonts;
+
+final readonly class ExtractedFontData {
+ public function __construct(
+ public string $metadataUuid,
+ public string $fileName,
+ public ?string $codec,
+ public string $mimeType,
+ public int $size,
+ public string $path,
+ public string $hash,
+ ) {}
+}
diff --git a/app/Data/Fonts/FontAttachmentData.php b/app/Data/Fonts/FontAttachmentData.php
new file mode 100644
index 00000000..57b205a0
--- /dev/null
+++ b/app/Data/Fonts/FontAttachmentData.php
@@ -0,0 +1,12 @@
+<?php
+
+namespace App\Data\Fonts;
+
+final readonly class FontAttachmentData {
+ public function __construct(
+ public int $streamIndex,
+ public string $fileName,
+ public ?string $codec,
+ public string $mimeType,
+ ) {}
+}
diff --git a/app/Http/Controllers/Api/V1/Metadata/FontController.php b/app/Http/Controllers/Api/V1/Metadata/FontController.php
new file mode 100644
index 00000000..75072ecf
--- /dev/null
+++ b/app/Http/Controllers/Api/V1/Metadata/FontController.php
@@ -0,0 +1,58 @@
+<?php
+
+namespace App\Http\Controllers\Api\V1\Metadata;
+
+use App\Enums\TaskStatus;
+use App\Http\Controllers\Controller;
+use App\Http\Resources\Metadata\FontResource;
+use App\Models\Font;
+use App\Models\Metadata;
+use App\Models\SubTask;
+use App\Services\FileJobService;
+use Illuminate\Support\Facades\Auth;
+use Illuminate\Support\Facades\Gate;
+use Illuminate\Support\Facades\Storage;
+
+class FontController extends Controller {
+ public function __construct(protected FileJobService $fileJobService) {}
+
+ /**
+ * List storyboard details for metadata.
+ */
+ public function show(Metadata $metadata) {
+ return FontResource::collection($metadata->fonts);
+ }
+
+ public function regenerate(Metadata $metadata) {
+ $isAdmin = Gate::allows('admin');
+
+ $rateLimitError = ! $isAdmin ? $this->getRateLimitError() : null;
+ if ($rateLimitError) {
+ return $rateLimitError;
+ }
+
+ if (! $isAdmin) {
+ $this->hitRateLimits();
+ }
+
+ $alreadyRunning = SubTask::where('reference_uuid', $metadata->uuid)
+ ->where('reference_type', Font::class)
+ ->whereIn('status', [TaskStatus::PENDING, TaskStatus::PROCESSING])
+ ->latest()
+ ->first();
+
+ if ($alreadyRunning) {
+ return response()->json(['message' => 'Font extraction already in progress', 'task_id' => $alreadyRunning->task_id], 409);
+ }
+
+ $FontDir = 'metadata/' . substr($metadata->uuid, 0, 2) . "/{$metadata->uuid}/fonts";
+ $metadata->fonts()->update([
+ 'path' => null,
+ ]);
+ Storage::disk('public')->deleteDirectory($FontDir);
+
+ $task = $this->fileJobService->regenerateFonts(Auth::id(), $metadata);
+
+ return response()->json(['task_id' => $task->id], 202);
+ }
+}
diff --git a/app/Jobs/Metadata/ExtractFonts.php b/app/Jobs/Metadata/ExtractFonts.php
new file mode 100644
index 00000000..529b0b01
--- /dev/null
+++ b/app/Jobs/Metadata/ExtractFonts.php
@@ -0,0 +1,140 @@
+<?php
+
+namespace App\Jobs\Metadata;
+
+use App\Enums\TaskStatus;
+use App\Jobs\ManagedSubTask;
+use App\Jobs\VerifyFiles;
+use App\Models\Font;
+use App\Models\Metadata;
+use App\Models\SubTask;
+use App\Services\Fonts\FontExtractionService;
+use App\Services\TaskService;
+use Carbon\Carbon;
+use Illuminate\Contracts\Filesystem\FileNotFoundException;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Log;
+
+class ExtractFonts extends ManagedSubTask {
+ protected string $filePath;
+
+ protected string $uuid;
+
+ public int $tries = 1;
+
+ public int $backoff = 0;
+
+ /**
+ * Create a new job instance.
+ *
+ * @return void
+ */
+ public function __construct(string $filePath, string $uuid, int $taskId) {
+ $this->filePath = $filePath;
+ $this->uuid = $uuid;
+
+ $subTask = SubTask::create([
+ 'task_id' => $taskId,
+ 'status' => TaskStatus::PENDING,
+ 'name' => 'Extract embedded fonts from ' . basename(dirname($filePath)) . '/' . basename($filePath),
+ 'reference_uuid' => $uuid,
+ 'reference_type' => Font::class,
+ ]);
+
+ $this->taskId = $taskId;
+ $this->subTaskId = $subTask->id;
+ }
+
+ /**
+ * Execute the job.
+ */
+ public function handle(TaskService $taskService, FontExtractionService $extractor): void {
+ if (! $this->beginSubTask($taskService, "Extracting fonts from $this->filePath")) {
+ return;
+ }
+
+ try {
+ $summary = $this->handleExtractFonts($extractor);
+ $this->completeSubTask($taskService, $summary);
+ } catch (\Throwable $th) {
+ Log::warning('Font extraction failed', [
+ 'uuid' => $this->uuid,
+ 'file' => basename($this->filePath),
+ 'error' => $th->getMessage(),
+ 'trace' => $th->getTraceAsString(),
+ ]);
+
+ $this->updateFontsScannedAt(null);
+ $this->failSubTask($taskService, $th);
+ // Don't stop subsequent jobs if this one fails (so don't throw)
+ if ($this->batch()->totalJobs === 1) {
+ throw $th;
+ } // Throw on reset fonts which is for one file
+ }
+ }
+
+ private function handleExtractFonts(FontExtractionService $extractor): string {
+ $metadata = Metadata::where('uuid', $this->uuid)->firstOrFail();
+ $metadata->load('video');
+
+ $mediaPath = VerifyFiles::getAbsoluteMediaPath($metadata->video);
+ if (! file_exists($mediaPath)) {
+ throw new FileNotFoundException("File not found: {$mediaPath}");
+ }
+
+ $metadata->raw_metadata = $this->verifyMetadata($metadata, $mediaPath);
+
+ $start = microtime(true);
+
+ $attachments = $extractor->parseFontAttachments($metadata);
+ $extractedFonts = $extractor->extractFonts(
+ mediaPath: $mediaPath,
+ uuid: $metadata->uuid,
+ attachments: $attachments,
+ );
+
+ $extractedFonts = $extractor->deduplicateFonts($extractedFonts);
+
+ $timeElapsed = round(microtime(true) - $start, 2);
+
+ DB::transaction(function () use ($metadata, $extractor, $extractedFonts) {
+ $extractor->upsertFontData($extractedFonts);
+ $extractor->clearDeletedFonts($metadata);
+ $this->updateFontsScannedAt(now());
+ });
+
+ return 'Extracted ' . count($extractedFonts) . " embedded fonts for {$this->uuid} in {$timeElapsed}s";
+ }
+
+ /**
+ * Updates storyboard scanned flag on metadata based on the uuid
+ *
+ * @param ?Carbon $time Timestamp saved to metadata (null on error)
+ */
+ private function updateFontsScannedAt(?Carbon $time) {
+ Metadata::where('uuid', $this->uuid)->update([
+ 'fonts_scanned_at' => $time,
+ ]);
+ }
+
+ // Persist raw metadata
+ private function verifyMetadata(Metadata $metadata, string $filePath): array {
+ if (! empty($metadata->raw_metadata)) {
+ return $metadata->raw_metadata;
+ }
+
+ Log::warning('raw_metadata missing for updated file in extract fonts', [
+ 'uuid' => $metadata->uuid,
+ 'title' => $metadata->title,
+ 'file_path' => $filePath,
+ ]);
+
+ $rawMetadata = VerifyFiles::getFileMetadata($filePath, 'Extract Fonts'); // fallback but don't update metadata due to separation of concerns?
+
+ $metadata->update([
+ 'raw_metadata' => $rawMetadata,
+ ]);
+
+ return $rawMetadata;
+ }
+}
diff --git a/app/Jobs/Utility/Subtitles/ScanSubtitles.php b/app/Jobs/Utility/Subtitles/ScanSubtitles.php
index effd4dd9..1513d3a0 100644
--- a/app/Jobs/Utility/Subtitles/ScanSubtitles.php
+++ b/app/Jobs/Utility/Subtitles/ScanSubtitles.php
@@ -150,7 +150,7 @@ class ScanSubtitles extends ManagedSubTask {
}
}
- private function verifyMetadata($metadata, $filePath): array {
+ private function verifyMetadata(Metadata $metadata, string $filePath): array {
$raw_metadata = $metadata->raw_metadata;
if (empty($raw_metadata)) {
diff --git a/app/Jobs/VerifyFiles.php b/app/Jobs/VerifyFiles.php
index ded9f4c7..309a0289 100755
--- a/app/Jobs/VerifyFiles.php
+++ b/app/Jobs/VerifyFiles.php
@@ -476,7 +476,7 @@ class VerifyFiles extends ManagedSubTask {
}
}
- public static function getFileMetadata($filePath, $reason = 'und') {
+ public static function getFileMetadata(string $filePath, $reason = 'und') {
try {
// ? FFMPEG module with 6 test folders takes 35+ seconds but running the commands through shell takes 18 seconds
diff --git a/app/Services/FileJobService.php b/app/Services/FileJobService.php
index bdf87630..f43b0319 100644
--- a/app/Services/FileJobService.php
+++ b/app/Services/FileJobService.php
@@ -8,6 +8,7 @@ use App\Jobs\EmbedUidInMetadata;
use App\Jobs\GeneratePreviewImage;
use App\Jobs\IndexFiles;
use App\Jobs\Maintenance\PurgeStaleGuestData;
+use App\Jobs\Metadata\ExtractFonts;
use App\Jobs\Metadata\GenerateStoryboard;
use App\Jobs\SyncFiles;
use App\Jobs\Utility\Paths\CleanFolderPaths;
@@ -334,6 +335,27 @@ class FileJobService {
);
}
+ public function regenerateFonts(int $userId, Metadata $metadata): Task {
+ $name = 'Regenerate Fonts';
+ $description = 'Re-extracts embedded fonts for ' . $metadata->composite_id;
+
+ return $this->executeBatchOperation(
+ userId: $userId,
+ name: $name,
+ description: $description,
+ chain: function ($task) use ($metadata) {
+ return [
+ new ExtractFonts(
+ filePath: VerifyFiles::getAbsoluteMediaPath($metadata->video),
+ uuid: $metadata->uuid,
+ taskId: $task->id,
+ ),
+ ];
+ },
+ queue: 'encode'
+ );
+ }
+
public function executeBatchOperation(
?int $userId,
string $name,
diff --git a/app/Services/Fonts/FontExtractionService.php b/app/Services/Fonts/FontExtractionService.php
new file mode 100644
index 00000000..30e30ad6
--- /dev/null
+++ b/app/Services/Fonts/FontExtractionService.php
@@ -0,0 +1,181 @@
+<?php
+
+namespace App\Services\Fonts;
+
+use App\Data\Fonts\ExtractedFontData;
+use App\Data\Fonts\FontAttachmentData;
+use App\Exceptions\FFmpegException;
+use App\Models\Font;
+use App\Models\Metadata;
+use Illuminate\Support\Facades\Log;
+use Illuminate\Support\Facades\Storage;
+use RuntimeException;
+use Symfony\Component\Process\Exception\ProcessFailedException;
+use Symfony\Component\Process\Process;
+
+class FontExtractionService {
+ /**
+ * Extracts fronts from from a media file and ensures the output exists after extraction.
+ *
+ * @param string $mediaPath absolute path to media file
+ * @param FontAttachmentData[] $attachments
+ * @return ExtractedFontData[]
+ */
+ public function extractFonts(string $uuid, string $mediaPath, array $attachments): array {
+ $extractionStart = microtime(true);
+ $timings = [];
+ $extractedFonts = [];
+
+ try {
+ $shard = substr($uuid, 0, 2);
+
+ $disk = Storage::disk('public');
+ $relativeDir = "metadata/metadata/{$shard}/{$uuid}/fonts/";
+ $disk->makeDirectory($relativeDir);
+
+ if (! $disk->exists($relativeDir)) {
+ throw new RuntimeException("Failed to create font directory: {$relativeDir}");
+ }
+
+ $absoluteDir = str_replace('\\', '/', $disk->path($relativeDir));
+
+ foreach ($attachments as $attachment) {
+ $relativePath = "{$relativeDir}{$attachment->fileName}";
+ $outputPath = "{$absoluteDir}{$attachment->fileName}";
+
+ $commandStart = microtime(true);
+ $command = [
+ 'ffmpeg',
+ "-dump_attachment:{$attachment->streamIndex}",
+ $outputPath,
+ '-i',
+ "file:{$mediaPath}",
+ '-t',
+ '0',
+ '-f',
+ 'null',
+ 'null',
+ ];
+
+ $timings['build_command'] = microtime(true) - $commandStart;
+
+ $ffmpegStart = microtime(true);
+ $process = new Process($command);
+ $process->mustRun();
+ $timings['ffmpeg_execution'] = microtime(true) - $ffmpegStart;
+
+ $verifyStart = microtime(true);
+ if (! $disk->exists($relativePath)) {
+ throw new \RuntimeException("Font attachment was not extracted: {$attachment->fileName}");
+ }
+ $timings['verify_output'] = microtime(true) - $verifyStart;
+
+ $extractedFonts[] = new ExtractedFontData(
+ metadataUuid: $uuid,
+ fileName: $attachment->fileName,
+ codec: $attachment->codec ? strtolower($attachment->codec) : null,
+ mimeType: $attachment->mimeType,
+ size: filesize($outputPath),
+ path: $relativePath,
+ hash: hash_file('sha256', $outputPath),
+ );
+ }
+
+ $timings['total_extraction'] = microtime(true) - $extractionStart;
+
+ return $extractedFonts;
+ } catch (ProcessFailedException $e) {
+ $timings['total_extraction'] = microtime(true) - $extractionStart;
+ Log::error('Font extraction failed (process)', [
+ 'metadata_uuid' => $uuid,
+ 'command' => $e->getProcess()->getCommandLine(),
+ 'exit_code' => $e->getProcess()->getExitCode(),
+ 'error' => $e->getProcess()->getErrorOutput(),
+ 'timings_ms' => array_map(fn ($t) => round($t * 1000, 2), $timings),
+ ]);
+ throw new FFmpegException('FFmpeg failed: ' . $e->getProcess()->getErrorOutput());
+ } catch (\Throwable $th) {
+ $timings['total_extraction'] = microtime(true) - $extractionStart;
+ Log::error('Font extraction failed (general)', [
+ 'metadata_uuid' => $uuid,
+ 'error' => $th->getMessage(),
+ 'timings_ms' => array_map(fn ($t) => round($t * 1000, 2), $timings),
+ ]);
+ throw $th;
+ }
+ }
+
+ /**
+ * Parse the embedded font attachments from metadata.
+ *
+ * @return FontAttachmentData[]
+ */
+ public function parseFontAttachments(Metadata $metadata): array {
+ if ($metadata->raw_metadata === null) {
+ return [];
+ }
+ $rawMetadata = $metadata->raw_metadata;
+
+ $attachments = collect($rawMetadata['streams'])->filter(fn (array $stream) => ($stream['codec_type'] ?? null) === 'attachment');
+
+ $fontAttachments = $attachments->map(function (array $stream): FontAttachmentData {
+ $filename = $stream['tags']['filename'] ?? throw new RuntimeException('Font attachment is missing a filename.');
+ $mimeType = $stream['tags']['mimetype'] ?? 'application/octet-stream';
+
+ return new FontAttachmentData(
+ streamIndex: $stream['index'],
+ fileName: $filename,
+ codec: $stream['codec_name'] ?? null,
+ mimeType: $mimeType,
+ );
+ });
+
+ return $fontAttachments->values()->all();
+ }
+
+ /**
+ * Parse the embedded font attachments from metadata.
+ *
+ * @param ExtractedFontData[] $extractedFonts
+ */
+ public function upsertFontData(array $extractedFonts): void {
+ Font::upsert(
+ array_map(
+ fn (ExtractedFontData $font): array => [
+ 'metadata_uuid' => $font->metadataUuid,
+ 'file_name' => $font->fileName,
+ 'codec' => $font->codec,
+ 'mime_type' => $font->mimeType,
+ 'size' => $font->size,
+ 'path' => $font->path,
+ 'hash' => $font->hash,
+ 'updated_at' => now(),
+ ],
+ $extractedFonts,
+ ),
+ ['metadata_uuid', 'file_name'],
+ [
+ 'codec',
+ 'mime_type',
+ 'path',
+ 'size',
+ 'hash',
+ 'updated_at',
+ ]
+ );
+ }
+
+ /**
+ * @param ExtractedFontData[] $extractedFonts
+ * @return ExtractedFontData[]
+ */
+ public function deduplicateFonts(array $extractedFonts): array {
+ return collect($extractedFonts)->groupBy(fn (ExtractedFontData $font) => $font->fileName)->map(fn ($fonts) => $fonts->sortByDesc(
+ fn (ExtractedFontData $font) => $font->size
+ )->first())->values()->all();
+ }
+
+ public function clearDeletedFonts(Metadata $metadata): void {
+ $metadata->fonts()->whereNull('path')->delete();
+ }
+}
diff --git a/resources/js/service/media/attachments.ts b/resources/js/service/media/attachments.ts
index bed9d0c8..36f21261 100644
--- a/resources/js/service/media/attachments.ts
+++ b/resources/js/service/media/attachments.ts
@@ -5,5 +5,5 @@ export const resetSubtitles = (metadataId: number) => {
};
export const resetFonts = (metadataId: number) => {
- return API.delete(`/metadata/${metadataId}/fonts`, { headers: { 'X-Skip-Toast': 'true' } });
+ return API.post(`/metadata/${metadataId}/fonts`, { headers: { 'X-Skip-Toast': 'true' } });
};
diff --git a/routes/api.php b/routes/api.php
index 65281f07..8581e642 100755
--- a/routes/api.php
+++ b/routes/api.php
@@ -11,6 +11,7 @@ use App\Http\Controllers\Api\V1\Feed\ExploreController;
use App\Http\Controllers\Api\V1\FolderController;
use App\Http\Controllers\Api\V1\JobController;
use App\Http\Controllers\Api\V1\Media\MediaController;
+use App\Http\Controllers\Api\V1\Metadata\FontController;
use App\Http\Controllers\Api\V1\Metadata\StoryboardController;
use App\Http\Controllers\Api\V1\Metadata\SubtitleController;
use App\Http\Controllers\Api\V1\MetadataController;
@@ -85,6 +86,9 @@ Route::group(['middleware' => ['auth:sanctum']], function () {
Route::patch('/lyrics', [MetadataController::class, 'updateLyrics']);
// Subtitles
Route::delete('/subtitles', [SubtitleController::class, 'reset']); // clear cache
+ // Fonts
+ Route::get('/fonts', [FontController::class, 'show']);
+ Route::post('/fonts', [FontController::class, 'regenerate']); // clear existing files and queue regenerate
// Storyboard
Route::get('/storyboard', [StoryboardController::class, 'show']);
Route::post('/storyboard', [StoryboardController::class, 'regenerate']); // clear existing files and queue regenerate