ldc-boutique-cnc/app/Http/Controllers/Bo/DownloadController.php

40 lines
1.2 KiB
PHP
Raw Normal View History

2022-10-03 17:13:44 +02:00
<?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()) {
2022-10-03 17:13:44 +02:00
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',
]);
}
}