| Conditions | 13 |
| Paths | 74 |
| Total Lines | 75 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 1 | Features | 0 |
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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)); |
||
|
|
|||
| 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 | ]); |
||
| 96 |