Passed
Push — master ( 90fb01...efec1e )
by Darko
12:10 queued 01:32
created

CoverController::show()   C

Complexity

Conditions 13
Paths 74

Size

Total Lines 75
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 41
c 2
b 1
f 0
dl 0
loc 75
rs 6.6166
cc 13
nc 74
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
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

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