2021-03-15 12:26:39 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Helpers;
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2021-03-15 12:26:39 -07:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2024-07-04 23:07:20 -07:00
|
|
|
use Illuminate\Http\Response;
|
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse;
|
2021-03-15 12:26:39 -07:00
|
|
|
class StorageHelper
|
|
|
|
{
|
2024-07-04 23:07:20 -07:00
|
|
|
public static function downloader($filename, $disk = 'default') : BinaryFileResponse | RedirectResponse | StreamedResponse
|
2021-06-10 13:15:52 -07:00
|
|
|
{
|
|
|
|
if ($disk == 'default') {
|
2021-03-15 12:26:39 -07:00
|
|
|
$disk = config('filesystems.default');
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
switch (config("filesystems.disks.$disk.driver")) {
|
2021-03-15 12:26:39 -07:00
|
|
|
case 'local':
|
|
|
|
return response()->download(Storage::disk($disk)->path($filename)); //works for PRIVATE or public?!
|
|
|
|
|
|
|
|
case 's3':
|
|
|
|
return redirect()->away(Storage::disk($disk)->temporaryUrl($filename, now()->addMinutes(5))); //works for private or public, I guess?
|
|
|
|
|
|
|
|
default:
|
|
|
|
return Storage::disk($disk)->download($filename);
|
|
|
|
}
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
}
|