initial commit
This commit is contained in:
commit
1db373a03d
110 changed files with 67830 additions and 0 deletions
68
src/Controllers/ImageController.php
Normal file
68
src/Controllers/ImageController.php
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controllers;
|
||||
|
||||
use App\Core\Auth;
|
||||
|
||||
class ImageController
|
||||
{
|
||||
public function getImage(string $category, string $imageName): void
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
if ($user !== null && ($user->isAdmin() || $user->isMember())) {
|
||||
$file = $this->resolve(storage_path('img_storage'), $category, $imageName);
|
||||
|
||||
if ($file !== null) {
|
||||
$this->serve($file);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$placeholder = public_path('img/placeholder/icon_unknown.png');
|
||||
$this->serve($placeholder);
|
||||
}
|
||||
|
||||
/** Löst Kategorie + Dateiname zu einem sicheren Pfad auf. */
|
||||
private function resolve(string $base, string $category, string $imageName): ?string
|
||||
{
|
||||
$category = basename($category);
|
||||
$imageName = basename($imageName);
|
||||
|
||||
$file = realpath($base . '/' . $category . '/' . $imageName);
|
||||
|
||||
if ($file === false || !str_starts_with($file, realpath($base))) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $file;
|
||||
}
|
||||
|
||||
private function serve(string $file): void
|
||||
{
|
||||
$mime = $this->mimeType($file);
|
||||
|
||||
header('Content-Type: ' . $mime);
|
||||
header('Content-Length: ' . (string) filesize($file));
|
||||
readfile($file);
|
||||
exit;
|
||||
}
|
||||
|
||||
private function mimeType(string $file): string
|
||||
{
|
||||
$map = [
|
||||
'jpg' => 'image/jpeg',
|
||||
'jpeg' => 'image/jpeg',
|
||||
'png' => 'image/png',
|
||||
'gif' => 'image/gif',
|
||||
'webp' => 'image/webp',
|
||||
'pdf' => 'application/pdf',
|
||||
];
|
||||
|
||||
$ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
|
||||
|
||||
return $map[$ext] ?? 'application/octet-stream';
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue