From 9335034314a6529354a3566347b5bba528202f97 Mon Sep 17 00:00:00 2001 From: Timshel Date: Tue, 3 Sep 2024 19:23:04 +0200 Subject: [PATCH] Stop writing attachments in separate files --- src/lib/mailparser.ts | 8 -------- src/lib/mailserver.ts | 20 +------------------- test/mailparser.test.js | 17 ----------------- 3 files changed, 1 insertion(+), 44 deletions(-) diff --git a/src/lib/mailparser.ts b/src/lib/mailparser.ts index 38a281e2..7e896e7b 100644 --- a/src/lib/mailparser.ts +++ b/src/lib/mailparser.ts @@ -8,7 +8,6 @@ import type { StructuredHeader, } from "./type"; -const crypto = require("crypto"); const mime = require("mime"); const simpleParser = require("mailparser").simpleParser; const strtotime = require("./helpers/strtotime"); @@ -63,15 +62,8 @@ export async function parse(input): Promise { attachment.contentType, ); - const contentId = - !attachment.contentId && generatedFileName - ? crypto.createHash("md5").update(Buffer.from(generatedFileName, "utf-8")).digest("hex") + - "@mailparser" - : attachment.contentId; - return { ...attachment, - contentId, generatedFileName, }; }); diff --git a/src/lib/mailserver.ts b/src/lib/mailserver.ts index 69639579..903f8d2c 100644 --- a/src/lib/mailserver.ts +++ b/src/lib/mailserver.ts @@ -14,7 +14,6 @@ import { Outgoing } from "./outgoing"; import { SMTPServer } from "smtp-server"; import { promises as pfs } from "fs"; -const crypto = require("crypto"); const events = require("events"); const fs = require("fs"); const os = require("os"); @@ -391,7 +390,7 @@ export class MailServer { } for (const attachment of mail.attachments) { - if (attachment.filename === filename) { + if (attachment.generatedFileName === filename) { return attachment; } } @@ -454,17 +453,6 @@ function createMailDir(mailDir: string) { logger.info("MailDev using directory %s", mailDir); } -async function saveAttachment(mailServer: MailServer, id, attachment): Promise { - await pfs.mkdir(path.join(mailServer.mailDir, id), { recursive: true }); - - const contentId = - attachment.contentId ?? - crypto.createHash("md5").update(Buffer.from(attachment.filename, "utf-8")).digest("hex") + - "@mailparser"; - - return pfs.writeFile(path.join(mailServer.mailDir, id, contentId), attachment.content); -} - async function getDiskEmail(mailDir: string, envelope: Envelope): Promise { const emlPath = path.join(mailDir, envelope.id + ".eml"); const data = await pfs.readFile(emlPath, "utf8"); @@ -497,12 +485,6 @@ async function buildMail( async function saveEmailToStore(mailServer: MailServer, mail: Mail): Promise { logger.log("Saving email: %s, id: %s", mail.subject, mail.id); - await Promise.all( - mail.attachments.map((attachment) => { - return saveAttachment(mailServer, mail.id, attachment); - }), - ); - mailServer.store.push(mail.envelope); mailServer.eventEmitter.emit("new", mail); diff --git a/test/mailparser.test.js b/test/mailparser.test.js index 087e51c9..f19470cc 100644 --- a/test/mailparser.test.js +++ b/test/mailparser.test.js @@ -296,23 +296,6 @@ describe("Mailparser attachment encodings", () => { }); describe("Mailparser Attachment Content-Id", () => { - it("Default", (done) => { - const encodedText = - "Content-Type: application/octet-stream\r\n" + - "Content-Transfer-Encoding: QUOTED-PRINTABLE\r\n" + - 'Content-Disposition: attachment; filename="=?UTF-8?Q?=C3=95=C3=84=C3=96=C3=9C?="\r\n' + - "\r\n" + - "=00=01=02=03=FD=FE=FF"; - - mailParser(encodedText).then((mail) => { - assert.strictEqual( - mail?.attachments[0]?.contentId, - "7c7cf35ce5becf62faea56ed8d0ad6e4@mailparser", - ); - done(); - }); - }); - it("Defined", (done) => { const encodedText = "Content-Type: application/octet-stream\r\n" +