ldc-boutique-cnc/app/Http/Controllers/Bo/DownloadController.php
Jérémy Munsch f99e259bd5
Tons of fixes Refonte SYNC 1
- Authentification System OK
2022-11-16 12:47:41 +01:00

39 lines
1.2 KiB
PHP

<?php
namespace App\Http\Controllers\Bo;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\StreamedResponse;
class DownloadController extends Controller
{
/**
* Download a file from upload using control access
* Must be authenticated.
*
* @param string $filePath
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
*/
public function download(string $filePath): StreamedResponse
{
if (!\auth('backend')->check()) {
abort(404);
}
/** @var \Illuminate\Filesystem\FilesystemAdapter */
$disk = Storage::disk('uploads');
if (!$disk->exists($filePath)) {
abort(404);
}
return response()->stream(function () use ($disk, $filePath) {
$stream = $disk->readStream($filePath);
\fpassthru($stream);
\fclose($stream);
}, 200, [
'Content-Type' => File::mimeType($disk->path($filePath)),
'Content-Length' => $disk->size($filePath),
'Content-Disposition' => 'inline',
]);
}
}