NNTmux /
newznab-tmux
| 1 | <?php |
||
| 2 | |||
| 3 | namespace App\Http\Controllers; |
||
| 4 | |||
| 5 | use Illuminate\Http\Response; |
||
| 6 | use Illuminate\Support\Facades\Storage; |
||
| 7 | use Symfony\Component\HttpFoundation\BinaryFileResponse; |
||
| 8 | |||
| 9 | class CoverController extends Controller |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Serve cover images from storage |
||
| 13 | * |
||
| 14 | * @param string $type The type of cover (movies, console, music, etc.) |
||
| 15 | * @param string $filename The filename of the cover image |
||
| 16 | * @return BinaryFileResponse|Response |
||
| 17 | */ |
||
| 18 | public function show(string $type, string $filename) |
||
| 19 | { |
||
| 20 | // Validate cover type |
||
| 21 | $validTypes = ['anime', 'audio', 'audiosample', 'book', 'console', 'games', 'movies', 'music', 'preview', 'sample', 'tvrage', 'video', 'xxx', 'tvshows']; |
||
| 22 | |||
| 23 | if (! in_array($type, $validTypes)) { |
||
| 24 | abort(404); |
||
| 25 | } |
||
| 26 | |||
| 27 | // Build the file path |
||
| 28 | // For preview and sample images, try with _thumb suffix first |
||
| 29 | if (in_array($type, ['preview', 'sample'])) { |
||
| 30 | $pathInfo = pathinfo($filename); |
||
| 31 | $thumbFilename = $pathInfo['filename'].'_thumb.'.($pathInfo['extension'] ?? 'jpg'); |
||
| 32 | $thumbPath = storage_path("covers/{$type}/{$thumbFilename}"); |
||
| 33 | |||
| 34 | // Use thumb path if it exists, otherwise fall back to original filename |
||
| 35 | if (file_exists($thumbPath)) { |
||
| 36 | $filePath = $thumbPath; |
||
| 37 | } else { |
||
| 38 | $filePath = storage_path("covers/{$type}/{$filename}"); |
||
| 39 | } |
||
| 40 | } elseif ($type === 'anime') { |
||
| 41 | // For anime, check if the ID is valid (must be > 0) |
||
| 42 | // Reject covers for anidbid <= 0 (failed processing, no match, etc.) |
||
| 43 | if (preg_match('/^(-?\d+)(?:-cover)?\.jpg$/', $filename, $matches)) { |
||
| 44 | $anidbid = (int) $matches[1]; |
||
| 45 | if ($anidbid <= 0) { |
||
| 46 | // Return placeholder for invalid IDs |
||
| 47 | $placeholderPath = public_path('assets/images/no-cover.png'); |
||
| 48 | if (file_exists($placeholderPath)) { |
||
| 49 | return response()->file($placeholderPath); |
||
| 50 | } |
||
| 51 | abort(404); |
||
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | // For anime, try the requested filename first, then fall back to old format (without -cover) |
||
| 56 | $filePath = storage_path("covers/{$type}/{$filename}"); |
||
| 57 | |||
| 58 | // If file doesn't exist and filename ends with -cover.jpg, try old format |
||
| 59 | if (!file_exists($filePath) && preg_match('/^(\d+)-cover\.jpg$/', $filename, $matches)) { |
||
| 60 | $oldFormatPath = storage_path("covers/{$type}/{$matches[1]}.jpg"); |
||
| 61 | if (file_exists($oldFormatPath)) { |
||
| 62 | $filePath = $oldFormatPath; |
||
| 63 | } |
||
| 64 | } |
||
| 65 | } else { |
||
| 66 | $filePath = storage_path("covers/{$type}/{$filename}"); |
||
| 67 | } |
||
| 68 | |||
| 69 | // Check if file exists |
||
| 70 | if (! file_exists($filePath)) { |
||
| 71 | // Return placeholder image |
||
| 72 | $placeholderPath = public_path('assets/images/no-cover.png'); |
||
| 73 | if (file_exists($placeholderPath)) { |
||
| 74 | return response()->file($placeholderPath); |
||
| 75 | } |
||
| 76 | abort(404); |
||
| 77 | } |
||
| 78 | |||
| 79 | // Determine content type |
||
| 80 | $extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION)); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 81 | $contentType = match ($extension) { |
||
| 82 | 'jpg', 'jpeg' => 'image/jpeg', |
||
| 83 | 'png' => 'image/png', |
||
| 84 | 'gif' => 'image/gif', |
||
| 85 | 'webp' => 'image/webp', |
||
| 86 | default => 'image/jpeg', |
||
| 87 | }; |
||
| 88 | |||
| 89 | // Serve the file with proper headers |
||
| 90 | return response()->file($filePath, [ |
||
| 91 | 'Content-Type' => $contentType, |
||
| 92 | 'Cache-Control' => 'public, max-age=31536000', // Cache for 1 year |
||
| 93 | ]); |
||
| 94 | } |
||
| 95 | } |
||
| 96 |