E-Mail Anhang-Downloads stabilisieren

KI-AGENT: Lädt Anhänge nun authentifiziert als Blob herunter und macht die Backend-Zuordnung von Anhängen zur Originalmail robuster, inklusive Positions-Fallback.
This commit is contained in:
2026-05-23 20:42:56 +02:00
parent 358cd906ae
commit 8697810127
2 changed files with 50 additions and 7 deletions

View File

@@ -1,5 +1,5 @@
import { FastifyInstance } from "fastify"
import { and, desc, eq } from "drizzle-orm"
import { and, asc, desc, eq } from "drizzle-orm"
import { ImapFlow } from "imapflow"
import { simpleParser } from "mailparser"
@@ -625,6 +625,13 @@ export function emailSyncService(server: FastifyInstance) {
const row = rows[0]
if (!row) return null
const messageAttachments = await server.db
.select()
.from(emailAttachments)
.where(eq(emailAttachments.messageId, row.message.id))
.orderBy(asc(emailAttachments.createdAt), asc(emailAttachments.id))
const attachmentIndex = messageAttachments.findIndex((attachment) => attachment.id === attachmentId)
const account = await getAccount(tenantId, userId, row.message.accountId)
if (!account) {
throw new Error("E-Mail Konto nicht gefunden")
@@ -644,7 +651,7 @@ export function emailSyncService(server: FastifyInstance) {
if (!fetched.source) break
const parsed = await simpleParser(fetched.source)
const attachment = parsed.attachments.find((item) =>
const matchedAttachment = parsed.attachments.find((item) =>
(row.attachment.checksum && item.checksum === row.attachment.checksum)
|| (
item.filename === row.attachment.filename
@@ -652,6 +659,9 @@ export function emailSyncService(server: FastifyInstance) {
&& Number(item.size || 0) === Number(row.attachment.size || 0)
)
)
const attachment = matchedAttachment
|| parsed.attachments[attachmentIndex]
|| (parsed.attachments.length === 1 ? parsed.attachments[0] : null)
if (!attachment) break