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 FileConversionService from "../../../../server/services/convert/FileConversionService.js"; import ReplicaReader from "../../../../server/services/replica/ReplicaReader.js"; import SocialMailContext from "../../../../server/model/SocialMailContext.js"; @Prepare.authorize export default class extends Page { @Inject private fcs: FileConversionService; @Inject private replica: ReplicaReader; @Inject private db: SocialMailContext; async run() { const type = this.childPath[0]; // we will not convert as BigInt might have loss of data const fileID = this.childPath[1] as unknown as number; let fileContentID = this.childPath[2] as unknown as number; const fileName = this.childPath[this.childPath.length-1]; const appFile = await this.replica.query(async (db) => { if (!fileContentID) { // redirect... const fc1 = await db.appFiles.where({ fileID }, (x, p) => x.appFileID === p.fileID) .select((x) => ({fileContentID: x.fileContentID})) .first(); if (!fc1) { return null; } fileContentID = fc1.fileContentID; } db.verifyFilters = true; return await db.appFiles .filtered("read") .where({ fileID, fileContentID }, (x, p) => x.fileContentID === p.fileContentID && x.appFileID === p.fileID) .include((x) => x.fileContent) .first(); }); if (!appFile) { return this.notFound(); } this.db.verifyFilters = false; this.db.raiseEvents = false; this.cacheControl = "public, max-age=2592000, immutable"; const file = await this.fcs.downloadConvertedFile({ appFile, fileName, fileContentID, type }); return new TempFileResult( file, { contentDisposition: type === "download" ? "attachment" : "inline", immutable: true, etag: false } ); } }