39 lines
1.2 KiB
PHP
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',
|
|
]);
|
|
}
|
|
}
|