import Column from "@entity-access/entity-access/dist/decorators/Column.js"; import Table from "@entity-access/entity-access/dist/decorators/Table.js"; import { JsonProperty, ReadOnlyJsonProperty } from "@entity-access/server-pages/dist/services/ModelService.js"; import FileContent from "./FileContent.js"; import { RelateTo } from "@entity-access/entity-access/dist/decorators/Relate.js"; import Index from "@entity-access/entity-access/dist/decorators/Index.js"; import { Email } from "./Email.js"; import { User } from "./User.js"; import type AppFileTag from "./AppFileTag.js"; import Sql from "@entity-access/entity-access/dist/sql/Sql.js"; import DateTime from "@entity-access/entity-access/dist/types/DateTime.js"; import LoginSession from "./LoginSession.js"; import type UserPin from "./UserPin.js"; import type Contact from "./Contact.js"; import Mailbox from "./Mailbox.js"; import type WebSite from "./WebSite.js"; import type { WebSiteVersion } from "./WebSiteVersion.js"; import { LocalFile } from "@entity-access/server-pages/dist/core/LocalFile.js"; import { apiPath } from "../../../common/globalEnv.js"; import type { StoreItem } from "./StoreItem.js"; import type { StoreMediaFile } from "./StoreMediaFile.js"; import CheckConstraint from "@entity-access/entity-access/dist/decorators/CheckConstraint.js"; import type { StoreJournal } from "./StoreJournal.js"; import type ChannelEmail from "./ChannelEmail.js"; import type { ContactFile } from "./Contact.js"; const accessTypesEnum = ["create", "read", "write", "delegate"] as const; export type fileAccessTypes = typeof accessTypesEnum[number]; const contentDispositions = [ "attachment", "inline"] as const; export type contentDispositionTypes = typeof contentDispositions[number]; /** * Why path does not contain full folder path? * * The answer is for to improve indexing. * 1. Text Column indexes have size limit, so longer file names might not fit in the path. * 2. Usually we will search with in the folder, so we can always check all descendants. * 3. Too long file paths will also require very large index. * * So current path is set as `${parent.path}${id}/`. */ @Table("AppFiles") @Index({ name: "IX_AppFiles_Path", columns: [ { name: (x) => x.path, descending: false, operatorClass: "varchar_pattern" }, { name: (x) => x.isDeleted, descending: false }, ], include: [ (x) => x.contentType, (x) => x.isFolder, (x) => x.hidden ], filter: (x) => x.path !== null }) @Index({ name: "IX_AppFiles_Drives", columns: [ { name: (x) => x.dateUpdated, descending: true } ], filter: (x) => x.path !== null && x.isFolder === true && x.parentID === null }) @Index({ name: "IX_AppFiles_UniqueName", columns: [ { name: (x) => x.parentID, descending: false }, { name: (x) => x.name, descending: false } ], unique: true, filter: (x) => x.parentID !== null && x.isDeleted === false }) @Index({ name: "IX_AppFiles_ParentID", columns: [ { name: (x) => x.parentID, descending: false }, { name: (x) => x.name, descending: false }, { name: (x) => x.isDeleted, descending: false }, ], filter: (x) => x.parentID !== null, include: [ (x) => x.hidden ] }) @CheckConstraint({ name: "CX_AppFiles_Name", filter: (x) => Sql.text.length(x.name) > 0 }) export default class AppFile { @Column({ key: true, generated: "identity", dataType: "BigInt"}) public appFileID: number; /** * There is no need to store first slash as it is useless. * `22/*` can only match with `22/` and `22/5/`. * However, last slash is useful. */ @Column({ dataType: "AsciiChar", length: 400, nullable: true }) @ReadOnlyJsonProperty public path: string; // @Column({ dataType: "Char", nullable: true }) // public displayPath: string; @Column({ dataType: "Boolean", default: () => false}) public isFolder: boolean; @Column({ dataType: "BigInt", nullable: true}) public snapshotParentID: number; @Column({ dataType: "Char", length: 400}) public name: string; @Column({ dataType: "BigInt", default: () => 0}) public fileSize: number; @Column({ dataType: "Char", length: 200}) public contentType: string; @Column({ dataType: "AsciiChar", length: 10, enum: contentDispositions, nullable: true }) public contentDisposition: contentDispositionTypes; @Column({ dataType: "Char", nullable: true }) public contentID: string; @Column({ dataType: "BigInt", nullable: true }) @RelateTo(FileContent, { property: (af) => af.fileContent, inverseProperty: (fc) => fc.appFiles }) public fileContentID: number; public fileContent: FileContent; @Column({ dataType: "BigInt", nullable: true}) @RelateTo(User, { property: (af) => af.creator, inverseProperty: (af) => af.createdFiles, foreignKeyConstraint: { name: "FC_App_Files_Creator_ID", onDelete: "restrict" } }) public creatorID: number; @Column({ dataType: "BigInt", nullable: true }) @RelateTo(AppFile, { property: (af) => af.parent, inverseProperty: (af) => af.children, foreignKeyConstraint: { name: "FK_AppFiles_ParentID", onDelete: "restrict" } }) public parentID: number; @Column({ dataType: "BigInt", nullable: true }) @RelateTo({ type: () => Email, property: (af) => af.email, inverseProperty: (e) => e.attachments, foreignKeyConstraint: { name: "FC_App_Files_Prevent_Attachment_Delete", onDelete: "restrict" } }) public emailID: number; @Column({ dataType: "Boolean", default: () => false}) public isDeleted: boolean; @Column({ dataType: "Boolean", default: () => false}) public hidden: boolean; @Column({ dataType: "Boolean", default: () => false}) @ReadOnlyJsonProperty public isShared: boolean; @Column({ default: () => Sql.date.now() }) @ReadOnlyJsonProperty public dateCreated: DateTime; @Column({ dataType: "Char", length: 200, nullable: true}) public watermark: string; @Column({ nullable: true }) public dateUpdated: DateTime; @Column({ nullable: true, dataType: "Int"}) public versions: number; public email: Email; public parent: AppFile; public children: AppFile[]; public tags: AppFileTag[]; public creator: User; // public originalEmails: Email[]; public file: LocalFile; @JsonProperty() public sourceUrl: string; @JsonProperty() public sourceEmailLogID: number; public accessList: AppFileAccess[]; public fileVersions: AppFileVersion[]; public folderVersions: AppFileVersion[]; public userPins: UserPin[]; public previewContacts: Contact[]; public mailboxLogos: Mailbox[]; public websiteFolder: WebSite; public websiteVersions: WebSiteVersion[]; signed: string; storeJournals: StoreJournal[]; storeItems: StoreItem[]; currentVersionWebSites: WebSite[]; storeMediaFiles: StoreMediaFile[]; channelEmails: ChannelEmail[]; contactFiles: ContactFile[]; @ReadOnlyJsonProperty public get postID() { return 0; } @ReadOnlyJsonProperty public get url() { if (this.emailID) { return apiPath(`/api/emails/attachment/view/${this.emailID}/${this.appFileID}/${this.name}`); } return apiPath(`/api/files/view/${this.appFileID}/${this.fileContentID}/${this.name}`); } public set url(v) { // ignore... } // @ReadOnlyJsonProperty // public get downloadUrl() { // if (this.emailID) { // return apiPath(`/api/emails/attachment/view/${this.emailID}/${this.appFileID}/${this.name}`); // } // return apiPath(`/api/files/download/${this.appFileID}/${this.fileContentID}/${this.name}`); // } // public set downloadUrl(v) { // // ignore... // } // [GraphService.appendToGraph](sp) { // let postID = this.emailID as any; // if (postID) { // const es = ServiceProvider.resolve(sp, EncryptionService); // postID = es.general.encrypt(postID.toString()); // return { // postID, // url: this.url, // downloadUrl: this.downloadUrl // }; // } // return { // url: this.url, // downloadUrl: this.downloadUrl // }; // } } export class ExternalUser { @JsonProperty() emailAddress: string; @JsonProperty() name: string; } @Table("AppFileAccess") @Index({ name: "IX_FileAccess_UserID", columns: [ { name: (x) => x.userID, descending: true } ] }) export class AppFileAccess { // @Column({ dataType: "BigInt", key: true, generated: "identity"}) // public accessID: number; @Column({ dataType: "BigInt", key: true}) @RelateTo(AppFile, { property: (a) => a.file, inverseProperty: (af) => af.accessList, foreignKeyConstraint: { onDelete: "cascade" } }) public fileID: number; @Column({ dataType: "BigInt", key: true}) @RelateTo(User, { property: (a) => a.user, inverseProperty: (u) => u.fileAccessList, foreignKeyConstraint: { name: "FC_AppFileAccess_UserID", onDelete: "cascade" } }) public userID: number; @Column({ dataType: "Char", length: 10, key: true, order: 3, enum: accessTypesEnum}) public type: fileAccessTypes; @Column({ dataType: "Char", length: 200, nullable: true}) @ReadOnlyJsonProperty public accessCode: string; @Column({ nullable: true }) public expires: DateTime; @Column({}) public dateCreated: DateTime; @Column({}) public dateUpdated: DateTime; public file: AppFile; public user: User; @JsonProperty({ help: "Use this to create access via external user."}) public externalUser: ExternalUser; } @Table("AppFileVersions") export class AppFileVersion { @Column({ dataType: "BigInt", generated: "identity", key: true }) versionID: number; @Column({ dataType: "BigInt"}) @RelateTo(AppFile, { property: (v) => v.file, inverseProperty: (f) => f.fileVersions, foreignKeyConstraint: { onDelete: "cascade" } }) fileID: number; @Column({ dataType: "BigInt", nullable: true}) @RelateTo(User, { property: (v) => v.user, inverseProperty: (u) => u.versions, foreignKeyConstraint: { name: "FC_AppFileVersions_UserID", onDelete: "set-null" } }) userID: number; @Column({ dataType: "BigInt", nullable: true}) @RelateTo(LoginSession, { property: (v) => v.session, inverseProperty: (s) => s.versions }) sessionID: number; @Column({ dataType: "BigInt"}) @RelateTo(FileContent, { property: (v) => v.fileContent, inverseProperty: (fc) => fc.versions }) fileContentID: number; @Column({ dataType: "Char", length: 400 }) name: string; @Column({ dataType: "BigInt", nullable: true }) @RelateTo(AppFile, { property: (v) => v.folder, inverseProperty: (af) => af.folderVersions }) folderID: number; @Column({ }) dateCreated: DateTime; file: AppFile; fileContent: FileContent; folder: AppFile; user: User; session: LoginSession; }