mirror of
https://github.com/dreamhunter2333/cloudflare_temp_email.git
synced 2026-07-23 13:26:53 +08:00
feat: attachment viewing function (#58)
This commit is contained in:
@@ -16,7 +16,7 @@ api.get('/api/mails', async (c) => {
|
||||
return c.text("Invalid offset", 400)
|
||||
}
|
||||
const { results } = await c.env.DB.prepare(
|
||||
`SELECT id, source, subject, message FROM mails where address = ? order by id desc limit ? offset ?`
|
||||
`SELECT id, source, subject, message, message_id FROM mails where address = ? order by id desc limit ? offset ?`
|
||||
).bind(address, limit, offset).all();
|
||||
let count = 0;
|
||||
if (offset == 0) {
|
||||
@@ -25,6 +25,23 @@ api.get('/api/mails', async (c) => {
|
||||
).bind(address).first();
|
||||
count = mailCount;
|
||||
}
|
||||
// add attachments
|
||||
let attachmentResults = [];
|
||||
const message_ids = results.map((r) => r.message_id).filter((r) => r);
|
||||
console.log("message_ids", message_ids.map((id) => `'${id}'`).join(","));
|
||||
if (message_ids && message_ids.length > 0) {
|
||||
const { results: innerAttachmentResults } = await c.env.DB.prepare(
|
||||
`SELECT id, message_id FROM attachments where message_id in (${message_ids.map((id) => `'${id}'`).join(",")})`
|
||||
).all();
|
||||
attachmentResults = innerAttachmentResults || [];
|
||||
}
|
||||
results.forEach((r) => {
|
||||
const attachment_id = attachmentResults.filter((ar) => ar.message_id == r.message_id).map((ar) => ar.id);
|
||||
if (attachment_id && attachment_id.length > 0) {
|
||||
r.attachment_id = attachment_id[0];
|
||||
}
|
||||
delete r.message_id;
|
||||
})
|
||||
return c.json({
|
||||
results: results,
|
||||
count: count
|
||||
@@ -34,7 +51,7 @@ api.get('/api/mails', async (c) => {
|
||||
api.get('/api/settings', async (c) => {
|
||||
const { address } = c.get("jwtPayload")
|
||||
const results = await c.env.DB.prepare(
|
||||
`SELECT * FROM auto_reply_mails where address = ?`
|
||||
`SELECT * FROM auto_reply_mails where address = ? `
|
||||
).bind(address).first();
|
||||
if (!results) {
|
||||
return c.json({
|
||||
@@ -68,9 +85,9 @@ api.post('/api/settings', async (c) => {
|
||||
const { success } = await c.env.DB.prepare(
|
||||
`INSERT OR REPLACE INTO
|
||||
auto_reply_mails
|
||||
(name, address, source_prefix, subject, message, enabled)
|
||||
(name, address, source_prefix, subject, message, enabled)
|
||||
VALUES
|
||||
(?, ?, ?, ?, ?, ?)`
|
||||
(?, ?, ?, ?, ?, ?)`
|
||||
).bind(name || '', address, source_prefix || '', subject || '', message || '', enabled ? 1 : 0).run();
|
||||
if (!success) {
|
||||
return c.text("Failed to save settings", 500)
|
||||
@@ -226,7 +243,7 @@ api.get('/admin/mails_unknow', async (c) => {
|
||||
const { count: mailCount } = await c.env.DB.prepare(`
|
||||
SELECT count(*) as count FROM mails
|
||||
where address NOT IN
|
||||
(select concat('${c.env.PREFIX}', name) from address)`
|
||||
(select concat('${c.env.PREFIX}', name) from address)`
|
||||
).first();
|
||||
count = mailCount;
|
||||
}
|
||||
@@ -236,5 +253,16 @@ api.get('/admin/mails_unknow', async (c) => {
|
||||
})
|
||||
});
|
||||
|
||||
// attachments
|
||||
api.get("/api/attachment/:attachment_id", async (c) => {
|
||||
const { attachment_id } = c.req.param();
|
||||
const { data } = await c.env.DB.prepare(
|
||||
`SELECT data FROM attachments where id = ? `
|
||||
).bind(attachment_id).first();
|
||||
if (!data) {
|
||||
return c.text("Not found", 404)
|
||||
}
|
||||
return c.json(JSON.parse(data))
|
||||
})
|
||||
|
||||
export { api }
|
||||
|
||||
Reference in New Issue
Block a user