UNPKG

@foxpage/foxpage-manager

Version:

foxpage resource manager

147 lines (146 loc) 4.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FileManagerImpl = void 0; const common_1 = require("../common"); const data_service_1 = require("../data-service"); class FileManagerImpl extends common_1.ManagerBaseImpl { constructor(app) { super(app, { type: 'file', diskCache: { enable: true } }); /** * pathname and file id map * * @private */ this.pathnameMap = new Map(); /** * fileId and pathname map * * @private */ this.fileMap = new Map(); } /** * add a file * * @param {FPFile} file file content */ addFile(file) { const pathname = this.getPathname(file); if (pathname) { this.removePathname(pathname, file.id); this.addPathname(pathname, file.id); } this.addOne(file.id, file, file); return file; } /** * remove files * * @param {string[]} fileIds file ids */ async removeFiles(fileIds) { for (const item of fileIds) { const pathname = await this.getPathnameByFileId(item); this.removePathname(pathname, item); } this.remove(fileIds); } /** * get file via pathname * * @param {string} pathname * @return {Promise<FPFile | null>} */ async getFileByPathname(pathname) { const fileId = this.pathnameMap.get(pathname); if (!fileId) { return null; } const files = await this.find([fileId]); if (!files[0]) { this.removePathname(pathname, fileId); return null; } return files[0]; } /** * get file by fileId * @param fileId file id * @returns file */ async getFileById(fileId, opt = { autoFetch: true, autoCache: true }) { const files = await this.find([fileId], { autoFetch: opt.autoFetch, autoCache: opt.autoCache }); return files[0] || null; } /** * get pathname via file id * * @param {string} fileId * @return {Promise<string>} */ async getPathnameByFileId(fileId) { const files = await this.find([fileId]); if (!files[0]) { return ''; } return this.getPathname(files[0]); } async onPull(data) { // updates & removes is file ids const { updates, removes } = data.file || {}; if (updates === null || updates === void 0 ? void 0 : updates.length) { const fileIds = await this.filterExists(updates); this.markNeedUpdates(fileIds); const files = await data_service_1.foxpageDataService.fetchFiles(this.appId, { fileIds }); files.forEach(file => { this.addFile(file); }); } if (removes === null || removes === void 0 ? void 0 : removes.length) { await this.removeFiles(removes); } } onStash(data) { var _a; (_a = data.files) === null || _a === void 0 ? void 0 : _a.map(item => { this.addFile(item); }); } async onFetch(fileIds, opt) { const files = await data_service_1.foxpageDataService.fetchFiles(this.appId, { fileIds }); files.forEach(file => { if (opt === null || opt === void 0 ? void 0 : opt.autoCache) { this.addFile(file); } }); return files; } async createInstance(data) { return data; } getPathname(file) { const tags = file.tags; const pathnameTag = tags.find(tag => !!tag.pathname); return (pathnameTag === null || pathnameTag === void 0 ? void 0 : pathnameTag.pathname) || ''; } addPathname(pathname, fileId) { this.pathnameMap.set(pathname, fileId); this.fileMap.set(fileId, pathname); } removePathname(pathname, fileId) { const cachedPathname = this.fileMap.get(fileId); if (cachedPathname) { this.pathnameMap.delete(cachedPathname); } const cachedFileId = this.pathnameMap.get(pathname); if (cachedFileId) { this.fileMap.delete(cachedFileId); } } destroy() { super.destroy(); this.pathnameMap.clear(); this.fileMap.clear(); } } exports.FileManagerImpl = FileManagerImpl;