优化多个地方
This commit is contained in:
@@ -12,6 +12,7 @@ import utc from 'dayjs/plugin/utc';
|
||||
import timezone from 'dayjs/plugin/timezone';
|
||||
import roleService from '../service/role-service';
|
||||
import verifyUtils from '../utils/verify-utils';
|
||||
import r2Service from '../service/r2-service';
|
||||
|
||||
dayjs.extend(utc);
|
||||
dayjs.extend(timezone);
|
||||
@@ -152,7 +153,7 @@ export async function email(message, env, ctx) {
|
||||
attachment.accountId = emailRow.accountId;
|
||||
});
|
||||
|
||||
if (attachments.length > 0 && env.r2) {
|
||||
if (attachments.length > 0 && await r2Service.hasOSS({env})) {
|
||||
await attService.addAtt({ env }, attachments);
|
||||
}
|
||||
|
||||
|
||||
@@ -14,15 +14,15 @@ app.onError((err, c) => {
|
||||
}
|
||||
|
||||
if (err.message === `Cannot read properties of undefined (reading 'get')`) {
|
||||
return c.text('初始化失败:KV数据库未绑定或变量名错误 Initialization failed: KV database not bound or invalid variable name');
|
||||
return c.json(result.fail('初始化失败:KV数据库未绑定或变量名错误'));
|
||||
}
|
||||
|
||||
if (err.message === `Cannot read properties of undefined (reading 'put')`) {
|
||||
return c.text('初始化失败:KV数据库未绑定或变量名错误 Initialization failed: KV database not bound or invalid variable name');
|
||||
return c.json(result.fail('初始化失败:KV数据库未绑定或变量名错误'));
|
||||
}
|
||||
|
||||
if (err.message === `Cannot read properties of undefined (reading 'prepare')`) {
|
||||
return c.text('初始化失败:D1数据库未绑定或变量名错误 Initialization failed: D1 database not bound or invalid variable name');
|
||||
return c.json(result.fail('初始化失败:D1数据库未绑定或变量名错误'));
|
||||
}
|
||||
|
||||
return c.json(result.fail(err.message, err.code));
|
||||
|
||||
@@ -96,7 +96,6 @@ const en = {
|
||||
"系统设置": "System Settings",
|
||||
"设置查看": "View Settings",
|
||||
"设置修改": "Change Settings",
|
||||
"物理清空": "Physical Purge",
|
||||
"发件重置": "Reset Send Count"
|
||||
}
|
||||
};
|
||||
|
||||
@@ -96,7 +96,6 @@ const zh = {
|
||||
"系统设置": "系统设置",
|
||||
"设置查看": "设置查看",
|
||||
"设置修改": "设置修改",
|
||||
"物理清空": "物理清空",
|
||||
'发件重置': '发件重置'
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,6 @@ const init = {
|
||||
await this.v1_5DB(c);
|
||||
await this.v1_6DB(c);
|
||||
await this.v1_7DB(c);
|
||||
await this.v1_7DB(c);
|
||||
await this.v2DB(c);
|
||||
await settingService.refresh(c);
|
||||
return c.text(t('initSuccess'));
|
||||
@@ -34,7 +33,8 @@ const init = {
|
||||
c.env.db.prepare(`ALTER TABLE setting ADD COLUMN region TEXT NOT NULL DEFAULT '';`),
|
||||
c.env.db.prepare(`ALTER TABLE setting ADD COLUMN endpoint TEXT NOT NULL DEFAULT '';`),
|
||||
c.env.db.prepare(`ALTER TABLE setting ADD COLUMN s3_access_key TEXT NOT NULL DEFAULT '';`),
|
||||
c.env.db.prepare(`ALTER TABLE setting ADD COLUMN s3_secret_key TEXT NOT NULL DEFAULT '';`)
|
||||
c.env.db.prepare(`ALTER TABLE setting ADD COLUMN s3_secret_key TEXT NOT NULL DEFAULT '';`),
|
||||
c.env.db.prepare(`DELETE FROM perm WHERE perm_key = 'setting:clean'`)
|
||||
]);
|
||||
} catch (e) {
|
||||
console.error(e.message)
|
||||
|
||||
@@ -1,21 +1,31 @@
|
||||
import orm from '../entity/orm';
|
||||
import { att } from '../entity/att';
|
||||
import { and, eq, isNull, inArray, notInArray } from 'drizzle-orm';
|
||||
import { and, eq, isNull, inArray } from 'drizzle-orm';
|
||||
import r2Service from './r2-service';
|
||||
import constant from '../const/constant';
|
||||
import fileUtils from '../utils/file-utils';
|
||||
import { attConst } from '../const/entity-const';
|
||||
import { parseHTML } from 'linkedom';
|
||||
import domainUtils from '../utils/domain-uitls';
|
||||
|
||||
const attService = {
|
||||
|
||||
async addAtt(c, attachments) {
|
||||
|
||||
for (let attachment of attachments) {
|
||||
await r2Service.putObj(c, attachment.key, attachment.content, {
|
||||
|
||||
let metadate = {
|
||||
contentType: attachment.mimeType,
|
||||
contentDisposition: `attachment; filename="${attachment.filename}"`
|
||||
});
|
||||
}
|
||||
|
||||
if (!attachment.contentId) {
|
||||
metadate.contentDisposition = `attachment; filename="${attachment.filename}"`
|
||||
} else {
|
||||
metadate.contentDisposition = `inline; filename="${attachment.filename}"`
|
||||
metadate.cacheControl = `max-age=604800`
|
||||
}
|
||||
|
||||
await r2Service.putObj(c, attachment.key, attachment.content, metadate);
|
||||
}
|
||||
|
||||
await orm(c).insert(att).values(attachments).run();
|
||||
@@ -49,7 +59,7 @@ const attService = {
|
||||
const file = fileUtils.base64ToFile(src);
|
||||
const buff = await file.arrayBuffer();
|
||||
const key = constant.ATTACHMENT_PREFIX + await fileUtils.getBuffHash(buff) + fileUtils.getExtFileName(file.name);
|
||||
img.setAttribute('src', r2Domain + '/' + key);
|
||||
img.setAttribute('src', domainUtils.toOssDomain(r2Domain) + '/' + key);
|
||||
|
||||
const attData = {};
|
||||
attData.key = key;
|
||||
@@ -108,7 +118,9 @@ const attService = {
|
||||
attData.accountId = accountId;
|
||||
attData.type = attConst.type.EMBED;
|
||||
await r2Service.putObj(c, attData.key, attData.buff, {
|
||||
contentType: attData.mimeType
|
||||
contentType: attData.mimeType,
|
||||
cacheControl: `max-age=604800`,
|
||||
contentDisposition: `inline; filename="${attData.filename}"`
|
||||
});
|
||||
}
|
||||
|
||||
@@ -153,9 +165,7 @@ const attService = {
|
||||
await this.batchDelete(c, delKeyList);
|
||||
}
|
||||
|
||||
const delAttSql = fieldValues.map(value => c.env.db.prepare(`DELETE
|
||||
FROM attachments
|
||||
WHERE ${fieldName} = ?`).bind(value));
|
||||
const delAttSql = fieldValues.map(value => c.env.db.prepare(`DELETE FROM attachments WHERE ${fieldName} = ?`).bind(value));
|
||||
await c.env.db.batch(delAttSql);
|
||||
},
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ import dayjs from 'dayjs';
|
||||
import kvConst from '../const/kv-const';
|
||||
import { t } from '../i18n/i18n'
|
||||
import r2Service from './r2-service';
|
||||
import domainUtils from '../utils/domain-uitls';
|
||||
|
||||
const emailService = {
|
||||
|
||||
@@ -407,6 +408,8 @@ const emailService = {
|
||||
|
||||
}
|
||||
|
||||
r2domain = domainUtils.toOssDomain(r2domain)
|
||||
|
||||
if (src && src.startsWith(r2domain + '/')) {
|
||||
img.setAttribute('src', src.replace(r2domain + '/', '{{domain}}'));
|
||||
}
|
||||
|
||||
@@ -9,9 +9,23 @@ const s3Service = {
|
||||
|
||||
const { bucket } = await settingService.query(c);
|
||||
|
||||
await client.send(
|
||||
new PutObjectCommand({ Bucket: bucket, Key: key, Body: content, ...metadata })
|
||||
)
|
||||
let obj = { Bucket: bucket, Key: key, Body: content,
|
||||
CacheControl: metadata.cacheControl
|
||||
}
|
||||
|
||||
if (metadata.cacheControl) {
|
||||
obj.CacheControl = metadata.cacheControl
|
||||
}
|
||||
|
||||
if (metadata.contentDisposition) {
|
||||
obj.ContentDisposition = metadata.contentDisposition
|
||||
}
|
||||
|
||||
if (metadata.contentType) {
|
||||
obj.ContentType = metadata.contentType
|
||||
}
|
||||
|
||||
await client.send(new PutObjectCommand(obj))
|
||||
},
|
||||
|
||||
async deleteObj(c,keys) {
|
||||
|
||||
@@ -119,7 +119,9 @@ const settingService = {
|
||||
|
||||
|
||||
await r2Service.putObj(c, background, arrayBuffer, {
|
||||
contentType: file.type
|
||||
contentType: file.type,
|
||||
cacheControl: `public, max-age=31536000, immutable`,
|
||||
contentDisposition: `inline; filename="${file.name}"`
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@@ -34,3 +34,6 @@ crons = ["0 16 * * *"] #定时任务每天晚上12点执行
|
||||
#domain = [] #邮件域名可可配置多个 示例: ["example1.com","example2.com"]
|
||||
#admin = "" #管理员的邮箱 示例: admin@example.com
|
||||
#jwt_secret = "" #jwt令牌的密钥,随便填一串字符串
|
||||
|
||||
[build]
|
||||
command = "pnpm --prefix ../mail-vue install && pnpm --prefix ../mail-vue run build"
|
||||
|
||||
@@ -18,9 +18,10 @@ database_id = "a4c1a63a-6ef5-4e6d-8e8c-b6d9e8feb810"
|
||||
binding = "kv"
|
||||
id = "2io01d4b299e481b9de060ece9e7785c"
|
||||
|
||||
[[r2_buckets]]
|
||||
binding = "r2"
|
||||
bucket_name = "email"
|
||||
#[[r2_buckets]]
|
||||
#binding = "r2"
|
||||
#bucket_name = "email"
|
||||
|
||||
|
||||
[vars]
|
||||
orm_log = false
|
||||
|
||||
@@ -1,36 +1,40 @@
|
||||
name = "cloud-mail-test"
|
||||
name = "test-mail"
|
||||
main = "src/index.js"
|
||||
compatibility_date = "2025-06-04"
|
||||
keep_vars = true
|
||||
|
||||
[observability]
|
||||
enabled = true
|
||||
head_sampling_rate = 1
|
||||
|
||||
[[d1_databases]]
|
||||
binding = "db"
|
||||
database_name = "email-test"
|
||||
database_id = ""
|
||||
binding = "db" #d1数据库绑定名默认不可修改
|
||||
database_name = "test-email" #d1数据库名字
|
||||
database_id = "e65f099d-796d-4eaa-8dff-529b368b20db" #d1数据库id
|
||||
|
||||
[[kv_namespaces]]
|
||||
binding = "kv"
|
||||
id = ""
|
||||
binding = "kv" #kv绑定名默认不可修改
|
||||
id = "9be10cd058e04c55b526f8c0c116f50c" #kv数据库id
|
||||
|
||||
[[r2_buckets]]
|
||||
binding = "r2"
|
||||
bucket_name = "email-test"
|
||||
#(可选)
|
||||
#[[r2_buckets]]
|
||||
#binding = "r2" #r2对象存储绑定名默认不可修改
|
||||
#bucket_name = "test-email" #r2对象存储桶的名字
|
||||
|
||||
[assets]
|
||||
binding = "assets"
|
||||
directory = "./dist"
|
||||
binding = "assets" #静态资源绑定名默认不可修改
|
||||
directory = "./dist" #前端vue项目打包的静态资源存放位置,默认dist
|
||||
not_found_handling = "single-page-application"
|
||||
run_worker_first = true
|
||||
|
||||
[triggers]
|
||||
crons = ["0 16 * * *"]
|
||||
crons = ["0 16 * * *"] #定时任务每天晚上12点执行
|
||||
|
||||
|
||||
[vars]
|
||||
orm_log = true
|
||||
domain = ["", ""]
|
||||
admin = ""
|
||||
jwt_secret = ""
|
||||
orm_log = false
|
||||
domain = ["example.com"] #邮件域名可可配置多个 示例: ["example1.com","example2.com"]
|
||||
admin = "admin@example.com" #管理员的邮箱 示例: admin@example.com
|
||||
jwt_secret = "b7f29a1d-18e2-4d3b-941f-f6b2c97c02fd" #jwt令牌的密钥,随便填一串字符串
|
||||
|
||||
[build]
|
||||
command = "pnpm --prefix ../mail-vue install && pnpm --prefix ../mail-vue run build"
|
||||
|
||||
@@ -8,16 +8,17 @@ enabled = true
|
||||
|
||||
#[[d1_databases]]
|
||||
#binding = "db" #d1数据库绑定名默认不可修改
|
||||
#database_name = "" #d1数据库名字
|
||||
#database_id = "" #d1数据库id
|
||||
|
||||
#database_name = "email" #d1数据库名字
|
||||
#database_id = "b1b1a63a-6ef5-4e6d-8e8c-b6d9e8feb810" #d1数据库id
|
||||
#
|
||||
#[[kv_namespaces]]
|
||||
#binding = "kv" #kv绑定名默认不可修改
|
||||
#id = "" #kv数据库id
|
||||
#id = "0fa01d4b299e481b9de060ece9e7785c" #kv数据库id
|
||||
|
||||
##(可选)
|
||||
#[[r2_buckets]]
|
||||
#binding = "r2" #r2对象存储绑定名默认不可修改
|
||||
#bucket_name = "" #r2对象存储桶的名字
|
||||
#bucket_name = "email" #r2对象存储桶的名字
|
||||
|
||||
[assets]
|
||||
binding = "assets" #静态资源绑定名默认不可修改
|
||||
|
||||
Reference in New Issue
Block a user