fix(core): update
This commit is contained in:
parent
5c2d92c041
commit
b040120813
@ -3,6 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@push.rocks/smartbucket',
|
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.'
|
description: 'A TypeScript library that offers simple, cloud-independent object storage with features like bucket creation, file management, and directory management.'
|
||||||
}
|
}
|
||||||
|
@ -102,7 +102,7 @@ export class File {
|
|||||||
const metadata = await this.getMetaData();
|
const metadata = await this.getMetaData();
|
||||||
await metadata.setLock({
|
await metadata.setLock({
|
||||||
lock: 'locked',
|
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;
|
force?: boolean;
|
||||||
}) {
|
}) {
|
||||||
|
const metadata = await this.getMetaData();
|
||||||
|
await metadata.removeLock({
|
||||||
|
force: optionsArg?.force,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async updateWithContents(optionsArg: {
|
public async updateWithContents(optionsArg: {
|
||||||
@ -146,9 +149,27 @@ export class File {
|
|||||||
* @param updatedMetadata
|
* @param updatedMetadata
|
||||||
*/
|
*/
|
||||||
public async getMetaData() {
|
public async getMetaData() {
|
||||||
|
if (this.name.endsWith('.metadata')) {
|
||||||
|
throw new Error('metadata files cannot have metadata');
|
||||||
|
}
|
||||||
const metadata = await MetaData.createForFile({
|
const metadata = await MetaData.createForFile({
|
||||||
file: this,
|
file: this,
|
||||||
});
|
});
|
||||||
return metadata;
|
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),
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,9 +4,7 @@ import { File } from './classes.file.js';
|
|||||||
|
|
||||||
export class MetaData {
|
export class MetaData {
|
||||||
// static
|
// static
|
||||||
public static async createForFile(optionsArg: {
|
public static async createForFile(optionsArg: { file: File }) {
|
||||||
file: File;
|
|
||||||
}) {
|
|
||||||
const metaData = new MetaData();
|
const metaData = new MetaData();
|
||||||
metaData.fileRef = optionsArg.file;
|
metaData.fileRef = optionsArg.file;
|
||||||
|
|
||||||
@ -34,10 +32,10 @@ export class MetaData {
|
|||||||
useFileExtension?: boolean;
|
useFileExtension?: boolean;
|
||||||
useMagicBytes?: boolean;
|
useMagicBytes?: boolean;
|
||||||
}): Promise<string> {
|
}): Promise<string> {
|
||||||
if (optionsArg && optionsArg.useFileExtension || optionsArg.useFileExtension === undefined) {
|
if ((optionsArg && optionsArg.useFileExtension) || optionsArg.useFileExtension === undefined) {
|
||||||
return plugins.path.extname(this.fileRef.name);
|
return plugins.path.extname(this.fileRef.name);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* gets the size of the fileRef
|
* gets the size of the fileRef
|
||||||
@ -47,59 +45,56 @@ export class MetaData {
|
|||||||
path: this.fileRef.getBasePath(),
|
path: this.fileRef.getBasePath(),
|
||||||
});
|
});
|
||||||
return stat.size;
|
return stat.size;
|
||||||
};
|
}
|
||||||
|
|
||||||
private prefixCustomMetaData = 'custom_';
|
private prefixCustomMetaData = 'custom_';
|
||||||
|
|
||||||
public async storeCustomMetaData<T = any>(optionsArg: {
|
public async storeCustomMetaData<T = any>(optionsArg: { key: string; value: T }) {
|
||||||
key: string;
|
const data = await this.metadataFile.getContentsAsString();
|
||||||
value: T;
|
data[this.prefixCustomMetaData + optionsArg.key] = optionsArg.value;
|
||||||
}) {
|
await this.metadataFile.writeJsonData(data);
|
||||||
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 getCustomMetaData<T = any>(optionsArg: {
|
public async getCustomMetaData<T = any>(optionsArg: { key: string }): Promise<T> {
|
||||||
key: string;
|
const data = await this.metadataFile.getJsonData();
|
||||||
}): Promise<T> {
|
return data[this.prefixCustomMetaData + optionsArg.key];
|
||||||
const json = await this.metadataFile.getContentsAsString();
|
|
||||||
const parsed = await JSON.parse(json);
|
|
||||||
return parsed[this.prefixCustomMetaData + optionsArg.key];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async deleteCustomMetaData(optionsArg: {
|
public async deleteCustomMetaData(optionsArg: { key: string }) {
|
||||||
key: string;
|
const data = await this.metadataFile.getJsonData();
|
||||||
}) {
|
delete data[this.prefixCustomMetaData + optionsArg.key];
|
||||||
const json = await this.metadataFile.getContentsAsString();
|
await this.metadataFile.writeJsonData(data);
|
||||||
const parsed = await JSON.parse(json);
|
|
||||||
delete parsed[this.prefixCustomMetaData + optionsArg.key];
|
|
||||||
await this.metadataFile.updateWithContents({
|
|
||||||
contents: JSON.stringify(parsed),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* set a lock on the ref file
|
* set a lock on the ref file
|
||||||
* @param optionsArg
|
* @param optionsArg
|
||||||
*/
|
*/
|
||||||
public async setLock(optionsArg: {
|
public async setLock(optionsArg: { lock: string; expires: number }) {
|
||||||
lock: string;
|
const data = await this.metadataFile.getJsonData();
|
||||||
expires: Date;
|
data.lock = optionsArg.lock;
|
||||||
}) {
|
data.lockExpires = optionsArg.expires;
|
||||||
|
await this.metadataFile.writeJsonData(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* remove the lock on the ref file
|
* remove the lock on the ref file
|
||||||
* @param optionsArg
|
* @param optionsArg
|
||||||
*/
|
*/
|
||||||
public async removeLock(optionsArg: {
|
public async removeLock(optionsArg: { force: boolean }) {
|
||||||
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 };
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user