feat(mail) : décodage des en-têtes MIME + aperçu inline des pièces jointes
Some checks failed
Auto Tag Develop / tag (push) Has been cancelled

- Décode les encoded-words MIME (RFC 2047) des sujets et noms d'expéditeur
  via App\Mail\MimeHeaderDecoder, appliqué dans ImapMailProvider (sync propre)
- Commande app:mail:redecode-headers (--dry-run) pour re-décoder l'existant en base
- Aperçu inline images + PDF en visionneuse modale plein écran (MailAttachmentPreview),
  téléchargement conservé pour les autres types
- Tests unitaires du décodeur + maj docs/mail-integration.md

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Matthieu
2026-05-21 11:42:06 +02:00
parent 8f2a688740
commit bcbc04325e
8 changed files with 421 additions and 13 deletions

View File

@@ -1,5 +1,5 @@
<script setup lang="ts">
import type { MailMessageDetailDto, MailAddressDto } from '~/services/dto/mail'
import type { MailMessageDetailDto, MailAddressDto, MailAttachmentDto } from '~/services/dto/mail'
import { sanitizeMailHtml } from '~/utils/sanitizeMailHtml'
import { useMailService } from '~/services/mail'
@@ -24,22 +24,101 @@ const sanitizedBody = computed((): string => {
return sanitizeMailHtml(props.detail.bodyHtml, { allowImages: showImages.value })
})
// ─── Pièces jointes : aperçu / téléchargement ──────────────────────────────
function isImage(mime: string): boolean {
return mime.startsWith('image/')
}
function isPdf(mime: string): boolean {
return mime === 'application/pdf'
}
function isPreviewable(mime: string): boolean {
return isImage(mime) || isPdf(mime)
}
function attachmentIcon(mime: string): string {
if (isImage(mime)) return 'material-symbols:image-outline'
if (isPdf(mime)) return 'material-symbols:picture-as-pdf-outline'
return 'material-symbols:attach-file'
}
const previewOpen = ref(false)
const previewLoading = ref(false)
const previewAtt = ref<MailAttachmentDto | null>(null)
const previewUrl = ref<string | null>(null)
let previewBlob: Blob | null = null
function revokePreview(): void {
if (previewUrl.value) {
URL.revokeObjectURL(previewUrl.value)
previewUrl.value = null
}
previewBlob = null
}
watch(
() => props.detail?.header.id,
() => {
showImages.value = false
previewOpen.value = false
revokePreview()
},
)
watch(previewOpen, (open) => {
if (!open) revokePreview()
})
onBeforeUnmount(revokePreview)
async function handleAttachmentClick(att: MailAttachmentDto): Promise<void> {
if (!isPreviewable(att.mimeType)) {
await handleDownload(att.downloadId, att.filename)
return
}
previewAtt.value = att
previewUrl.value = null
previewLoading.value = true
previewOpen.value = true
try {
const { data } = await mailService.downloadAttachment(att.downloadId)
previewBlob = data
previewUrl.value = URL.createObjectURL(data)
} catch {
// useApi affiche déjà le toast — on referme la visionneuse.
previewOpen.value = false
} finally {
previewLoading.value = false
}
}
function downloadFromPreview(): void {
const att = previewAtt.value
if (!att) return
if (previewBlob) {
triggerBlobDownload(previewBlob, att.filename)
} else {
void handleDownload(att.downloadId, att.filename)
}
}
function triggerBlobDownload(blob: Blob, filename: string): void {
const url = URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = url
a.download = filename
a.click()
URL.revokeObjectURL(url)
}
async function handleDownload(downloadId: string, filename: string): Promise<void> {
try {
const { data } = await mailService.downloadAttachment(downloadId)
const url = URL.createObjectURL(data)
const a = document.createElement('a')
a.href = url
a.download = filename
a.click()
URL.revokeObjectURL(url)
triggerBlobDownload(data, filename)
} catch {
// L'erreur est gérée par useApi (toast automatique)
}
@@ -169,15 +248,31 @@ function joinAddresses(addresses: MailAddressDto[]): string {
:key="att.downloadId"
type="button"
class="flex items-center gap-1.5 rounded border border-neutral-200 bg-neutral-50 px-2.5 py-1.5 text-xs text-neutral-700 transition-colors hover:bg-neutral-100 hover:border-neutral-300"
:title="att.filename"
@click="handleDownload(att.downloadId, att.filename)"
:title="isPreviewable(att.mimeType) ? t('mail.preview.open') : t('mail.actions.download')"
@click="handleAttachmentClick(att)"
>
<Icon name="material-symbols:attach-file" size="14" class="flex-shrink-0 text-neutral-400" />
<Icon :name="attachmentIcon(att.mimeType)" size="14" class="flex-shrink-0 text-neutral-400" />
<span class="max-w-[180px] truncate">{{ att.filename }}</span>
<span class="text-neutral-400">({{ Math.round(att.size / 1024) }} Ko)</span>
<Icon
v-if="isPreviewable(att.mimeType)"
name="material-symbols:visibility-outline"
size="13"
class="flex-shrink-0 text-neutral-400"
/>
</button>
</div>
</div>
<MailAttachmentPreview
v-if="previewAtt"
v-model="previewOpen"
:filename="previewAtt.filename"
:mime-type="previewAtt.mimeType"
:url="previewUrl"
:loading="previewLoading"
@download="downloadFromPreview"
/>
</template>
</div>
</template>