import Inject, { RegisterScoped } from "@entity-access/entity-access/dist/di/di.js"; import { LocalFile } from "@entity-access/server-pages/dist/core/LocalFile.js"; import SocialMailContext from "../../model/SocialMailContext.js"; import BaseStorage from "../../storage/BaseStorage.js"; import TempFileService from "../../storage/TempFileService.js"; import GZip from "../../storage/compression/gz.js"; import DateTime from "@entity-access/entity-access/dist/types/DateTime.js"; import { randomUUID } from "crypto"; import sleep from "../../../common/sleep.js"; import Sql from "@entity-access/entity-access/dist/sql/Sql.js"; import { AppStringHelper } from "../../../common/AppStringHelper.js"; @RegisterScoped export default class EmailLogService { @Inject db: SocialMailContext; @Inject storage: BaseStorage; @Inject tfs: TempFileService; async saveText(emailID, title: string, log: string) { const { tfs } = this; // postgres does not store utf 0x00 log = AppStringHelper.remove00(log); const file = await tfs.createFrom(title, Buffer.from(log, "utf-8"), "text/plain"); return this.save(emailID, file); } async deleteAll(emailID: number, updateStorage = true) { const logs = await this.db.emailLogs.where({ emailID }, (x, p) => x.emailID === p.emailID) .map(({ logID, cloudPath, fileSize }) => ({ logID, cloudPath, fileSize })) .toArray(); await Promise.all(logs.map((x) => this.storage.deleteFile(x.cloudPath))); for (const element of logs) { await this.db.emailLogs.statements.delete(element); if(updateStorage) { await this.updateStorage({ emailID, size: -element.fileSize}); } } } async delete(logID: number) { await using tx = await this.db.connection.createTransaction(); const log = await this.db.emailLogs.statements.select({}, { logID}); await this.storage.deleteFile(log.cloudPath); await this.db.emailLogs.statements.delete({ logID }); await tx.commit(); await this.updateStorage({ emailID: log.emailID, size: -log.fileSize}); } async save(emailID: number, file: LocalFile, compression: string = "gz") { const { db, tfs } = this; const dateCreated = DateTime.now; const cloudPath = `email-files/${dateCreated.year}-${String(dateCreated.month).padStart(2, '0')}-${String(dateCreated.day).padStart(2, '0')}/${randomUUID()}/${file.fileName}/email.log.gz`; const compressed = await tfs.createFrom(file.fileName + ".gz", GZip.zip(file.openRead()), "text/plain"); await this.storage.createFile(cloudPath, compressed); const size = file.contentSize; const log = await db.emailLogs.statements.insert({ emailID, fileName: file.fileName, fileSize: size, dateCreated, cloudPath, compression }); if(emailID) { await this.updateStorage({ emailID, size}); } return log; } async load({ logID = 0, cloudPath = "", compression = "gz", fileName = "file.log"}) { const { tfs } = this; if (!cloudPath) { const e = await this.db.emailLogs.where({ logID }, (x, p) => x.logID === p.logID).first(); cloudPath = e.cloudPath; compression = e.compression; fileName = e.fileName; } const s = await this.storage.streamFile(cloudPath); const tf = await tfs.createFrom(fileName, s.stream, s.contentType); if (compression) { return await tfs.createFrom(fileName, GZip.unzipStream(tf.openReadStream()), s.contentType); } return tf; } async updateStorage({ emailID, size }) { const { db } = this; let lastError = null; // we will make sure that we succeed once... for (let index = 0; index < 3; index++) { try { await db.mailboxes.where({ emailID}, (x, p) => x.emails.some((e) => e.emailID === p.emailID)) .update({ size }, (x, p) => ({ storage: x.storage + Sql.cast.asInteger(p.size), dateUpdated: Sql.date.now() })); } catch (error) { lastError = error; await sleep(1000); continue; } return; } if (lastError) { console.error(lastError); } } }