import * as plugins from './plugins.js'; import { File } from './classes.file.js'; export class MetaData { // static public static async createForFile(optionsArg: { file: File }) { const metaData = new MetaData(); metaData.fileRef = optionsArg.file; // lets find the existing metadata file metaData.metadataFile = await metaData.fileRef.parentDirectoryRef.getFile({ name: metaData.fileRef.name + '.metadata', createWithContents: '{}', }); return metaData; } // instance /** * the file that contains the metadata */ metadataFile: File; /** * the file that the metadata is for */ fileRef: File; public async getFileType(optionsArg?: { useFileExtension?: boolean; useMagicBytes?: boolean; }): Promise { if ((optionsArg && optionsArg.useFileExtension) || optionsArg.useFileExtension === undefined) { return plugins.path.extname(this.fileRef.name); } } /** * gets the size of the fileRef */ public async getSizeInBytes(): Promise { const stat = await this.fileRef.parentDirectoryRef.bucketRef.fastStat({ path: this.fileRef.getBasePath(), }); return stat.ContentLength; } private prefixCustomMetaData = 'custom_'; public async storeCustomMetaData(optionsArg: { key: string; value: T }) { const data = await this.metadataFile.getContentsAsString(); data[this.prefixCustomMetaData + optionsArg.key] = optionsArg.value; await this.metadataFile.writeJsonData(data); } public async getCustomMetaData(optionsArg: { key: string }): Promise { const data = await this.metadataFile.getJsonData(); return data[this.prefixCustomMetaData + optionsArg.key]; } public async deleteCustomMetaData(optionsArg: { key: string }) { const data = await this.metadataFile.getJsonData(); delete data[this.prefixCustomMetaData + optionsArg.key]; await this.metadataFile.writeJsonData(data); } /** * set a lock on the ref file * @param optionsArg */ public async setLock(optionsArg: { lock: string; expires: number }) { const data = await this.metadataFile.getJsonData(); data.lock = optionsArg.lock; data.lockExpires = optionsArg.expires; await this.metadataFile.writeJsonData(data); } /** * remove the lock on the ref file * @param optionsArg */ public async removeLock(optionsArg: { force: boolean }) { const data = await this.metadataFile.getJsonData(); delete data.lock; delete data.lockExpires; await this.metadataFile.writeJsonData(data); } public async checkLocked(): Promise { const data = await this.metadataFile.getJsonData(); return data.lock && data.lockExpires > Date.now(); } public async getLockInfo(): Promise<{ lock: string; expires: number }> { const data = await this.metadataFile.getJsonData(); return { lock: data.lock, expires: data.lockExpires }; } }