新增邮件发送和管理员功能
This commit is contained in:
@@ -3,9 +3,7 @@ import emailService from '../service/email-service';
|
||||
import accountService from '../service/account-service';
|
||||
import settingService from '../service/setting-service';
|
||||
import attService from '../service/att-service';
|
||||
import r2Service from '../service/r2-service';
|
||||
import constant from '../const/constant';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import fileUtils from '../utils/file-utils';
|
||||
|
||||
export async function email(message, env, ctx) {
|
||||
@@ -40,35 +38,29 @@ export async function email(message, env, ctx) {
|
||||
accountId: account.accountId
|
||||
};
|
||||
|
||||
const emailRow = await emailService.receive({ env }, params);
|
||||
const attachments = [];
|
||||
const cidAttachments = [];
|
||||
|
||||
if (!env.r2) {
|
||||
console.warn('r2对象存储未配置, 附件取消保存');
|
||||
return;
|
||||
}
|
||||
|
||||
if (email.attachments.length > 0) {
|
||||
|
||||
let attachments = email.attachments.map(item => {
|
||||
let attachment = { ...item };
|
||||
attachment.emailId = emailRow.emailId;
|
||||
attachment.userId = emailRow.userId;
|
||||
attachment.accountId = emailRow.accountId;
|
||||
attachment.key = constant.ATTACHMENT_PREFIX + uuidv4() + fileUtils.getExtFileName(item.filename);
|
||||
attachment.size = item.content.length ?? item.content.byteLength;
|
||||
return attachment;
|
||||
});
|
||||
|
||||
await attService.addAtt({ env }, attachments);
|
||||
|
||||
for (let attachment of attachments) {
|
||||
await r2Service.putObj({ env }, attachment.key, attachment.content, {
|
||||
contentType: attachment.mimeType,
|
||||
contentDisposition: `attachment; filename="${attachment.filename}"`
|
||||
});
|
||||
for(let item of email.attachments) {
|
||||
let attachment = { ...item };
|
||||
attachment.key = constant.ATTACHMENT_PREFIX + await fileUtils.getBuffHash(attachment.content) + fileUtils.getExtFileName(item.filename);
|
||||
attachment.size = item.content.length ?? item.content.byteLength;
|
||||
attachments.push(attachment);
|
||||
if (attachment.contentId) {
|
||||
cidAttachments.push(attachment)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
const emailRow = await emailService.receive({ env }, params, cidAttachments);
|
||||
|
||||
attachments.forEach(attachment => {
|
||||
attachment.emailId = emailRow.emailId;
|
||||
attachment.userId = emailRow.userId;
|
||||
attachment.accountId = emailRow.accountId;
|
||||
})
|
||||
|
||||
await attService.addAtt({ env }, attachments);
|
||||
|
||||
} catch (e) {
|
||||
console.error('邮件接收异常: ', e);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user