1
0
Fork 0
BitHarbor/backend/app/Http/Controllers/ImageController.php

45 lines
1.1 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
class ImageController extends Controller
{
public function index(string $path): Response
{
$cleanPath = ltrim($path, '/');
if ($cleanPath === '' || Str::contains($cleanPath, ['../', '..\\'])) {
abort(404);
}
$disk = Storage::disk('images');
if (! $disk->exists($cleanPath)) {
abort(404);
}
$file = $disk->get($cleanPath);
if ($file === null) {
abort(404);
}
$contentType = match (strtolower(pathinfo($cleanPath, PATHINFO_EXTENSION))) {
'jpg', 'jpeg' => 'image/jpeg',
'png' => 'image/png',
'webp' => 'image/webp',
'gif' => 'image/gif',
'svg' => 'image/svg+xml',
default => 'application/octet-stream',
};
return response($file, 200, [
'Content-Type' => $contentType,
'Cache-Control' => 'public, max-age=31536000, immutable',
]);
}
}