A self-hosted media server and file management system
Vue ffmpegfile-serverjellyfinlaravelmediamedia-playermedia-servermediabrowsermediaservermusicmusic-librarymusic-playermusicplayerself-hostedvuejs
chore: pint
aminnausin7d0ec04839d3
diff --git a/app/Exceptions/FFmpegException.php b/app/Exceptions/FFmpegException.phpnew file mode 100644index 00000000..678a1d6a--- /dev/null+++ b/app/Exceptions/FFmpegException.php@@ -0,0 +1,7 @@+<?php++namespace App\Exceptions;++use Exception;++class FFmpegException extends Exception {}diff --git a/app/Http/Controllers/Api/V1/Metadata/StoryboardController.php b/app/Http/Controllers/Api/V1/Metadata/StoryboardController.phpindex 9e0aa0c0..51d7b34a 100644--- a/app/Http/Controllers/Api/V1/Metadata/StoryboardController.php+++ b/app/Http/Controllers/Api/V1/Metadata/StoryboardController.php@@ -9,6 +9,7 @@ use App\Models\Metadata;use App\Models\Storyboard;use App\Models\SubTask;use App\Services\FileJobService;+use Illuminate\Http\JsonResponse;use Illuminate\Support\Facades\Auth;use Illuminate\Support\Facades\Gate;use Illuminate\Support\Facades\RateLimiter;@@ -27,24 +28,13 @@ class StoryboardController extends Controller {public function regenerate(Metadata $metadata) {$isAdmin = Gate::allows('admin');+ $rateLimitError = ! $isAdmin ? $this->getRateLimitError() : null;+ if ($rateLimitError) {+ return $rateLimitError;+ }+if (! $isAdmin) {- $perMinuteKey = 'storyboard-regenerate:minute:' . Auth::id();- $perHourKey = 'storyboard-regenerate:hour:' . Auth::id();-- if (RateLimiter::tooManyAttempts($perMinuteKey, 1)) {- return response()->json([- 'message' => 'Too many requests. Try again in ' . RateLimiter::availableIn($perMinuteKey) . ' seconds.',- ], 429);- }-- if (RateLimiter::tooManyAttempts($perHourKey, 15)) {- return response()->json([- 'message' => 'Hourly limit reached. Try again in ' . ceil(RateLimiter::availableIn($perHourKey) / 60) . ' minutes.',- ], 429);- }-- RateLimiter::hit($perMinuteKey, 60);- RateLimiter::hit($perHourKey, 3600);+ $this->hitRateLimits();}$alreadyRunning = SubTask::where('reference_uuid', $metadata->uuid)@@ -65,4 +55,31 @@ class StoryboardController extends Controller {return response()->json(['task_id' => $task->id], 202);}++ private function getRateLimitError(): ?JsonResponse {+ $perMinuteKey = 'storyboard-regenerate:minute:' . Auth::id();+ $perHourKey = 'storyboard-regenerate:hour:' . Auth::id();++ if (RateLimiter::tooManyAttempts($perMinuteKey, 1)) {+ return response()->json([+ 'message' => 'Too many requests. Try again in ' . RateLimiter::availableIn($perMinuteKey) . ' seconds.',+ ], 429);+ }++ if (RateLimiter::tooManyAttempts($perHourKey, 15)) {+ return response()->json([+ 'message' => 'Hourly limit reached. Try again in ' . ceil(RateLimiter::availableIn($perHourKey) / 60) . ' minutes.',+ ], 429);+ }++ return null;+ }++ private function hitRateLimits(): void {+ $perMinuteKey = 'storyboard-regenerate:minute:' . Auth::id();+ $perHourKey = 'storyboard-regenerate:hour:' . Auth::id();++ RateLimiter::hit($perMinuteKey, 60);+ RateLimiter::hit($perHourKey, 3600);+ }}diff --git a/app/Jobs/Metadata/GenerateStoryboard.php b/app/Jobs/Metadata/GenerateStoryboard.phpindex 24f64134..84e8a1c7 100644--- a/app/Jobs/Metadata/GenerateStoryboard.php+++ b/app/Jobs/Metadata/GenerateStoryboard.php@@ -3,6 +3,7 @@namespace App\Jobs\Metadata;use App\Enums\TaskStatus;+use App\Exceptions\FFmpegException;use App\Exceptions\StoryboardNotSupportedException;use App\Jobs\ManagedSubTask;use App\Models\Metadata;@@ -123,7 +124,7 @@ class GenerateStoryboard extends ManagedSubTask {'error' => $process->getErrorOutput(),'th' => $th->getMessage(),]);- throw new \RuntimeException('FFmpeg failed: ' . $process->getErrorOutput());+ throw new FFmpegException('FFmpeg failed: ' . $process->getErrorOutput());}$timeElapsed = round(microtime(true) - $start, 2);@@ -132,7 +133,7 @@ class GenerateStoryboard extends ManagedSubTask {$sheetCount = count($sheets);if ($sheetCount === 0) {- throw new \RuntimeException('FFmpeg produced no output files');+ throw new FFmpegException('FFmpeg produced no output files');}DB::transaction(function () use ($options, $tile_count, $timeElapsed, $rawCommand) {diff --git a/app/Services/Hardware/HardwareDetectionService.php b/app/Services/Hardware/HardwareDetectionService.phpindex e0f183f6..740df710 100644--- a/app/Services/Hardware/HardwareDetectionService.php+++ b/app/Services/Hardware/HardwareDetectionService.php@@ -8,6 +8,12 @@ use Symfony\Component\Process\Process;class HardwareDetectionService {private ?HardwareProfile $cached = null;+ const DEFAULT_ARGUMENTS = [+ '-hide_banner',+ '-loglevel',+ 'error',+ ];+public function detect(): HardwareProfile {if ($this->cached) {return $this->cached;@@ -32,7 +38,7 @@ class HardwareDetectionService {}public function getHwaccels(): array {- $process = new Process(['ffmpeg', '-hwaccels', '-hide_banner']);+ $process = new Process(['ffmpeg', '-hwaccels', ...self::DEFAULT_ARGUMENTS]);$process->run();$output = $process->getOutput();@@ -52,9 +58,7 @@ class HardwareDetectionService {public function validateCuda(): bool {$process = new Process(['ffmpeg',- '-hide_banner',- '-loglevel',- 'error',+ ...self::DEFAULT_ARGUMENTS,'-hwaccel','cuda','-hwaccel_output_format',@@ -77,9 +81,7 @@ class HardwareDetectionService {public function validateVaapi(): bool {$process = new Process(['ffmpeg',- '-hide_banner',- '-loglevel',- 'error',+ ...self::DEFAULT_ARGUMENTS,'-hwaccel','vaapi','-hwaccel_device',@@ -102,9 +104,7 @@ class HardwareDetectionService {private function validateQsv(): bool {$process = new Process(['ffmpeg',- '-hide_banner',- '-loglevel',- 'error',+ ...self::DEFAULT_ARGUMENTS,'-hwaccel','qsv','-f',diff --git a/app/Services/Hardware/HardwareProfile.php b/app/Services/Hardware/HardwareProfile.phpindex ffc595a5..3b101401 100644--- a/app/Services/Hardware/HardwareProfile.php+++ b/app/Services/Hardware/HardwareProfile.php@@ -12,16 +12,11 @@ class HardwareProfile {) {}public function best(): HardwareType {- if ($this->cuda) {- return HardwareType::CUDA;- }- if ($this->qsv) {- return HardwareType::QSV;- }- if ($this->vaapi) {- return HardwareType::VAAPI;- }-- return HardwareType::CPU;+ return match (true) {+ $this->cuda => HardwareType::CUDA,+ $this->qsv => HardwareType::QSV,+ $this->vaapi => HardwareType::VAAPI,+ default => HardwareType::CPU,+ };}}