105 lines
2.5 KiB
TypeScript
105 lines
2.5 KiB
TypeScript
import * as plugins from './plugins.js';
|
|
|
|
import { File } from './classes.file.js';
|
|
|
|
export class MetaData {
|
|
// static
|
|
public static async createForFile(optionsArg: {
|
|
file: File;
|
|
}) {
|
|
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;
|
|
|
|
public async getFileType(optionsArg?: {
|
|
useFileExtension?: boolean;
|
|
useMagicBytes?: boolean;
|
|
}): Promise<string> {
|
|
if (optionsArg && optionsArg.useFileExtension || optionsArg.useFileExtension === undefined) {
|
|
return plugins.path.extname(this.fileRef.name);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* 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.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 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 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),
|
|
});
|
|
}
|
|
|
|
/**
|
|
* set a lock on the ref file
|
|
* @param optionsArg
|
|
*/
|
|
public async setLock(optionsArg: {
|
|
lock: string;
|
|
expires: Date;
|
|
}) {
|
|
|
|
}
|
|
|
|
/**
|
|
* remove the lock on the ref file
|
|
* @param optionsArg
|
|
*/
|
|
public async removeLock(optionsArg: {
|
|
force: boolean;
|
|
}) {
|
|
|
|
}
|
|
} |