|
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
|
|
|
$filePath = storage_path("covers/{$type}/{$filename}"); |
|
29
|
|
|
|
|
30
|
|
|
// For preview and sample images, try with _thumb suffix if original doesn't exist |
|
31
|
|
|
if (! file_exists($filePath) && in_array($type, ['preview', 'sample'])) { |
|
32
|
|
|
// Try with _thumb suffix |
|
33
|
|
|
$pathInfo = pathinfo($filename); |
|
34
|
|
|
$thumbFilename = $pathInfo['filename'].'_thumb.'.($pathInfo['extension'] ?? 'jpg'); |
|
35
|
|
|
$thumbPath = storage_path("covers/{$type}/{$thumbFilename}"); |
|
36
|
|
|
|
|
37
|
|
|
if (file_exists($thumbPath)) { |
|
38
|
|
|
$filePath = $thumbPath; |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
// Check if file exists |
|
43
|
|
|
if (! file_exists($filePath)) { |
|
44
|
|
|
// Return placeholder image |
|
45
|
|
|
$placeholderPath = public_path('assets/images/no-cover.png'); |
|
46
|
|
|
if (file_exists($placeholderPath)) { |
|
47
|
|
|
return response()->file($placeholderPath); |
|
48
|
|
|
} |
|
49
|
|
|
abort(404); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
// Determine content type |
|
53
|
|
|
$extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION)); |
|
|
|
|
|
|
54
|
|
|
$contentType = match ($extension) { |
|
55
|
|
|
'jpg', 'jpeg' => 'image/jpeg', |
|
56
|
|
|
'png' => 'image/png', |
|
57
|
|
|
'gif' => 'image/gif', |
|
58
|
|
|
'webp' => 'image/webp', |
|
59
|
|
|
default => 'image/jpeg', |
|
60
|
|
|
}; |
|
61
|
|
|
|
|
62
|
|
|
// Serve the file with proper headers |
|
63
|
|
|
return response()->file($filePath, [ |
|
64
|
|
|
'Content-Type' => $contentType, |
|
65
|
|
|
'Cache-Control' => 'public, max-age=31536000', // Cache for 1 year |
|
66
|
|
|
]); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|