48 lines
1.6 KiB
PHP
48 lines
1.6 KiB
PHP
<?php
|
|
namespace App\Http\Controllers\Filament;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\DocumentoStabile;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class DocumentoStabileDownloadController extends Controller
|
|
{
|
|
public function download(Request $request, DocumentoStabile $documentoStabile)
|
|
{
|
|
$path = is_string($documentoStabile->percorso_file) ? trim((string) $documentoStabile->percorso_file) : '';
|
|
if ($path === '') {
|
|
abort(404);
|
|
}
|
|
|
|
$selectedDisk = null;
|
|
if (Storage::disk('public')->exists($path)) {
|
|
$selectedDisk = 'public';
|
|
} elseif (Storage::disk('local')->exists($path)) {
|
|
$selectedDisk = 'local';
|
|
}
|
|
|
|
if (! $selectedDisk) {
|
|
abort(404);
|
|
}
|
|
|
|
$filename = is_string($documentoStabile->nome_originale) && trim((string) $documentoStabile->nome_originale) !== ''
|
|
? trim((string) $documentoStabile->nome_originale)
|
|
: ((is_string($documentoStabile->nome_file) && trim((string) $documentoStabile->nome_file) !== '')
|
|
? trim((string) $documentoStabile->nome_file)
|
|
: ('documento-stabile-' . (int) $documentoStabile->id . '.pdf'));
|
|
|
|
$filename = str_replace(["\r", "\n", '"'], '', $filename);
|
|
|
|
$documentoStabile->incrementaDownload();
|
|
|
|
if ($request->boolean('inline')) {
|
|
return response()->file(Storage::disk($selectedDisk)->path($path), [
|
|
'Content-Disposition' => 'inline; filename="' . $filename . '"',
|
|
]);
|
|
}
|
|
|
|
return Storage::disk($selectedDisk)->download($path, $filename);
|
|
}
|
|
}
|