Update books handling

This commit is contained in:
DariusIII
2026-04-23 14:51:59 +02:00
parent d8b19cb346
commit 891d5f6865
17 changed files with 353 additions and 56 deletions
+28 -14
View File
@@ -10,6 +10,11 @@ use Symfony\Component\HttpFoundation\BinaryFileResponse;
class CoverController extends Controller
{
/**
* @var array<int, string>
*/
private const array NUMERIC_ID_TYPES = ['anime', 'book', 'console', 'games', 'music', 'tvshows'];
/**
* Serve cover images from storage
*
@@ -26,6 +31,10 @@ class CoverController extends Controller
abort(404);
}
if (in_array($type, self::NUMERIC_ID_TYPES, true) && $this->isInvalidNumericCoverFilename($filename)) {
return $this->respondWithPlaceholder();
}
// Build the file path
// For preview and sample images, try with _thumb suffix first
if (in_array($type, ['preview', 'sample'], true)) {
@@ -40,20 +49,6 @@ class CoverController extends Controller
$filePath = storage_path("covers/{$type}/{$filename}");
}
} elseif ($type === 'anime') {
// For anime, check if the ID is valid (must be > 0)
// Reject covers for anidbid <= 0 (failed processing, no match, etc.)
if (preg_match('/^(-?\d+)(?:-cover)?\.jpg$/', $filename, $matches)) {
$anidbid = (int) $matches[1];
if ($anidbid <= 0) {
// Return placeholder for invalid IDs
$placeholderPath = public_path('assets/images/no-cover.png');
if (file_exists($placeholderPath)) {
return response()->file($placeholderPath);
}
abort(404);
}
}
// For anime, try the requested filename first, then fall back to old format (without -cover)
$filePath = storage_path("covers/{$type}/{$filename}");
@@ -94,4 +89,23 @@ class CoverController extends Controller
'Cache-Control' => 'public, max-age=31536000', // Cache for 1 year
]);
}
private function isInvalidNumericCoverFilename(string $filename): bool
{
if (preg_match('/^(-?\d+)(?:-cover)?\.jpg$/', $filename, $matches) !== 1) {
return false;
}
return (int) $matches[1] <= 0;
}
private function respondWithPlaceholder(): Response|BinaryFileResponse
{
$placeholderPath = public_path('assets/images/no-cover.png');
if (file_exists($placeholderPath)) {
return response()->file($placeholderPath);
}
abort(404);
}
}