smartbucket/ts/smartbucket.classes.file.ts

141 lines
3.4 KiB
TypeScript
Raw Normal View History

2022-03-30 23:45:46 +00:00
import * as plugins from './smartbucket.plugins.js';
import { Directory } from './smartbucket.classes.directory.js';
2019-10-14 18:55:07 +00:00
2019-10-16 16:12:18 +00:00
export interface IFileMetaData {
name: string;
fileType: string;
size: string;
}
2024-05-17 16:53:11 +00:00
/**
* represents a file in a directory
*/
2019-10-16 16:12:18 +00:00
export class File {
// STATIC
2024-05-17 16:53:11 +00:00
/**
* 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,
});
if (contents instanceof plugins.stream.Readable) {} else {
await optionsArg.directory.fastPut({
path: optionsArg.name,
contents: contents,
});
}
return file;
2019-10-16 16:12:18 +00:00
}
// INSTANCE
2019-10-18 13:43:06 +00:00
public parentDirectoryRef: Directory;
public name: string;
2019-10-16 16:12:18 +00:00
public path: string;
public metaData: IFileMetaData;
2024-05-17 16:53:11 +00:00
constructor(optionsArg: { directoryRefArg: Directory; fileName: string }) {
this.parentDirectoryRef = optionsArg.directoryRefArg;
this.name = optionsArg.fileName;
2019-10-16 16:12:18 +00:00
}
public async getContentAsString() {
const fileBuffer = await this.getContentAsBuffer();
return fileBuffer.toString();
}
public async getContentAsBuffer() {
const done = plugins.smartpromise.defer();
2019-10-18 13:43:06 +00:00
const fileStream = await this.parentDirectoryRef.bucketRef.smartbucketRef.minioClient
.getObject(this.parentDirectoryRef.bucketRef.name, this.path)
2020-10-12 00:37:50 +00:00
.catch((e) => console.log(e));
2022-06-07 15:15:36 +00:00
let completeFile = Buffer.from('');
2024-05-17 16:53:11 +00:00
const duplexStream = new plugins.smartstream.SmartDuplex<Buffer, Buffer>(
{
writeFunction: async (chunk) => {
completeFile = Buffer.concat([chunk]);
return chunk;
},
finalFunction: async (cb) => {
done.resolve();
return Buffer.from('');
},
2019-10-16 17:15:48 +00:00
}
);
2019-10-16 16:12:18 +00:00
if (!fileStream) {
return null;
}
fileStream.pipe(duplexStream);
await done.promise;
return completeFile;
}
2024-05-17 16:53:11 +00:00
public async readStreaming() {
2019-10-16 16:12:18 +00:00
// TODO
2021-06-02 09:14:24 +00:00
throw new Error('not yet implemented');
2019-10-16 16:12:18 +00:00
}
/**
* removes this file
*/
2019-10-16 17:15:48 +00:00
public async remove() {
2019-10-18 13:43:06 +00:00
await this.parentDirectoryRef.bucketRef.smartbucketRef.minioClient.removeObject(
this.parentDirectoryRef.bucketRef.name,
2019-10-16 17:15:48 +00:00
this.path
);
2019-10-18 13:43:06 +00:00
await this.parentDirectoryRef.listFiles();
2019-10-16 16:12:18 +00:00
}
2024-05-17 16:53:11 +00:00
/**
* deletes the file
*/
public async delete() {}
/**
* allows locking the file
* @param optionsArg
*/
public async lock(optionsArg?: { timeoutMillis?: number }) {}
/**
* actively unlocks a file
*
*/
public async unlock(optionsArg?: {
/**
* unlock the file even if not locked from this instance
*/
force?: boolean;
}) {}
public async updateWithContents(optionsArg: {
contents: Buffer | string | plugins.stream.Readable;
encoding?: 'utf8' | 'binary';
}) {}
/**
* allows updating the metadata of a file
* @param updatedMetadata
*/
public async updateMetaData(updatedMetadata: any) {}
2019-10-16 16:12:18 +00:00
}