47 lines
1.3 KiB
PHP
47 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Attachment;
|
|
use App\Models\ServiceTask;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class AttachmentController extends Controller
|
|
{
|
|
public function store(Request $request, ServiceTask $task)
|
|
{
|
|
$request->validate([
|
|
'file' => 'required|file|max:10240', // 10MB max
|
|
]);
|
|
|
|
$file = $request->file('file');
|
|
$originalName = $file->getClientOriginalName();
|
|
$filename = time() . '_' . $originalName;
|
|
$path = $file->storeAs('attachments/' . $task->id, $filename, 'public');
|
|
|
|
$attachment = $task->attachments()->create([
|
|
'filename' => $filename,
|
|
'original_name' => $originalName,
|
|
'path' => $path,
|
|
'mime_type' => $file->getMimeType(),
|
|
'size' => $file->getSize(),
|
|
]);
|
|
|
|
return back()->with('success', 'Fichier ajouté avec succès.');
|
|
}
|
|
|
|
public function download(Attachment $attachment)
|
|
{
|
|
return Storage::disk('public')->download($attachment->path, $attachment->original_name);
|
|
}
|
|
|
|
public function destroy(Attachment $attachment)
|
|
{
|
|
Storage::disk('public')->delete($attachment->path);
|
|
$attachment->delete();
|
|
|
|
return back()->with('success', 'Fichier supprimé.');
|
|
}
|
|
}
|