/* eslint-disable no-console */ import Inject, { RegisterTransient } from "@entity-access/entity-access/dist/di/di.js"; import SocialMailContext from "../../model/SocialMailContext.js"; import DateTime from "@entity-access/entity-access/dist/types/DateTime.js"; import { SessionUser } from "@entity-access/server-pages/dist/core/SessionUser.js"; import AppFile from "../../model/entities/AppFile.js"; import BaseStorage from "../../storage/BaseStorage.js"; import TempFileService from "../../storage/TempFileService.js"; import {ICacheContainer, ObjectCache } from "../../../common/cacheFor.js"; import { globalEnv } from "../../../common/globalEnv.js"; import Sql from "@entity-access/entity-access/dist/sql/Sql.js"; import EntityAccessError from "@entity-access/entity-access/dist/common/EntityAccessError.js"; import MessagingService from "../message-events/MessagingService.js"; import WebSiteContentService from "./WebSiteContentService.js"; import WorkflowContext from "@entity-access/entity-access/dist/workflows/WorkflowContext.js"; import GenerateTemplatePreviewWorkflow from "../../workflows/site/generate-previews/GenerateTemplatePreviewWorkflow.js"; let cdn = globalEnv.cdn || ""; if (cdn) { cdn = `//${cdn}`; } @RegisterTransient export default class SnapshotService implements Disposable, ICacheContainer { cache: ObjectCache; @Inject db: SocialMailContext; @Inject wcs: WebSiteContentService; @Inject wc: WorkflowContext; @Inject storage: BaseStorage; @Inject tempFileService: TempFileService; @Inject ms: MessagingService; root: AppFile; snapshotRoot: AppFile; allFiles: AppFile[] = null; snapshotSize: number = 0; [Symbol.dispose]() { this.cache?.[Symbol.dispose](); } async createSnapshot(folderID, sessionUser = null as SessionUser, verify = true) { const { db } = this; db.verifyFilters = false; // verify access to publish let q = this.db.appFiles .where({ folderID }, (x, p) => x.appFileID === p.folderID); if (verify) { if (!sessionUser.isGlobalPublisher) { q = q.where(sessionUser, (x, p) => Sql.text.likeAny(x.path, p.writeFileAccess)); } } const folder = await q .first(); if (!folder) { throw new EntityAccessError(`Access denied to create new snapshot`); } await using tx = await db.connection.createTransaction(); const sourcePath = folder.path + "%"; this.allFiles = await db.appFiles.where({ sourcePath, folderID }, (x, p) => Sql.text.like(x.path, p.sourcePath) && x.isDeleted === false && x.appFileID !== p.folderID ) .toArray(); if (!this.allFiles.length) { throw new EntityAccessError(`File not found under ${sourcePath}/`); } for (const file of this.allFiles) { if (file.isFolder) { continue; } this.snapshotSize += Number(file.fileSize); } const dateCreated = DateTime.now; const dateUpdated = dateCreated; let creatorID = void 0; creatorID = sessionUser?.userID; const snapshotRoot = await db.appFiles.statements.insert({ ... folder, contentType: "snapshot", snapshotParentID: folderID, appFileID: void 0, parentID: null, creatorID, path: null, dateCreated, dateUpdated }); const path = `${snapshotRoot.appFileID}/`; snapshotRoot.path = path; // this.snapshotFiles.push(snapshotRoot); await db.appFiles.statements.update({ path }, { appFileID: snapshotRoot.appFileID }); this.root = folder; this.snapshotRoot = snapshotRoot; await this.copyChildren(db, folder, snapshotRoot); await db.saveChanges(); await this.db.appFiles.where(this.snapshotRoot, (x, p) => x.appFileID === p.appFileID) .update({ size: this.snapshotSize}, (x, p) => ({ fileSize: Sql.cast.asBigInt(p.size) })); await tx.commit(); // generate preview.. await this.db.websiteFolders.withKeys({ folderID}).update({ previewGenerated: false, cacheKey: `${snapshotRoot.appFileID}-${Date.now().toString(36)}` }); this.ms.snapshotRefresh.notify({ folderID }); await GenerateTemplatePreviewWorkflow.queue(this.wc, folderID); const ws = await this.wcs.getCurrentSnapshot(folderID, snapshotRoot.appFileID); // in case of design, we need to fix template.html to reference // cdn image to reduce load time if (ws?.isDesign) { const templateFile = await this.db.appFiles.where(ws, (x, p) => x.parentID === p.snapshotFolderID && x.name === "template.html") .first(); if (templateFile) { const templateFileContent = await this.wcs.getSnapshotFile(ws, ["template.html"]); // we need to save the file content.. await this.storage.updateFile(templateFile, templateFileContent); await db.appFiles.statements.update({ fileContentID: templateFile.fileContentID, dateUpdated: dateCreated }, { appFileID: templateFile.appFileID }); } } return snapshotRoot; } async copyChildren(db: SocialMailContext, source: AppFile, dest: AppFile, siteRoot = "") { const { dateCreated, dateUpdated } = dest; if (!source.children) { return; } for (const srcChild of source.children) { // console.log(`Adding ${srcChild.path} - ${srcChild.name} in ${dest.path} - ${dest.name}`); const destChild = await db.appFiles.statements.insert({ ... srcChild, contentType: srcChild.isFolder ? "snapshot" : srcChild.contentType, appFileID: void 0, parentID: dest.appFileID, path: null, dateCreated, dateUpdated }); const path = `${dest.path}${destChild.appFileID}/`; destChild.path = path; await db.appFiles.statements.update({ path }, { appFileID: destChild.appFileID }); if (srcChild.isFolder) { await this.copyChildren(db, srcChild, destChild, siteRoot + "/" + srcChild.name ); } } } }