smartbucket/ts/classes.metadata.ts

115 lines
3.3 KiB
TypeScript
Raw 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({
path: metaData.fileRef.name + '.metadata',
2024-05-20 23:22:21 +00:00
createWithContents: '{}',
});
return metaData;
}
// instance
/**
* the file that contains the metadata
*/
metadataFile!: File;
2024-05-20 23:22:21 +00:00
/**
* 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<plugins.smartmime.IFileTypeResult | undefined> {
if ((optionsArg && optionsArg.useFileExtension) || !optionsArg) {
const fileType = await plugins.smartmime.detectMimeType({
path: this.fileRef.name,
});
return fileType;
}
if (optionsArg && optionsArg.useMagicBytes) {
const fileType = await plugins.smartmime.detectMimeType({
buffer: await this.fileRef.getMagicBytes({
length: 100,
})
});
return fileType;
2024-05-20 23:22:21 +00:00
}
throw new Error('optionsArg.useFileExtension and optionsArg.useMagicBytes cannot both be false');
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(),
});
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.getJsonData();
2024-05-21 16:42:55 +00:00
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
}