diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index e00bec9..8f89296 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@push.rocks/smartbucket', - version: '3.0.4', + version: '3.0.5', description: 'A TypeScript library that offers simple, cloud-independent object storage with features like bucket creation, file management, and directory management.' } diff --git a/ts/classes.file.ts b/ts/classes.file.ts index e3ba742..6d6edba 100644 --- a/ts/classes.file.ts +++ b/ts/classes.file.ts @@ -102,7 +102,7 @@ export class File { const metadata = await this.getMetaData(); await metadata.setLock({ lock: 'locked', - expires: new Date(Date.now() + (optionsArg?.timeoutMillis || 1000)), + expires: Date.now() + (optionsArg?.timeoutMillis || 1000), }); } @@ -116,7 +116,10 @@ export class File { */ force?: boolean; }) { - + const metadata = await this.getMetaData(); + await metadata.removeLock({ + force: optionsArg?.force, + }); } public async updateWithContents(optionsArg: { @@ -146,9 +149,27 @@ export class File { * @param updatedMetadata */ public async getMetaData() { + if (this.name.endsWith('.metadata')) { + throw new Error('metadata files cannot have metadata'); + } const metadata = await MetaData.createForFile({ file: this, }); return metadata; } + + /** + * gets the contents as json + */ + public async getJsonData() { + const json = await this.getContentsAsString(); + const parsed = await JSON.parse(json); + return parsed; + } + + public async writeJsonData(dataArg: any) { + await this.updateWithContents({ + contents: JSON.stringify(dataArg), + }); + } } diff --git a/ts/classes.metadata.ts b/ts/classes.metadata.ts index c9c4fc6..e520eea 100644 --- a/ts/classes.metadata.ts +++ b/ts/classes.metadata.ts @@ -4,9 +4,7 @@ import { File } from './classes.file.js'; export class MetaData { // static - public static async createForFile(optionsArg: { - file: File; - }) { + public static async createForFile(optionsArg: { file: File }) { const metaData = new MetaData(); metaData.fileRef = optionsArg.file; @@ -29,16 +27,16 @@ export class MetaData { * 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) { + if ((optionsArg && optionsArg.useFileExtension) || optionsArg.useFileExtension === undefined) { return plugins.path.extname(this.fileRef.name); } - }; - + } + /** * gets the size of the fileRef */ @@ -47,59 +45,56 @@ export class MetaData { path: this.fileRef.getBasePath(), }); return stat.size; - }; + } private prefixCustomMetaData = 'custom_'; - public async storeCustomMetaData(optionsArg: { - key: string; - value: T; - }) { - const json = await this.metadataFile.getContentsAsString(); - const parsed = await JSON.parse(json); - parsed[this.prefixCustomMetaData + optionsArg.key] = optionsArg.value; - await this.metadataFile.updateWithContents({ - contents: JSON.stringify(parsed), - }); + 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 json = await this.metadataFile.getContentsAsString(); - const parsed = await JSON.parse(json); - return parsed[this.prefixCustomMetaData + optionsArg.key]; + 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 json = await this.metadataFile.getContentsAsString(); - const parsed = await JSON.parse(json); - delete parsed[this.prefixCustomMetaData + optionsArg.key]; - await this.metadataFile.updateWithContents({ - contents: JSON.stringify(parsed), - }); + 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: Date; - }) { - + 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 + * @param optionsArg */ - public async removeLock(optionsArg: { - force: boolean; - }) { - + public async removeLock(optionsArg: { force: boolean }) { + const data = await this.metadataFile.getJsonData(); + delete data.lock; + delete data.lockExpires; + await this.metadataFile.writeJsonData(data); } -} \ No newline at end of file + + 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 }; + } +}