feat(documents) : bouton reload explorateur + liaison d'un fichier du partage SMB à un ticket

This commit is contained in:
Matthieu
2026-06-12 15:23:56 +02:00
parent 0f1eeeba1c
commit 73a34ef438
12 changed files with 472 additions and 42 deletions
@@ -28,7 +28,15 @@
<!-- File info -->
<div class="min-w-0 flex-1">
<p class="truncate text-xs font-medium text-neutral-700">{{ doc.originalName }}</p>
<p class="text-xs text-neutral-400">{{ formatFileSize(doc.size) }}</p>
<p class="flex items-center gap-1 text-xs text-neutral-400">
<Icon
v-if="doc.sharePath"
name="heroicons:link"
class="h-3 w-3 shrink-0 text-primary-400"
:title="$t('taskDocuments.shareLinkBadge')"
/>
{{ formatFileSize(doc.size) }}
</p>
</div>
<!-- Delete button -->
@@ -0,0 +1,156 @@
<template>
<Teleport v-if="modelValue" to="body">
<Transition name="modal" appear>
<div class="fixed inset-0 z-[70] flex items-center justify-center">
<div class="absolute inset-0 bg-black/30" @click.stop="close" />
<div class="relative z-10 flex max-h-[80vh] w-full max-w-2xl flex-col rounded-lg bg-white shadow-xl">
<!-- En-tête -->
<div class="flex items-center justify-between border-b border-neutral-200 px-6 py-4">
<h3 class="text-lg font-bold text-neutral-900">{{ $t('taskDocuments.linkShareTitle') }}</h3>
<MalioButtonIcon
icon="heroicons:x-mark"
:aria-label="$t('common.cancel')"
variant="ghost"
icon-size="20"
button-class="text-neutral-400 hover:text-neutral-700"
@click="close"
/>
</div>
<!-- Fil d'Ariane -->
<nav class="flex flex-wrap items-center gap-1 border-b border-neutral-100 px-6 py-2 text-sm text-neutral-500">
<button class="hover:text-primary-500" @click="openPath('')">{{ $t('sharedFiles.root') }}</button>
<template v-for="crumb in breadcrumb" :key="crumb.path">
<span>/</span>
<button class="hover:text-primary-500" @click="openPath(crumb.path)">{{ crumb.name }}</button>
</template>
</nav>
<!-- Contenu -->
<div class="min-h-[12rem] flex-1 overflow-auto px-2 py-2">
<div v-if="loading" class="flex justify-center py-12">
<Icon name="heroicons:arrow-path" class="h-6 w-6 animate-spin text-neutral-400" />
</div>
<p v-else-if="error" class="px-4 py-12 text-center text-sm text-red-600">{{ error }}</p>
<p v-else-if="entries.length === 0" class="px-4 py-12 text-center text-sm text-neutral-400">{{ $t('sharedFiles.empty') }}</p>
<ul v-else class="text-sm">
<li
v-for="entry in entries"
:key="entry.path"
class="flex cursor-pointer items-center gap-2 rounded px-3 py-2 hover:bg-neutral-50"
:class="{ 'opacity-60': linking }"
@click="onEntryClick(entry)"
>
<Icon :name="entry.isDir ? 'mdi:folder-outline' : iconForMime(entry.mimeType)" class="h-5 w-5 shrink-0 text-neutral-400" />
<span class="flex-1 truncate">{{ entry.name }}</span>
<span class="shrink-0 text-xs text-neutral-400">{{ entry.isDir ? '' : formatFileSize(entry.size) }}</span>
</li>
</ul>
</div>
<p class="border-t border-neutral-100 px-6 py-3 text-xs text-neutral-400">{{ $t('taskDocuments.linkShareHint') }}</p>
</div>
</div>
</Transition>
</Teleport>
</template>
<script setup lang="ts">
import type { Breadcrumb, FileEntry } from '~/services/dto/share'
import { useShareService } from '~/services/share'
import { useTaskDocumentService } from '~/services/task-documents'
import { formatFileSize } from '~/utils/format'
const props = defineProps<{
modelValue: boolean
taskId: number
}>()
const emit = defineEmits<{
(e: 'update:modelValue', value: boolean): void
(e: 'linked'): void
}>()
const { browse } = useShareService()
const { linkShare } = useTaskDocumentService()
const toast = useToast()
const { t } = useI18n()
const currentPath = ref('')
const breadcrumb = ref<Breadcrumb[]>([])
const entries = ref<FileEntry[]>([])
const loading = ref(false)
const error = ref<string | null>(null)
const linking = ref(false)
async function load(path: string) {
loading.value = true
error.value = null
try {
const result = await browse(path)
currentPath.value = result.path
breadcrumb.value = result.breadcrumb
entries.value = result.entries
} catch (e: unknown) {
error.value = (e as Error)?.message ?? t('sharedFiles.previewError')
entries.value = []
} finally {
loading.value = false
}
}
function openPath(path: string) {
load(path)
}
async function onEntryClick(entry: FileEntry) {
if (linking.value) return
if (entry.isDir) {
load(entry.path)
return
}
linking.value = true
try {
await linkShare(props.taskId, entry.path)
toast.success({ title: '', message: t('taskDocuments.linkShareSuccess') })
emit('linked')
close()
} catch {
toast.error({ title: 'Erreur', message: t('taskDocuments.linkShareError') })
} finally {
linking.value = false
}
}
function iconForMime(mime: string): string {
if (mime.startsWith('image/')) return 'mdi:file-image-outline'
if (mime === 'application/pdf') return 'mdi:file-pdf-box'
if (mime.includes('wordprocessingml') || mime === 'application/msword') return 'mdi:file-word-outline'
if (mime.includes('spreadsheetml') || mime === 'application/vnd.ms-excel') return 'mdi:file-excel-outline'
if (mime.startsWith('text/')) return 'mdi:file-document-outline'
return 'mdi:file-outline'
}
function close() {
emit('update:modelValue', false)
}
watch(() => props.modelValue, (open) => {
if (open) {
entries.value = []
load('')
}
})
</script>
<style scoped>
.modal-enter-active,
.modal-leave-active {
transition: opacity 0.2s ease;
}
.modal-enter-from,
.modal-leave-to {
opacity: 0;
}
</style>
+19
View File
@@ -184,6 +184,20 @@
:task-id="task.id"
@uploaded="handleDocumentUploaded"
/>
<div v-if="isEditing && task && isAdmin && shareEnabled" class="mt-2">
<MalioButton
variant="tertiary"
:label="$t('taskDocuments.linkShareButton')"
button-class="w-auto px-3"
@click="showShareLinker = true"
/>
</div>
<TaskDocumentShareLinker
v-if="isEditing && task && isAdmin"
v-model="showShareLinker"
:task-id="task.id"
@linked="handleDocumentUploaded"
/>
<TaskDocumentList
v-if="isEditing && task"
:documents="localDocuments"
@@ -869,6 +883,11 @@ function formatMailDate(iso: string | null): string {
const localDocuments = ref<TaskDocument[]>([])
const previewDoc = ref<TaskDocument | null>(null)
// Lien vers un fichier du partage SMB (en plus de l'upload classique)
const { enabled: shareEnabled, ensureLoaded: ensureShareStatus } = useShareStatus()
const showShareLinker = ref(false)
ensureShareStatus()
// Sync documents from task prop when modal opens or task changes
watch(() => props.task?.documents, (docs) => {
localDocuments.value = docs ? [...docs] : []
+8 -1
View File
@@ -128,7 +128,13 @@
"download": "Télécharger",
"copy": "Copier",
"copied": "Contenu copié !",
"maxSizeError": "Le fichier dépasse la taille maximale de 50 Mo."
"maxSizeError": "Le fichier dépasse la taille maximale de 50 Mo.",
"linkShareButton": "Lier depuis le partage",
"linkShareTitle": "Lier un fichier du partage",
"linkShareHint": "Cliquez sur un dossier pour naviguer, sur un fichier pour le lier au ticket.",
"linkShareSuccess": "Fichier du partage lié au ticket.",
"linkShareError": "Impossible de lier ce fichier (type non autorisé ou introuvable).",
"shareLinkBadge": "Lien vers le partage"
},
"tasks": {
"created": "Ticket créé avec succès.",
@@ -434,6 +440,7 @@
"empty": "Ce dossier est vide.",
"filterPlaceholder": "Filtrer ce dossier…",
"download": "Télécharger",
"reload": "Recharger",
"previewError": "Aperçu impossible. Téléchargez le fichier pour l'ouvrir.",
"colName": "Nom",
"colSize": "Taille",
+21 -6
View File
@@ -11,12 +11,23 @@
</template>
</nav>
<!-- Filtre local -->
<div class="mt-4 max-w-sm">
<MalioInputText
v-model="filter"
:placeholder="$t('sharedFiles.filterPlaceholder')"
input-class="w-full"
<!-- Filtre local + rechargement -->
<div class="mt-4 flex items-center gap-2">
<div class="max-w-sm flex-1">
<MalioInputText
v-model="filter"
:placeholder="$t('sharedFiles.filterPlaceholder')"
input-class="w-full"
/>
</div>
<MalioButtonIcon
icon="heroicons:arrow-path"
:aria-label="$t('sharedFiles.reload')"
variant="ghost"
icon-size="20"
:disabled="loading"
button-class="text-neutral-500 hover:text-primary-500"
@click="reload"
/>
</div>
@@ -113,6 +124,10 @@ function openPath(path: string) {
load(path)
}
function reload() {
load(currentPath.value)
}
function onEntryClick(entry: FileEntry) {
if (entry.isDir) {
openPath(entry.path)
+2 -1
View File
@@ -5,7 +5,8 @@ export type TaskDocument = {
id: number
task: string
originalName: string
fileName: string
fileName?: string | null
sharePath?: string | null
mimeType: string
size: number
createdAt: string
+10 -1
View File
@@ -31,6 +31,15 @@ export function useTaskDocumentService() {
return uploadWithRelation('task', `/api/tasks/${taskId}`, file)
}
async function linkShare(taskId: number, sharePath: string): Promise<TaskDocument> {
return $fetch<TaskDocument>(`${baseURL}/task_documents`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ task: `/api/tasks/${taskId}`, sharePath }),
credentials: 'include',
})
}
async function remove(id: number): Promise<void> {
await api.delete(`/task_documents/${id}`, {}, {
toastSuccessKey: 'taskDocuments.deleted',
@@ -48,5 +57,5 @@ export function useTaskDocumentService() {
})
}
return { getByTask, upload, remove, getDownloadUrl, getContent }
return { getByTask, upload, linkShare, remove, getDownloadUrl, getContent }
}
+33
View File
@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Ajoute le support des documents de tâche liés au partage SMB :
* colonne share_path (chemin relatif sur le partage) et file_name rendu nullable
* (un lien SMB n'a pas de fichier sur disque).
*/
final class Version20260612131431 extends AbstractMigration
{
public function getDescription(): string
{
return 'TaskDocument: add share_path column and make file_name nullable (SMB share links)';
}
public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE task_document ADD share_path VARCHAR(1024) DEFAULT NULL');
$this->addSql('ALTER TABLE task_document ALTER file_name DROP NOT NULL');
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE task_document ALTER file_name SET NOT NULL');
$this->addSql('ALTER TABLE task_document DROP share_path');
}
}
@@ -5,24 +5,33 @@ declare(strict_types=1);
namespace App\Controller;
use App\Entity\TaskDocument;
use App\Service\Share\Exception\ShareConnectionException;
use App\Service\Share\Exception\ShareNotConfiguredException;
use App\Service\Share\FileSource;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\HeaderUtils;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use function is_resource;
class TaskDocumentDownloadController extends AbstractController
{
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly FileSource $fileSource,
private readonly string $uploadDir,
) {}
#[Route('/api/task_documents/{id}/download', name: 'task_document_download', methods: ['GET'], priority: 1)]
#[IsGranted('IS_AUTHENTICATED_FULLY')]
public function __invoke(int $id): BinaryFileResponse
public function __invoke(int $id): Response
{
$document = $this->entityManager->getRepository(TaskDocument::class)->find($id);
@@ -30,6 +39,20 @@ class TaskDocumentDownloadController extends AbstractController
throw new NotFoundHttpException('Document not found.');
}
$mimeType = $document->getMimeType() ?? 'application/octet-stream';
// Inline for images (except SVG) and PDFs, attachment for everything else.
// SVG is always attachment to prevent XSS via embedded JavaScript.
$inline = 'image/svg+xml' !== $mimeType && (str_starts_with($mimeType, 'image/') || 'application/pdf' === $mimeType);
$disposition = $inline ? ResponseHeaderBag::DISPOSITION_INLINE : ResponseHeaderBag::DISPOSITION_ATTACHMENT;
return $document->isShareLink()
? $this->streamFromShare($document, $mimeType, $disposition)
: $this->streamFromDisk($document, $mimeType, $disposition);
}
private function streamFromDisk(TaskDocument $document, string $mimeType, string $disposition): BinaryFileResponse
{
$filePath = $this->uploadDir.'/'.$document->getFileName();
if (!file_exists($filePath)) {
@@ -37,18 +60,32 @@ class TaskDocumentDownloadController extends AbstractController
}
$response = new BinaryFileResponse($filePath);
$mimeType = $document->getMimeType() ?? 'application/octet-stream';
// Inline for images and PDFs, attachment for everything else
// SVG files are always served as attachment to prevent XSS via embedded JavaScript
$disposition = 'image/svg+xml' === $mimeType
? ResponseHeaderBag::DISPOSITION_ATTACHMENT
: (str_starts_with($mimeType, 'image/') || 'application/pdf' === $mimeType
? ResponseHeaderBag::DISPOSITION_INLINE
: ResponseHeaderBag::DISPOSITION_ATTACHMENT);
$response->setContentDisposition($disposition, $document->getOriginalName());
$response->setContentDisposition($disposition, (string) $document->getOriginalName());
$response->headers->set('Content-Type', $mimeType);
$response->headers->set('X-Content-Type-Options', 'nosniff');
return $response;
}
private function streamFromShare(TaskDocument $document, string $mimeType, string $disposition): StreamedResponse
{
try {
$stream = $this->fileSource->read((string) $document->getSharePath());
} catch (ShareNotConfiguredException) {
throw new NotFoundHttpException('Share not configured.');
} catch (ShareConnectionException) {
throw new NotFoundHttpException('File not found on the share.');
}
$response = new StreamedResponse(function () use ($stream): void {
if (is_resource($stream)) {
fpassthru($stream);
fclose($stream);
}
});
$response->headers->set('Content-Type', $mimeType);
$response->headers->set('Content-Disposition', HeaderUtils::makeDisposition($disposition, (string) $document->getOriginalName()));
$response->headers->set('X-Content-Type-Options', 'nosniff');
return $response;
}
+27 -2
View File
@@ -53,10 +53,18 @@ class TaskDocument
#[Groups(['task_document:read', 'task:read'])]
private ?string $originalName = null;
#[ORM\Column(length: 255)]
#[ORM\Column(length: 255, nullable: true)]
#[Groups(['task_document:read', 'task:read'])]
private ?string $fileName = null;
/**
* Chemin relatif sur le partage SMB lorsque le document est un lien vers un fichier du partage
* (au lieu d'un fichier uploadé stocké sur disque). Mutuellement exclusif avec fileName.
*/
#[ORM\Column(length: 1024, nullable: true)]
#[Groups(['task_document:read', 'task:read'])]
private ?string $sharePath = null;
#[ORM\Column(length: 100)]
#[Groups(['task_document:read', 'task:read'])]
private ?string $mimeType = null;
@@ -108,13 +116,30 @@ class TaskDocument
return $this->fileName;
}
public function setFileName(string $fileName): static
public function setFileName(?string $fileName): static
{
$this->fileName = $fileName;
return $this;
}
public function getSharePath(): ?string
{
return $this->sharePath;
}
public function setSharePath(?string $sharePath): static
{
$this->sharePath = $sharePath;
return $this;
}
public function isShareLink(): bool
{
return null !== $this->sharePath;
}
public function getMimeType(): ?string
{
return $this->mimeType;
@@ -17,6 +17,11 @@ class TaskDocumentListener
public function preRemove(TaskDocument $document, PreRemoveEventArgs $event): void
{
// Un lien vers le partage SMB ne possède pas de fichier sur disque : rien à nettoyer.
if ($document->isShareLink()) {
return;
}
$filePath = $this->uploadDir.'/'.$document->getFileName();
if (file_exists($filePath)) {
+133 -18
View File
@@ -8,13 +8,22 @@ use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
use App\Entity\Task;
use App\Entity\TaskDocument;
use App\Service\Share\Exception\InvalidPathException;
use App\Service\Share\Exception\ShareConnectionException;
use App\Service\Share\Exception\ShareNotConfiguredException;
use App\Service\Share\FileEntry;
use App\Service\Share\FileSource;
use App\Service\Share\SharePathResolver;
use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Uid\Uuid;
use function in_array;
/**
* @implements ProcessorInterface<TaskDocument, TaskDocument>
*/
@@ -55,6 +64,8 @@ final readonly class TaskDocumentProcessor implements ProcessorInterface
private EntityManagerInterface $entityManager,
private Security $security,
private RequestStack $requestStack,
private FileSource $fileSource,
private SharePathResolver $pathResolver,
private string $uploadDir,
) {}
@@ -69,6 +80,44 @@ final readonly class TaskDocumentProcessor implements ProcessorInterface
throw new BadRequestHttpException('No request available.');
}
// Deux modes de création : upload d'un fichier (multipart) ou lien vers un fichier du partage SMB (JSON).
$sharePath = $this->extractSharePath($request);
$document = null !== $sharePath
? $this->createShareLink($request, $sharePath)
: $this->createUpload($request);
$document->setCreatedAt(new DateTimeImmutable());
$document->setUploadedBy($this->security->getUser());
$this->entityManager->persist($document);
$this->entityManager->flush();
return $document;
}
private function extractSharePath(Request $request): ?string
{
// Lien SMB : champ multipart/form OU corps JSON { "sharePath": "..." }
$fromForm = $request->request->get('sharePath');
if (is_string($fromForm) && '' !== $fromForm) {
return $fromForm;
}
if (str_contains((string) $request->headers->get('Content-Type'), 'application/json')) {
$payload = json_decode($request->getContent() ?: '{}', true);
if (is_array($payload) && isset($payload['sharePath']) && is_string($payload['sharePath']) && '' !== $payload['sharePath']) {
return $payload['sharePath'];
}
}
return null;
}
private function createUpload(Request $request): TaskDocument
{
$file = $request->files->get('file');
if (null === $file || !$file->isValid()) {
@@ -79,17 +128,7 @@ final readonly class TaskDocumentProcessor implements ProcessorInterface
throw new BadRequestHttpException('File size exceeds 50 MB limit.');
}
$taskIri = $request->request->get('task', '');
if ('' === $taskIri) {
throw new BadRequestHttpException('A task IRI is required.');
}
$task = $this->entityManager->getRepository(Task::class)->find((int) basename($taskIri));
if (null === $task) {
throw new BadRequestHttpException('Task not found.');
}
$task = $this->resolveTask($request->request->get('task', ''));
// Use server-detected MIME type (finfo), not the client-supplied one
$originalName = $file->getClientOriginalName();
@@ -101,8 +140,7 @@ final readonly class TaskDocumentProcessor implements ProcessorInterface
}
$extension = self::MIME_TO_EXTENSION[$mimeType] ?? 'bin';
$uuid = Uuid::v4()->toRfc4122();
$fileName = $uuid.'.'.$extension;
$fileName = Uuid::v4()->toRfc4122().'.'.$extension;
if (!is_dir($this->uploadDir)) {
mkdir($this->uploadDir, 0o775, true);
@@ -116,12 +154,89 @@ final readonly class TaskDocumentProcessor implements ProcessorInterface
$document->setFileName($fileName);
$document->setMimeType($mimeType);
$document->setSize($fileSize);
$document->setCreatedAt(new DateTimeImmutable());
$document->setUploadedBy($this->security->getUser());
$this->entityManager->persist($document);
$this->entityManager->flush();
return $document;
}
private function createShareLink(Request $request, string $rawSharePath): TaskDocument
{
$taskIri = $request->request->get('task');
if (!is_string($taskIri) || '' === $taskIri) {
$payload = json_decode($request->getContent() ?: '{}', true);
$taskIri = is_array($payload) ? ($payload['task'] ?? '') : '';
}
$task = $this->resolveTask((string) $taskIri);
try {
$path = $this->pathResolver->normalizeRelative($rawSharePath);
} catch (InvalidPathException) {
throw new BadRequestHttpException('Invalid share path.');
}
if ('' === $path) {
throw new BadRequestHttpException('A share path is required.');
}
$entry = $this->findShareEntry($path);
if (null === $entry) {
throw new BadRequestHttpException('File not found on the share.');
}
if (!in_array($entry->mimeType, self::ALLOWED_MIME_TYPES, true)) {
throw new BadRequestHttpException(sprintf('File type "%s" is not allowed.', $entry->mimeType));
}
$document = new TaskDocument();
$document->setTask($task);
$document->setOriginalName($entry->name);
$document->setSharePath($path);
$document->setMimeType($entry->mimeType);
$document->setSize($entry->size);
return $document;
}
/**
* Récupère les métadonnées (taille, type) du fichier sur le partage en listant son dossier parent.
*/
private function findShareEntry(string $path): ?FileEntry
{
$parent = dirname($path);
$parent = ('.' === $parent || '/' === $parent) ? '' : $parent;
$name = basename($path);
try {
$entries = $this->fileSource->dir($parent);
} catch (ShareNotConfiguredException) {
throw new BadRequestHttpException('Share not configured.');
} catch (ShareConnectionException) {
throw new BadRequestHttpException('Unable to reach the share.');
}
foreach ($entries as $entry) {
if (!$entry->isDir && $entry->name === $name) {
return $entry;
}
}
return null;
}
private function resolveTask(string $taskIri): Task
{
if ('' === $taskIri) {
throw new BadRequestHttpException('A task IRI is required.');
}
$task = $this->entityManager->getRepository(Task::class)->find((int) basename($taskIri));
if (null === $task) {
throw new BadRequestHttpException('Task not found.');
}
return $task;
}
}