import { Servicer } from '../servicer/servicer'; import { MessageType } from '../core/message-type'; import { Options } from '../options'; import { ZipOptions } from '../interfaces/zip-options'; import JSZip, { file } from 'jszip'; const commands = MessageType.file; export class FileSystem { constructor( private servicer: Servicer, private options: Options ) { } /** * 连接路径 * @param paths - 路径列表 */ public joinPath(paths: Array) { return paths.reduce((path, subPath) => { const hasLastSlash = path.length > 0 && path[path.length - 1] === '/'; const hasFirstSlash = subPath.length > 0 && subPath[0] === '/'; if(hasLastSlash && hasFirstSlash) { return `${path}${subPath.substring(1)}`; } else if(!hasLastSlash && !hasFirstSlash) { return `${path}/${subPath}`; } else { return `${path}${subPath}`; } }, ''); } /** * 写入文件 * @param path - 文件路径 * @param content - 文件内容 * @param options - 配置参数 */ async writeFile(path: string, content: string | Uint8Array, options: WriteFileOptions = { flags: 'w' }) { await this.servicer.do(commands.writeFile, [path, content, options]); } /** * 读取文件内容 * @param path - 文件路径 * @param options - 配置参数 */ async readFile(path: string, options: ReadFileOptions = { encoding: 'utf8', flags: 'r' }): Promise { return this.servicer.do(commands.readFile, [path, options]); } /** * 创建目录 * @param dir - 目录地址 */ async mkdir(dir: string) { await this.servicer.do(commands.mkdir, [dir]); } /** * 移除文件 * @param path - 文件地址 */ async removeFile(path: string) { await this.servicer.do(commands.remove, [path]); } /** * 加密文件 * @param src - 要加密的文件路径 * @param dst - 加密后的文件存储路径 */ async encryptFile(src: string, dst: string) { await this.servicer.do(commands.encryptFile, [src, dst]); } /** * 加密文本 * @param content - 要加密的文本 */ encryptContent(content: string): Promise> { return this.servicer.do(commands.encryptContent, [content]); } /** * 加密内容 * @param buffer - 内容数据 */ encryptBuffer(buffer: ArrayLike): Promise> { const chars = []; for(let i = 0; i < buffer.length; i++) { chars.push(String.fromCharCode(buffer[i])); } return this.encryptContent(chars.join('')); } /** * 解密内容 * @param buffer - 内容数据 */ decryptBuffer(buffer: ArrayLike): Promise { return this.servicer.do(commands.decryptBuffer, [buffer]); } /** * 解密文本 * @param content - 文本 */ decryptContent(content: string): Promise { const buffer = []; for(let i = 0; i < content.length; i++) { buffer.push(content.charCodeAt(i)); } return this.decryptBuffer(buffer); } /** * 解压 zip 包,将内部的 shader 写入文件系统,返回 zip 中的配置 * @param zipData - zip 包数据 * @param options - 解压配置 * @param isEncrypted - zip 包内容是否被加密 * @returns 滤镜配置字符串 */ async loadZip(zipData: any, options?: ZipOptions, isEncrypted: boolean = true): Promise<{ config: string, files: JSZip.JSZipObject[]}> { const zip = await JSZip.loadAsync(zipData, options); const filenames = Object.getOwnPropertyNames(zip.files); const files: Array = [] let configFile: JSZip.JSZipObject | null = null; filenames.forEach(filename => { // 移除 __MACOS 文件 if(filename.includes('__MACOSX')) { return; } const file = zip.files[filename]; if(!filename.includes('config.json') && !file.dir) { files.push(file); } else { configFile = file; } }); // 处理配置文件 if(!configFile) { throw new Error('config file is not exist'); } let config = await configFile!.async(isEncrypted ? 'array' : 'string'); if(isEncrypted) { config = await this.decryptBuffer(config as number[]); } return { config: config as string, files }; } /** * 将 shader 写入文件系统 * @param shader - shader 内容 * @param name - shader 名称 * @param version - 版本号 */ async writeShader(shader: string | Uint8Array, name: string, version?: string) { const { workResource } = this.options; const filename = version ? `${version}_${name}` : name; const path = this.joinPath([workResource!, filename]); this.writeFile(path, shader); } }