Skip to content

Commit

Permalink
Stop writing attachments in separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
Timshel committed Sep 3, 2024
1 parent 4a87e79 commit 9335034
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 44 deletions.
8 changes: 0 additions & 8 deletions src/lib/mailparser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -63,15 +62,8 @@ export async function parse(input): Promise<ParsedMail> {
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,
};
});
Expand Down
20 changes: 1 addition & 19 deletions src/lib/mailserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -391,7 +390,7 @@ export class MailServer {
}

for (const attachment of mail.attachments) {
if (attachment.filename === filename) {
if (attachment.generatedFileName === filename) {
return attachment;
}
}
Expand Down Expand Up @@ -454,17 +453,6 @@ function createMailDir(mailDir: string) {
logger.info("MailDev using directory %s", mailDir);
}

async function saveAttachment(mailServer: MailServer, id, attachment): Promise<void> {
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<Mail> {
const emlPath = path.join(mailDir, envelope.id + ".eml");
const data = await pfs.readFile(emlPath, "utf8");
Expand Down Expand Up @@ -497,12 +485,6 @@ async function buildMail(
async function saveEmailToStore(mailServer: MailServer, mail: Mail): Promise<void> {
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);

Expand Down
17 changes: 0 additions & 17 deletions test/mailparser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" +
Expand Down

0 comments on commit 9335034

Please sign in to comment.