UNPKG

@mieweb/wikigdrive

Version:

Google Drive to MarkDown synchronization

70 lines (69 loc) 2.34 kB
/* eslint-disable @typescript-eslint/no-explicit-any */ import { Buffer } from 'node:buffer'; import { getSignature } from 'file-isignature'; import { FileService, pathResolve } from './FileService.js'; export class FileContentService extends FileService { constructor(rootPath = '/', virtualPath = '/') { super(rootPath); Object.defineProperty(this, "rootPath", { enumerable: true, configurable: true, writable: true, value: rootPath }); Object.defineProperty(this, "virtualPath", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.virtualPath = virtualPath || '/'; if (!this.virtualPath.endsWith('/')) { this.virtualPath += '/'; } } async readFile(filePath) { const buffer = await this.readBuffer(filePath); return buffer.toString('utf-8'); } async writeFile(filePath, content) { return this.writeBuffer(filePath, Buffer.from(content)); } async readJson(filePath) { try { const content = await this.readFile(filePath); return JSON.parse(content); // eslint-disable-next-line @typescript-eslint/no-unused-vars } catch (error) { return null; } } async guessExtension(filePath) { const signature = getSignature(pathResolve(this.rootPath, filePath)); return signature.value || 'bin'; } async writeJson(filePath, data) { if (!data) { return; } await this.writeFile(filePath, JSON.stringify(data, null, 2)); } async getSubFileService(subPath, virtualPath) { if (!subPath) { throw new Error('Empty subPath'); } if (subPath.startsWith('/')) { // this.virtualPath always ends with '/' subPath = subPath.substring(1); } const subFileService = new FileContentService(pathResolve(this.rootPath, subPath), virtualPath !== undefined ? virtualPath : this.virtualPath + subPath + '/'); await subFileService.mkdir('/'); return subFileService; } getVirtualPath() { return this.virtualPath; } getRealPath() { return this.rootPath; } }