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 | } else { |
||
41 | $filePath = storage_path("covers/{$type}/{$filename}"); |
||
42 | } |
||
43 | |||
44 | // Check if file exists |
||
45 | if (! file_exists($filePath)) { |
||
46 | // Return placeholder image |
||
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 | // Determine content type |
||
55 | $extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION)); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
56 | $contentType = match ($extension) { |
||
57 | 'jpg', 'jpeg' => 'image/jpeg', |
||
58 | 'png' => 'image/png', |
||
59 | 'gif' => 'image/gif', |
||
60 | 'webp' => 'image/webp', |
||
61 | default => 'image/jpeg', |
||
62 | }; |
||
63 | |||
64 | // Serve the file with proper headers |
||
65 | return response()->file($filePath, [ |
||
66 | 'Content-Type' => $contentType, |
||
67 | 'Cache-Control' => 'public, max-age=31536000', // Cache for 1 year |
||
68 | ]); |
||
69 | } |
||
70 | } |
||
71 |