mirror of
https://github.com/sakaljurgis/nntp-worker.git
synced 2026-07-08 21:27:41 +00:00
29 lines
949 B
TypeScript
29 lines
949 B
TypeScript
import { Attachment, PartialMessageMime } from './types';
|
|
import { writeFileSync } from 'fs';
|
|
import { type ParsedMail } from 'mailparser';
|
|
|
|
export const parseAttachments = async (article: ParsedMail, partial: PartialMessageMime['params'] | null) => {
|
|
let attachments: Attachment[] = [];
|
|
if (!article.attachments) {
|
|
return attachments;
|
|
}
|
|
|
|
article.attachments.map((attachment) => {
|
|
if (partial) {
|
|
const fileName = `${partial.id}-part_${partial.number}-attachment`;
|
|
const filePath = `./partials/${fileName}`
|
|
writeFileSync(filePath, attachment.content);
|
|
}
|
|
|
|
attachments.push({
|
|
file: partial ? undefined : new File([attachment.content], attachment.filename ?? crypto.randomUUID(), { type: attachment.contentType }),
|
|
fileName: attachment.filename ?? '',
|
|
size: attachment.size,
|
|
checksum: attachment.checksum,
|
|
contentType: attachment.contentType,
|
|
})
|
|
});
|
|
|
|
return attachments;
|
|
}
|