feat: use ArrayBuffer for attachment sending
This commit is contained in:
@@ -422,7 +422,7 @@ const emailService = {
|
|||||||
subject: params.subject,
|
subject: params.subject,
|
||||||
text: params.text,
|
text: params.text,
|
||||||
html: params.html,
|
html: params.html,
|
||||||
attachments: params.attachments
|
attachments: await this.toArrayBufferAttachments(params.attachments)
|
||||||
};
|
};
|
||||||
|
|
||||||
if (params.sendType === 'reply') {
|
if (params.sendType === 'reply') {
|
||||||
@@ -436,34 +436,11 @@ const emailService = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
async toCloudflareAttachments(attachments) {
|
async toCloudflareAttachments(attachments) {
|
||||||
|
const arrayBufferAttachments = await this.toArrayBufferAttachments(attachments);
|
||||||
|
|
||||||
const result = [];
|
return arrayBufferAttachments.map(attachment => {
|
||||||
|
|
||||||
for (const attachment of attachments) {
|
|
||||||
let content = attachment.content;
|
|
||||||
|
|
||||||
if (!content && attachment.path) {
|
|
||||||
const response = await fetch(attachment.path);
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new BizError(`Attachment fetch failed: ${attachment.filename}`);
|
|
||||||
}
|
|
||||||
content = await response.arrayBuffer();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!content) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof content === 'string' && content.startsWith('data:')) {
|
|
||||||
content = content.split(',')[1] || content;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof content === 'string') {
|
|
||||||
content = fileUtils.base64ToUint8Array(content.replace(/\s+/g, '')).buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
const item = {
|
const item = {
|
||||||
content,
|
content: attachment.content,
|
||||||
filename: attachment.filename,
|
filename: attachment.filename,
|
||||||
type: attachment.mimeType || attachment.contentType || attachment.type || 'application/octet-stream',
|
type: attachment.mimeType || attachment.contentType || attachment.type || 'application/octet-stream',
|
||||||
disposition: attachment.contentId ? 'inline' : 'attachment'
|
disposition: attachment.contentId ? 'inline' : 'attachment'
|
||||||
@@ -473,12 +450,58 @@ const emailService = {
|
|||||||
item.contentId = attachment.contentId.replace(/^<|>$/g, '');
|
item.contentId = attachment.contentId.replace(/^<|>$/g, '');
|
||||||
}
|
}
|
||||||
|
|
||||||
result.push(item);
|
return item;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
async toArrayBufferAttachments(attachments = []) {
|
||||||
|
const result = [];
|
||||||
|
|
||||||
|
for (const attachment of attachments) {
|
||||||
|
const content = await this.toAttachmentArrayBuffer(attachment);
|
||||||
|
if (!content) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
result.push({ ...attachment, content });
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
async toAttachmentArrayBuffer(attachment) {
|
||||||
|
let content = attachment.content;
|
||||||
|
|
||||||
|
if (!content && attachment.path) {
|
||||||
|
const response = await fetch(attachment.path);
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new BizError(`Attachment fetch failed: ${attachment.filename}`);
|
||||||
|
}
|
||||||
|
return await response.arrayBuffer();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!content) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (content instanceof ArrayBuffer) {
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (content instanceof Uint8Array) {
|
||||||
|
return content.buffer.slice(content.byteOffset, content.byteOffset + content.byteLength);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof content === 'string') {
|
||||||
|
if (content.startsWith('data:')) {
|
||||||
|
content = content.split(',')[1] || content;
|
||||||
|
}
|
||||||
|
return fileUtils.base64ToUint8Array(content.replace(/\s+/g, '')).buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
return content;
|
||||||
|
},
|
||||||
|
|
||||||
//处理站内邮件发送
|
//处理站内邮件发送
|
||||||
async HandleOnSiteEmail(c, receiveEmail, sendEmailData, attList) {
|
async HandleOnSiteEmail(c, receiveEmail, sendEmailData, attList) {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user