import Inject from "@entity-access/entity-access/dist/di/di.js"; import { TempFileResult } from "@entity-access/server-pages/dist/Content.js"; import Page from "@entity-access/server-pages/dist/Page.js"; import { Prepare } from "@entity-access/server-pages/dist/decorators/Prepare.js"; import SocialMailContext from "../../../../../server/model/SocialMailContext.js"; import FileConversionService from "../../../../../server/services/convert/FileConversionService.js"; @Prepare.authorize export default class extends Page { @Inject private fcs: FileConversionService; @Inject private db: SocialMailContext; async run() { const type = this.childPath[0]; // we will not convert as BigInt might have loss of data const emailID = this.childPath[1] as unknown as number; const fileContentID = this.childPath[2] as unknown as number; if(!await this.db.emails.filtered("read").where({ emailID }, (x, p) => x.emailID === p.emailID) .some()) { return this.notFound(true); } const appFile = await this.db.appFiles.where({ emailID, fileContentID}, (x, p) => x.emailID === p.emailID && x.fileContentID === p.fileContentID) .first(); this.cacheControl = "public, max-age=2592000, immutable"; const key = this.request.path; const fileName = this.childPath[this.childPath.length-1]; const file = await this.fcs.downloadConvertedFile({ type, key, appFile, fileName }); return new TempFileResult( file, { contentDisposition: type === "download" ? "attachment" : "inline", immutable: true, etag: false } ); } }