From 8930bb11fce19c773201b4f499d99c1c3c28efa6 Mon Sep 17 00:00:00 2001 From: Yuqian Yang Date: Wed, 30 Apr 2025 00:20:23 +0800 Subject: HALF WORK!: 2025-5-14 2 --- .../docker/mail-server/aws-sendmail/aws/app.ts | 22 +++++ .../mail-server/aws-sendmail/aws/retriever.ts | 90 ++++++++++++++++++++ .../mail-server/aws-sendmail/aws/retriver.ts | 98 ---------------------- .../docker/mail-server/aws-sendmail/aws/service.ts | 48 +++++++++++ 4 files changed, 160 insertions(+), 98 deletions(-) create mode 100644 services/docker/mail-server/aws-sendmail/aws/app.ts create mode 100644 services/docker/mail-server/aws-sendmail/aws/retriever.ts delete mode 100644 services/docker/mail-server/aws-sendmail/aws/retriver.ts create mode 100644 services/docker/mail-server/aws-sendmail/aws/service.ts (limited to 'services/docker/mail-server/aws-sendmail/aws') diff --git a/services/docker/mail-server/aws-sendmail/aws/app.ts b/services/docker/mail-server/aws-sendmail/aws/app.ts new file mode 100644 index 0000000..e25c92c --- /dev/null +++ b/services/docker/mail-server/aws-sendmail/aws/app.ts @@ -0,0 +1,22 @@ +import { Hono } from "https://jsr.io/@hono/hono/4.7.9/src/hono.ts"; +import { AppBase } from "../app.ts"; +import { MailDeliverer } from "../mail.ts"; +import { AwsContext } from "./context.ts"; +import { AwsMailDeliverer } from "./deliver.ts"; + +export class AwsRelayApp extends AppBase { + private readonly context = new AwsContext(); + private readonly deliverer = new AwsMailDeliverer(this.context); + + constructor() { + super(); + } + + protected override setupHono(_hono: Hono): Promise { + + } + + protected override get outboundMailDeliverer(): MailDeliverer { + return this.deliverer; + } +} diff --git a/services/docker/mail-server/aws-sendmail/aws/retriever.ts b/services/docker/mail-server/aws-sendmail/aws/retriever.ts new file mode 100644 index 0000000..de577b0 --- /dev/null +++ b/services/docker/mail-server/aws-sendmail/aws/retriever.ts @@ -0,0 +1,90 @@ +/// + +import { + GetObjectCommand, + ListObjectsV2Command, + S3Client, +} from "@aws-sdk/client-s3"; + +import { AwsContext, s3MoveObject } from "./context.ts"; +import { getLogger } from "../logger.ts"; +import { getConfig } from "../config.ts"; +import { Mail } from "../mail.ts"; + +export class AwsMailRetriever { + readonly liveMailPrefix = "mail/live/"; + readonly archiveMailPrefix = "mail/archive/"; + readonly mailBucket = getConfig().getValue("awsMailBucket"); + + private readonly s3Client; + + constructor( + aws: AwsContext, + private readonly callback: (mail: Mail) => Promise, + ) { + const { region, credentials } = aws; + this.s3Client = new S3Client({ region, credentials }); + } + + async listLiveMails(): Promise { + const listCommand = new ListObjectsV2Command({ + Bucket: this.mailBucket, + Prefix: this.liveMailPrefix, + }); + const res = await this.s3Client.send(listCommand); + + if (res.Contents == null) { + getLogger().warn("Listing live mails in S3 returns null Content."); + return []; + } + + const result: string[] = []; + for (const object of res.Contents) { + if (object.Key != null) { + // TODO: check prefix consistence here. + result.push(object.Key.slice(this.liveMailPrefix.length)); + } else { + getLogger().warn( + "Listing live mails in S3 returns an object with no Key.", + ); + } + } + return result; + } + + async deliverS3MailObject(messageId: string) { + const mailPath = `${this.liveMailPrefix}${messageId}`; + const command = new GetObjectCommand({ + Bucket: this.mailBucket, + Key: mailPath, + }); + const res = await this.s3Client.send(command); + + if (res.Body == null) { + // TODO: Better error. + throw new Error(); + } + + const rawMail = await res.Body.transformToString(); + const mail = new Mail(rawMail); + mail.awsMessageId = messageId; + await this.callback(mail); + + // TODO: Continue here. + await s3MoveObject( + this.s3Client, + this.mailBucket, + mailPath, + `${this.archiveMailPrefix}${ + mail.simpleGetDateString("invalid-date") + }/${messageId}`, + ); + } + + async recycleLiveMails() { + const mails = await this.listLiveMails(); + for (const messageId of mails) { + await this.deliverS3MailObject(messageId); + } + } +} diff --git a/services/docker/mail-server/aws-sendmail/aws/retriver.ts b/services/docker/mail-server/aws-sendmail/aws/retriver.ts deleted file mode 100644 index b599c15..0000000 --- a/services/docker/mail-server/aws-sendmail/aws/retriver.ts +++ /dev/null @@ -1,98 +0,0 @@ -/// - -import { - GetObjectCommand, - ListObjectsV2Command, - S3Client, -} from "@aws-sdk/client-s3"; - -import { AwsContext, s3MoveObject } from "./context.ts"; -import { getLogger } from "../logger.ts"; -import { getConfig } from "../config.ts"; -import { Mail } from "../mail.ts"; - -export class AwsMailRetriever { - readonly liveMailPrefix = "mail/live/"; - readonly archiveMailPrefix = "mail/archive/"; - readonly mailBucket = getConfig().getValue("awsMailBucket"); - - private readonly s3Client; - private readonly liveMailRecyclerAborter = new AbortController(); - - constructor( - aws: AwsContext, - private readonly callback: (mail: Mail) => Promise, - ) { - const { region, credentials } = aws; - this.s3Client = new S3Client({ region, credentials }); - } - - setupLiveMailRecycler() { - Deno.cron("live-mail-recycler", "0 */6 * * *", { - signal: this.liveMailRecyclerAborter.signal, - }, () => { - }); - } - - async listLiveMails(): Promise { - const listCommand = new ListObjectsV2Command({ - Bucket: this.mailBucket, - Prefix: this.liveMailPrefix, - }); - const res = await this.s3Client.send(listCommand); - - if (res.Contents == null) { - getLogger().warn("Listing live mails in S3 returns null Content."); - return []; - } - - const result: string[] = []; - for (const object of res.Contents) { - if (object.Key != null) { - // TODO: check prefix consistence here. - result.push(object.Key.slice(this.liveMailPrefix.length)); - } else { - getLogger().warn( - "Listing live mails in S3 returns an object with no Key.", - ); - } - } - return result; - } - - async deliverS3MailObject(messageId: string) { - const mailPath = `${this.liveMailPrefix}${messageId}`; - const command = new GetObjectCommand({ - Bucket: this.mailBucket, - Key: mailPath, - }); - const res = await this.s3Client.send(command); - - if (res.Body == null) { - // TODO: Better error. - throw new Error(); - } - - const rawMail = await res.Body.transformToString(); - const mail = new Mail(rawMail); - mail.awsMessageId = messageId; - await this.callback(mail); - - // TODO: Continue here. - await s3MoveObject( - this.s3Client, - this.mailBucket, - mailPath, - `${this.archiveMailPrefix}${ - mail.simpleGetDateString("invalid-date") - }/${messageId}`, - ); - } - - async recycleLiveMails() { - const mails = await this.listLiveMails(); - for (const messageId of mails) { - await this.deliverS3MailObject(messageId); - } - } -} diff --git a/services/docker/mail-server/aws-sendmail/aws/service.ts b/services/docker/mail-server/aws-sendmail/aws/service.ts new file mode 100644 index 0000000..d0db7ae --- /dev/null +++ b/services/docker/mail-server/aws-sendmail/aws/service.ts @@ -0,0 +1,48 @@ +import { Mail } from "../mail.ts"; +import { AwsContext } from "./context.ts"; +import { AwsMailRetriever } from "./retriever.ts"; + +export interface AwsServiceSetupOptions { + receiveCallback: (mail: Mail) => Promise; +} + +interface Setup { + receiveCallback: (mail: Mail) => Promise; + retriever: AwsMailRetriever; + liveMailRecyclerAborter: AbortController; +} + +export class AwsService implements Disposable { + private _setup: Setup | null = null; + + constructor(private readonly aws: AwsContext) {} + + setup(options: AwsServiceSetupOptions): Disposable { + if (this._setup != null) { + // TODO: Better error. + throw new Error("Aws service has already been set up."); + } + const { receiveCallback } = options; + const liveMailRecyclerAborter = new AbortController(); + const retriever = new AwsMailRetriever(this.aws, receiveCallback); + + Deno.cron("live-mail-recycler", "0 */6 * * *", { + signal: liveMailRecyclerAborter.signal, + }, () => { + retriever.recycleLiveMails(); + }); + + this._setup = { + receiveCallback, + retriever, + liveMailRecyclerAborter, + }; + + return this; + } + + [Symbol.dispose]() { + if (this._setup == null) return; + this._setup.liveMailRecyclerAborter.abort("Aws service is being disposed."); + } +} -- cgit v1.2.3