feat: add support for embedded files in email attachments and enhance logging in MailViewer

This commit is contained in:
Flavio Fois
2026-02-05 12:39:43 +01:00
parent e9b013412a
commit 7cfc12c6ef
2 changed files with 65 additions and 3 deletions

View File

@@ -2,6 +2,7 @@ package internal
import ( import (
"bytes" "bytes"
"encoding/base64"
"fmt" "fmt"
"io" "io"
"net/mail" "net/mail"
@@ -58,10 +59,68 @@ func ReadEmlFile(filePath string) (*EmailData, error) {
body = email.TextBody body = email.TextBody
} }
// Process attachments and detect PEC // Process attachments list and PEC detection
var attachments []EmailAttachment var attachments []EmailAttachment
var hasDatiCert, hasSmime, hasInnerEmail bool var hasDatiCert, hasSmime, hasInnerEmail bool
// Process embedded files (inline images) -> add to body AND add as attachments
for _, ef := range email.EmbeddedFiles {
data, err := io.ReadAll(ef.Data)
if err != nil {
continue
}
// Convert to base64
b64 := base64.StdEncoding.EncodeToString(data)
mimeType := ef.ContentType
if parts := strings.Split(mimeType, ";"); len(parts) > 0 {
mimeType = strings.TrimSpace(parts[0])
}
if mimeType == "" {
mimeType = "application/octet-stream"
}
// Create data URI
dataURI := fmt.Sprintf("data:%s;base64,%s", mimeType, b64)
// Replace cid:reference with data URI in HTML body
// ef.CID is already trimmed of <>
target := "cid:" + ef.CID
body = strings.ReplaceAll(body, target, dataURI)
// ALSO ADD AS ATTACHMENTS for the viewer
filename := ef.CID
if filename == "" {
filename = "embedded_image"
}
// If no extension, try to infer from mimetype
if !strings.Contains(filename, ".") {
ext := "dat"
switch mimeType {
case "image/jpeg":
ext = "jpg"
case "image/png":
ext = "png"
case "image/gif":
ext = "gif"
case "application/pdf":
ext = "pdf"
default:
if parts := strings.Split(mimeType, "/"); len(parts) > 1 {
ext = parts[1]
}
}
filename = fmt.Sprintf("%s.%s", filename, ext)
}
attachments = append(attachments, EmailAttachment{
Filename: filename,
ContentType: mimeType,
Data: data,
})
}
// Process standard attachments
for _, att := range email.Attachments { for _, att := range email.Attachments {
data, err := io.ReadAll(att.Data) data, err := io.ReadAll(att.Data)
if err != nil { if err != nil {

View File

@@ -9,6 +9,7 @@
import { mailState } from "$lib/stores/mail-state.svelte"; import { mailState } from "$lib/stores/mail-state.svelte";
import { settingsStore } from "$lib/stores/settings.svelte"; import { settingsStore } from "$lib/stores/settings.svelte";
import * as m from "$lib/paraglide/messages"; import * as m from "$lib/paraglide/messages";
import { dev } from "$app/environment";
let unregisterEvents = () => {}; let unregisterEvents = () => {};
let isLoading = $state(false); let isLoading = $state(false);
@@ -22,11 +23,13 @@
} }
$effect(() => { $effect(() => {
console.log("Current email changed:", mailState.currentEmail); if(dev) {
console.log(mailState.currentEmail)
}
console.info("Current email changed:", mailState.currentEmail?.subject);
if(mailState.currentEmail !== null) { if(mailState.currentEmail !== null) {
sidebarOpen.set(false); sidebarOpen.set(false);
} }
console.log(mailState.currentEmail?.attachments)
}) })
onDestroy(() => { onDestroy(() => {