Passed
Push — master ( 9f15d2...3a106b )
by Darko
10:30
created

CoverController::show()   B

Complexity

Conditions 7
Paths 18

Size

Total Lines 48
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 48
rs 8.5866
cc 7
nc 18
nop 2
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));
0 ignored issues
show
Bug introduced by
It seems like pathinfo($filePath, App\...ers\PATHINFO_EXTENSION) can also be of type array; however, parameter $string of strtolower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
        $extension = strtolower(/** @scrutinizer ignore-type */ pathinfo($filePath, PATHINFO_EXTENSION));
Loading history...
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