import Inject, { RegisterScoped } from "@entity-access/entity-access/dist/di/di.js"; import EncryptionService from "../encryption/EncryptionService.js"; import { globalEnv } from "../../../common/globalEnv.js"; import AppFile from "../../model/entities/AppFile.js"; import SessionSecurity from "@entity-access/server-pages/dist/services/SessionSecurity.js"; export interface IFileUrlOptions { eDomain?: string; postID?: any; file: AppFile; size?: any; type?: "webp" | "webm" | "jpg" | "pdf" | "png" | "html" | "txt"; ext?: string; publicRead?: boolean; } @RegisterScoped export default class ChannelUrlService { @Inject ecs: EncryptionService; @Inject ss: SessionSecurity; toWebMLinkUrl({ file, eDomain }: IFileUrlOptions): string { const cid = this.ecs.general.encrypt(file.fileContentID.toString()); return `https://${globalEnv.cdnOrHost}/social-mail/video-player/${eDomain}/webm-branded/${cid}/${file.name}.b1.webm`; } toWebMUrl({ eDomain, file, publicRead }: IFileUrlOptions): string { const cid = this.ecs.general.encrypt(file.fileContentID.toString()); if (eDomain) { return `https://${globalEnv.cdnOrHost}/social-mail/c/e/${eDomain}/webm-branded/${cid}/${file.name}.b1.webm`; } if (publicRead) { this.ss.injectPublicKey(file); } else { this.ss.injectKey(file); } const eFileID = (file as any).$key; return `https://${globalEnv.host}/social-mail/fd/webm/${eFileID}/${cid}/${file.name}.webm`; } toChannelFileUrl(fo: IFileUrlOptions) { if (/video/i.test(fo.file.contentType)) { return this.toWebMUrl(fo); } if (/image/i.test(fo.file.contentType)) { return this.toImageUrl(fo); } return this.toFileUrl(fo); } toImageUrl({ eDomain, file, size = 700, type = "webp", ext = type, publicRead, }: IFileUrlOptions): string { const cid = this.ecs.general.encrypt(file.fileContentID.toString()); let method; let name; if (size) { method = `${type}(${size})`; name = `${file.name}.i1-${size}.${ext}`; } else { method = type; name = `${file.name}.i1-${ext}`; } if (eDomain) { return `https://${globalEnv.cdnOrHost}/social-mail/c/e/${eDomain}/${method}/${cid}/${name}`; } if (publicRead) { this.ss.injectPublicKey(file); } else { this.ss.injectKey(file); } const eFileID = (file as any).$key; return `https://${globalEnv.host}/social-mail/fd/${method}/${eFileID}/${cid}/${name}`; } toFileUrl({ eDomain, file, publicRead, }: IFileUrlOptions) { const cid = this.ecs.general.encrypt(file.fileContentID.toString()); const method = "view"; const name = file.name; if(eDomain) { return `https://${globalEnv.cdnOrHost}/social-mail/c/e/${eDomain}/${method}/${cid}/${name}`; } if (publicRead) { this.ss.injectPublicKey(file); } else { this.ss.injectKey(file); } const eFileID = (file as any).$key; return `https://${globalEnv.host}/social-mail/fd/${method}/${eFileID}/${cid}/${name}`; } }