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;
|
2024-10-21 08:34:03 -07:00
|
|
|
use Illuminate\Contracts\Filesystem\FileNotFoundException;
|
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);
|
|
|
|
}
|
|
|
|
}
|
2024-10-16 16:07:54 -07:00
|
|
|
|
2024-10-16 16:18:40 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This determines the file types that should be allowed inline and checks their fileinfo extension
|
2024-10-17 08:31:17 -07:00
|
|
|
* to determine that they are safe to display inline.
|
2024-10-16 16:18:40 -07:00
|
|
|
*
|
|
|
|
* @author <A. Gianotto> [<snipe@snipe.net]>
|
|
|
|
* @since v7.0.14
|
|
|
|
* @param $file_with_path
|
|
|
|
* @return bool
|
|
|
|
*/
|
2024-10-16 16:07:54 -07:00
|
|
|
public static function allowSafeInline($file_with_path) {
|
|
|
|
|
|
|
|
$allowed_inline = [
|
|
|
|
'pdf',
|
|
|
|
'svg',
|
|
|
|
'jpg',
|
|
|
|
'gif',
|
|
|
|
'svg',
|
|
|
|
'avif',
|
|
|
|
'webp',
|
|
|
|
'png',
|
|
|
|
];
|
|
|
|
|
2024-10-21 08:34:03 -07:00
|
|
|
|
|
|
|
// The file exists and is allowed to be displayed inline
|
|
|
|
if (Storage::exists($file_with_path) && (in_array(pathinfo($file_with_path, PATHINFO_EXTENSION), $allowed_inline))) {
|
2024-10-16 16:07:54 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
2024-10-21 08:34:03 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Decide whether to show the file inline or download it.
|
|
|
|
*/
|
|
|
|
public static function showOrDownloadFile($file, $filename) {
|
|
|
|
|
|
|
|
$headers = [];
|
|
|
|
|
|
|
|
if (request('inline') == 'true') {
|
|
|
|
|
|
|
|
$headers = [
|
|
|
|
'Content-Disposition' => 'inline',
|
|
|
|
];
|
|
|
|
|
|
|
|
// This is NOT allowed as inline - force it to be displayed as text in the browser
|
|
|
|
if (self::allowSafeInline($file) != true) {
|
|
|
|
$headers = array_merge($headers, ['Content-Type' => 'text/plain']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Everything else seems okay, but the file doesn't exist on the server.
|
|
|
|
if (Storage::missing($file)) {
|
|
|
|
throw new FileNotFoundException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Storage::download($file, $filename, $headers);
|
|
|
|
|
|
|
|
}
|
2021-06-10 13:15:52 -07:00
|
|
|
}
|