smartbucket/ts/classes.metadata.ts

101 lines
2.9 KiB
TypeScript
Raw Permalink Normal View History

2024-05-20 23:22:21 +00:00
import * as plugins from './plugins.js';
import { File } from './classes.file.js';
export class MetaData {
// static
2024-05-21 16:42:55 +00:00
public static async createForFile(optionsArg: { file: File }) {
2024-05-20 23:22:21 +00:00
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;
2024-05-21 16:42:55 +00:00
2024-05-20 23:22:21 +00:00
public async getFileType(optionsArg?: {
useFileExtension?: boolean;
useMagicBytes?: boolean;
}): Promise<string> {
2024-05-21 16:42:55 +00:00
if ((optionsArg && optionsArg.useFileExtension) || optionsArg.useFileExtension === undefined) {
2024-05-20 23:22:21 +00:00
return plugins.path.extname(this.fileRef.name);
}
2024-05-21 16:42:55 +00:00
}
2024-05-20 23:22:21 +00:00
/**
* gets the size of the fileRef
*/
public async getSizeInBytes(): Promise<number> {
const stat = await this.fileRef.parentDirectoryRef.bucketRef.fastStat({
path: this.fileRef.getBasePath(),
});
2024-06-17 14:01:35 +00:00
return stat.ContentLength;
2024-05-21 16:42:55 +00:00
}
2024-05-20 23:22:21 +00:00
private prefixCustomMetaData = 'custom_';
2024-05-21 16:42:55 +00:00
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);
2024-05-20 23:22:21 +00:00
}
2024-05-21 16:42:55 +00:00
public async getCustomMetaData<T = any>(optionsArg: { key: string }): Promise<T> {
const data = await this.metadataFile.getJsonData();
return data[this.prefixCustomMetaData + optionsArg.key];
2024-05-20 23:22:21 +00:00
}
2024-05-21 16:42:55 +00:00
public async deleteCustomMetaData(optionsArg: { key: string }) {
const data = await this.metadataFile.getJsonData();
delete data[this.prefixCustomMetaData + optionsArg.key];
await this.metadataFile.writeJsonData(data);
2024-05-20 23:22:21 +00:00
}
/**
* set a lock on the ref file
* @param optionsArg
*/
2024-05-21 16:42:55 +00:00
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);
2024-05-20 23:22:21 +00:00
}
/**
* remove the lock on the ref file
2024-05-21 16:42:55 +00:00
* @param optionsArg
2024-05-20 23:22:21 +00:00
*/
2024-05-21 16:42:55 +00:00
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();
}
2024-05-20 23:22:21 +00:00
2024-05-21 16:42:55 +00:00
public async getLockInfo(): Promise<{ lock: string; expires: number }> {
const data = await this.metadataFile.getJsonData();
return { lock: data.lock, expires: data.lockExpires };
2024-05-20 23:22:21 +00:00
}
2024-05-21 16:42:55 +00:00
}