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}"); // If file doesn't exist and filename ends with -cover.jpg, try old format if (! file_exists($filePath) && preg_match('/^(\d+)-cover\.jpg$/', $filename, $matches)) { $oldFormatPath = storage_path("covers/{$type}/{$matches[1]}.jpg"); if (file_exists($oldFormatPath)) { $filePath = $oldFormatPath; } } } else { $filePath = storage_path("covers/{$type}/{$filename}"); } // Check if file exists if (! file_exists($filePath)) { // Return placeholder image $placeholderPath = public_path('assets/images/no-cover.png'); if (file_exists($placeholderPath)) { return response()->file($placeholderPath); } abort(404); } // Determine content type $extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION)); $contentType = match ($extension) { 'jpg', 'jpeg' => 'image/jpeg', 'png' => 'image/png', 'gif' => 'image/gif', 'webp' => 'image/webp', default => 'image/jpeg', }; // Serve the file with proper headers return response()->file($filePath, [ 'Content-Type' => $contentType, 'Cache-Control' => 'public, max-age=31536000', // Cache for 1 year ]); } }