|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * NOTICE OF LICENSE. |
| 7 | + * |
| 8 | + * UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0 |
| 9 | + * The details is bundled with this project in the file LICENSE.txt. |
| 10 | + * |
| 11 | + * @project UNIT3D Community Edition |
| 12 | + * |
| 13 | + * @author Roardom <roardom@protonmail.com> |
| 14 | + * @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0 |
| 15 | + */ |
| 16 | + |
| 17 | +namespace App\Http\Controllers; |
| 18 | + |
| 19 | +use App\Models\Article; |
| 20 | +use App\Models\Category; |
| 21 | +use App\Models\Playlist; |
| 22 | +use App\Models\Torrent; |
| 23 | +use App\Models\User; |
| 24 | +use Illuminate\Support\Facades\Storage; |
| 25 | + |
| 26 | +class AuthenticatedImageController extends Controller |
| 27 | +{ |
| 28 | + private const array HEADERS = [ |
| 29 | + 'Cache-Control' => 'private, max-age=7200', |
| 30 | + ]; |
| 31 | + |
| 32 | + public function articleImage(Article $article): \Symfony\Component\HttpFoundation\BinaryFileResponse |
| 33 | + { |
| 34 | + $path = $article->image === null |
| 35 | + ? public_path('img/missing-image.png') |
| 36 | + : Storage::disk('article-images')->path($article->image); |
| 37 | + |
| 38 | + abort_unless(file_exists($path), 404); |
| 39 | + |
| 40 | + return response()->file($path, self::HEADERS); |
| 41 | + } |
| 42 | + |
| 43 | + public function categoryImage(Category $category): \Symfony\Component\HttpFoundation\BinaryFileResponse |
| 44 | + { |
| 45 | + $path = Storage::disk('category-images')->path($category->image); |
| 46 | + |
| 47 | + abort_unless(file_exists($path), 404); |
| 48 | + |
| 49 | + return response()->file($path, self::HEADERS); |
| 50 | + } |
| 51 | + |
| 52 | + public function playlistImage(Playlist $playlist): \Symfony\Component\HttpFoundation\BinaryFileResponse |
| 53 | + { |
| 54 | + $path = Storage::disk('playlist-images')->path($playlist->cover_image); |
| 55 | + |
| 56 | + abort_unless(file_exists($path), 404); |
| 57 | + |
| 58 | + return response()->file($path, self::HEADERS); |
| 59 | + } |
| 60 | + |
| 61 | + public function torrentBanner(Torrent $torrent): \Symfony\Component\HttpFoundation\BinaryFileResponse |
| 62 | + { |
| 63 | + $path = Storage::disk('torrent-banners')->path("torrent-banner_{$torrent->id}"); |
| 64 | + |
| 65 | + abort_unless(file_exists($path), 404); |
| 66 | + |
| 67 | + return response()->file($path, self::HEADERS); |
| 68 | + } |
| 69 | + |
| 70 | + public function torrentCover(Torrent $torrent): \Symfony\Component\HttpFoundation\BinaryFileResponse |
| 71 | + { |
| 72 | + $path = Storage::disk('torrent-covers')->path("torrent-cover_{$torrent->id}"); |
| 73 | + |
| 74 | + abort_unless(file_exists($path), 404); |
| 75 | + |
| 76 | + return response()->file($path, self::HEADERS); |
| 77 | + } |
| 78 | + |
| 79 | + public function userAvatar(User $user): \Symfony\Component\HttpFoundation\BinaryFileResponse |
| 80 | + { |
| 81 | + $path = Storage::disk('user-avatars')->path($user->image); |
| 82 | + |
| 83 | + abort_unless(file_exists($path), 404); |
| 84 | + |
| 85 | + return response()->file($path, self::HEADERS); |
| 86 | + } |
| 87 | + |
| 88 | + public function userIcon(User $user): \Symfony\Component\HttpFoundation\BinaryFileResponse |
| 89 | + { |
| 90 | + $path = Storage::disk('user-icons')->path($user->icon); |
| 91 | + |
| 92 | + abort_unless(file_exists($path), 404); |
| 93 | + |
| 94 | + return response()->file($path, self::HEADERS); |
| 95 | + } |
| 96 | +} |
0 commit comments