Initial commit: Import existing Laravel project
This commit is contained in:
33
app/Http/Controllers/AttachmentController.php
Normal file
33
app/Http/Controllers/AttachmentController.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Attachment;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
|
||||
class AttachmentController extends Controller
|
||||
{
|
||||
/**
|
||||
* Affiche/télécharge de manière sécurisée une pièce jointe.
|
||||
*/
|
||||
public function show(Attachment $attachment)
|
||||
{
|
||||
// On vérifie si l'utilisateur a le droit de voir la commande liée à cette pièce jointe
|
||||
$order = $attachment->order;
|
||||
|
||||
Gate::authorize('view', $order);
|
||||
|
||||
// Si le fichier n'existe pas dans le stockage public
|
||||
if (!Storage::disk('public')->exists($attachment->file_path)) {
|
||||
abort(404, 'Fichier non trouvé.');
|
||||
}
|
||||
|
||||
$path = Storage::disk('public')->path($attachment->file_path);
|
||||
|
||||
// On renvoie le fichier pour affichage inline (utile pour les PDF/images)
|
||||
return response()->file($path, [
|
||||
'Content-Disposition' => 'inline; filename="' . basename($attachment->file_name) . '"'
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user