fix(core): update

This commit is contained in:
Philipp Kunz 2024-05-21 18:42:55 +02:00
parent 5c2d92c041
commit b040120813
3 changed files with 63 additions and 47 deletions

View File

@ -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.'
}

View File

@ -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),
});
}
}

View File

@ -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<string> {
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<T = any>(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<T = any>(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<T = any>(optionsArg: {
key: string;
}): Promise<T> {
const json = await this.metadataFile.getContentsAsString();
const parsed = await JSON.parse(json);
return parsed[this.prefixCustomMetaData + optionsArg.key];
public async getCustomMetaData<T = any>(optionsArg: { key: string }): Promise<T> {
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);
}
}
public async checkLocked(): Promise<boolean> {
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 };
}
}