2024-05-20 23:22:21 +00:00
|
|
|
import * as plugins from './plugins.js';
|
2024-05-27 10:56:25 +00:00
|
|
|
import * as helpers from './helpers.js';
|
|
|
|
import * as interfaces from './interfaces.js';
|
2024-05-20 23:22:21 +00:00
|
|
|
import { Directory } from './classes.directory.js';
|
|
|
|
import { MetaData } from './classes.metadata.js';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* represents a file in a directory
|
|
|
|
*/
|
|
|
|
export class File {
|
|
|
|
// STATIC
|
|
|
|
|
|
|
|
/**
|
|
|
|
* creates a file in draft mode
|
|
|
|
* you need to call .save() to store it in s3
|
|
|
|
* @param optionsArg
|
|
|
|
*/
|
|
|
|
public static async create(optionsArg: {
|
|
|
|
directory: Directory;
|
|
|
|
name: string;
|
|
|
|
contents: Buffer | string | plugins.stream.Readable;
|
|
|
|
/**
|
|
|
|
* if contents are of type string, you can specify the encoding here
|
|
|
|
*/
|
|
|
|
encoding?: 'utf8' | 'binary';
|
|
|
|
}): Promise<File> {
|
|
|
|
const contents =
|
|
|
|
typeof optionsArg.contents === 'string'
|
|
|
|
? Buffer.from(optionsArg.contents, optionsArg.encoding)
|
|
|
|
: optionsArg.contents;
|
|
|
|
const file = new File({
|
|
|
|
directoryRefArg: optionsArg.directory,
|
|
|
|
fileName: optionsArg.name,
|
|
|
|
});
|
2024-06-03 19:35:08 +00:00
|
|
|
if (contents instanceof plugins.stream.Readable) {
|
|
|
|
} else {
|
2024-05-20 23:22:21 +00:00
|
|
|
await optionsArg.directory.fastPut({
|
|
|
|
path: optionsArg.name,
|
|
|
|
contents: contents,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
// INSTANCE
|
|
|
|
public parentDirectoryRef: Directory;
|
|
|
|
public name: string;
|
|
|
|
|
|
|
|
public getBasePath(): string {
|
|
|
|
return plugins.path.join(this.parentDirectoryRef.getBasePath(), this.name);
|
2024-06-03 19:35:08 +00:00
|
|
|
}
|
2024-05-20 23:22:21 +00:00
|
|
|
|
|
|
|
constructor(optionsArg: { directoryRefArg: Directory; fileName: string }) {
|
|
|
|
this.parentDirectoryRef = optionsArg.directoryRefArg;
|
|
|
|
this.name = optionsArg.fileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async getContentsAsString(): Promise<string> {
|
|
|
|
const fileBuffer = await this.getContents();
|
|
|
|
return fileBuffer.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
public async getContents(): Promise<Buffer> {
|
|
|
|
const resultBuffer = await this.parentDirectoryRef.bucketRef.fastGet({
|
|
|
|
path: this.getBasePath(),
|
|
|
|
});
|
|
|
|
return resultBuffer;
|
|
|
|
}
|
|
|
|
|
2024-06-03 19:35:08 +00:00
|
|
|
public async getReadStream(typeArg: 'webstream'): Promise<ReadableStream>;
|
|
|
|
public async getReadStream(typeArg: 'nodestream'): Promise<plugins.stream.Readable>;
|
|
|
|
public async getReadStream(
|
|
|
|
typeArg: 'nodestream' | 'webstream'
|
|
|
|
): Promise<ReadableStream | plugins.stream.Readable> {
|
|
|
|
const readStream = this.parentDirectoryRef.bucketRef.fastGetStream(
|
|
|
|
{
|
|
|
|
path: this.getBasePath(),
|
|
|
|
},
|
|
|
|
typeArg as any
|
|
|
|
);
|
|
|
|
return readStream;
|
2024-05-20 23:22:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-06-03 19:35:08 +00:00
|
|
|
* deletes this file
|
2024-05-20 23:22:21 +00:00
|
|
|
*/
|
2024-06-03 19:35:08 +00:00
|
|
|
public async delete(optionsArg?: {
|
|
|
|
mode: 'trash' | 'permanent';
|
|
|
|
}) {
|
|
|
|
|
|
|
|
optionsArg = {
|
|
|
|
... {
|
|
|
|
mode: 'permanent',
|
|
|
|
},
|
|
|
|
...optionsArg,
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optionsArg.mode === 'permanent') {
|
2024-05-20 23:22:21 +00:00
|
|
|
await this.parentDirectoryRef.bucketRef.fastRemove({
|
2024-06-03 19:35:08 +00:00
|
|
|
path: this.getBasePath(),
|
|
|
|
});
|
|
|
|
if (!this.name.endsWith('.metadata')) {
|
|
|
|
const metadata = await this.getMetaData();
|
|
|
|
await metadata.metadataFile.delete(optionsArg);
|
|
|
|
}
|
|
|
|
} else if (optionsArg.mode === 'trash') {
|
|
|
|
const metadata = await this.getMetaData();
|
|
|
|
await metadata.storeCustomMetaData({
|
|
|
|
key: 'recycle',
|
|
|
|
value: {
|
|
|
|
deletedAt: Date.now(),
|
|
|
|
originalPath: this.getBasePath(),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const trashName = plugins.smartunique.uuid4();
|
|
|
|
await this.move({
|
|
|
|
directory: await this.parentDirectoryRef.bucketRef.getBaseDirectory(),
|
|
|
|
path: plugins.path.join('trash', trashName),
|
2024-05-20 23:22:21 +00:00
|
|
|
});
|
|
|
|
}
|
2024-06-03 19:35:08 +00:00
|
|
|
|
2024-05-20 23:22:21 +00:00
|
|
|
await this.parentDirectoryRef.listFiles();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* allows locking the file
|
|
|
|
* @param optionsArg
|
|
|
|
*/
|
|
|
|
public async lock(optionsArg?: { timeoutMillis?: number }) {
|
|
|
|
const metadata = await this.getMetaData();
|
|
|
|
await metadata.setLock({
|
|
|
|
lock: 'locked',
|
2024-05-21 16:42:55 +00:00
|
|
|
expires: Date.now() + (optionsArg?.timeoutMillis || 1000),
|
2024-05-20 23:22:21 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* actively unlocks a file
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public async unlock(optionsArg?: {
|
|
|
|
/**
|
|
|
|
* unlock the file even if not locked from this instance
|
|
|
|
*/
|
|
|
|
force?: boolean;
|
|
|
|
}) {
|
2024-05-21 16:42:55 +00:00
|
|
|
const metadata = await this.getMetaData();
|
|
|
|
await metadata.removeLock({
|
|
|
|
force: optionsArg?.force,
|
|
|
|
});
|
2024-05-20 23:22:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async updateWithContents(optionsArg: {
|
2024-06-03 19:35:08 +00:00
|
|
|
contents: Buffer | string | plugins.stream.Readable | ReadableStream;
|
2024-05-20 23:22:21 +00:00
|
|
|
encoding?: 'utf8' | 'binary';
|
|
|
|
}) {
|
2024-06-03 19:35:08 +00:00
|
|
|
if (
|
|
|
|
optionsArg.contents instanceof plugins.stream.Readable ||
|
|
|
|
optionsArg.contents instanceof ReadableStream
|
|
|
|
) {
|
2024-05-20 23:22:21 +00:00
|
|
|
await this.parentDirectoryRef.bucketRef.fastPutStream({
|
|
|
|
path: this.getBasePath(),
|
|
|
|
dataStream: optionsArg.contents,
|
|
|
|
});
|
|
|
|
} else if (Buffer.isBuffer(optionsArg.contents)) {
|
|
|
|
await this.parentDirectoryRef.bucketRef.fastPut({
|
|
|
|
path: this.getBasePath(),
|
|
|
|
contents: optionsArg.contents,
|
|
|
|
});
|
|
|
|
} else if (typeof optionsArg.contents === 'string') {
|
|
|
|
await this.parentDirectoryRef.bucketRef.fastPut({
|
|
|
|
path: this.getBasePath(),
|
|
|
|
contents: Buffer.from(optionsArg.contents, optionsArg.encoding),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-27 10:56:25 +00:00
|
|
|
/**
|
|
|
|
* moves the file to another directory
|
|
|
|
*/
|
|
|
|
public async move(pathDescriptorArg: interfaces.IPathDecriptor) {
|
|
|
|
let moveToPath = '';
|
|
|
|
const isDirectory = await this.parentDirectoryRef.bucketRef.isDirectory(pathDescriptorArg);
|
|
|
|
if (isDirectory) {
|
|
|
|
moveToPath = await helpers.reducePathDescriptorToPath({
|
|
|
|
...pathDescriptorArg,
|
|
|
|
path: plugins.path.join(pathDescriptorArg.path, this.name),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// lets move the file
|
|
|
|
await this.parentDirectoryRef.bucketRef.fastMove({
|
|
|
|
sourcePath: this.getBasePath(),
|
|
|
|
destinationPath: moveToPath,
|
|
|
|
});
|
|
|
|
|
|
|
|
// lets move the metadatafile
|
|
|
|
const metadata = await this.getMetaData();
|
|
|
|
await metadata.metadataFile.move(pathDescriptorArg);
|
|
|
|
}
|
|
|
|
|
2024-05-20 23:22:21 +00:00
|
|
|
/**
|
|
|
|
* allows updating the metadata of a file
|
|
|
|
* @param updatedMetadata
|
|
|
|
*/
|
|
|
|
public async getMetaData() {
|
2024-05-21 16:42:55 +00:00
|
|
|
if (this.name.endsWith('.metadata')) {
|
|
|
|
throw new Error('metadata files cannot have metadata');
|
|
|
|
}
|
2024-05-20 23:22:21 +00:00
|
|
|
const metadata = await MetaData.createForFile({
|
|
|
|
file: this,
|
|
|
|
});
|
|
|
|
return metadata;
|
|
|
|
}
|
2024-05-21 16:42:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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),
|
|
|
|
});
|
|
|
|
}
|
2024-05-20 23:22:21 +00:00
|
|
|
}
|