diff --git a/test/test.ts b/test/test.ts index e068b7c..c4917e7 100644 --- a/test/test.ts +++ b/test/test.ts @@ -30,17 +30,18 @@ tap.test('should get a bucket', async () => { expect(myBucket.name).to.equal('smartbucket'); }); -tap.test('should store data in bucket', async () => { - await myBucket.store('hithere/socool.txt', 'hi there!'); +// Fast operations +tap.test('should store data in bucket fast', async () => { + await myBucket.fastStore('hithere/socool.txt', 'hi there!'); }); tap.test('should get data in bucket', async () => { - const fileString = await myBucket.get('hithere/socool.txt'); + const fileString = await myBucket.fastGet('hithere/socool.txt'); console.log(fileString); }); tap.test('should delete data in bucket', async () => { - await myBucket.remove('hithere/socool.txt'); + await myBucket.fastRemove('hithere/socool.txt'); }); diff --git a/ts/smartbucket.classes.bucket.ts b/ts/smartbucket.classes.bucket.ts index c51d6fb..7185811 100644 --- a/ts/smartbucket.classes.bucket.ts +++ b/ts/smartbucket.classes.bucket.ts @@ -34,10 +34,15 @@ export class Bucket { this.name = bucketName; } + + // =============== + // Fast Operations + // =============== + /** * store file */ - public async store(pathArg: string, fileContent: string) { + public async fastStore(pathArg: string, fileContent: string) { const streamIntake = new plugins.streamfunction.Intake(); const putPromise = this.smartbucketRef.minioClient.putObject(this.name, pathArg, streamIntake.getReadable()).catch(e => console.log(e)); streamIntake.pushData(fileContent); @@ -48,7 +53,7 @@ export class Bucket { /** * get file */ - public async get(pathArg: string) { + public async fastGet(pathArg: string) { const done = plugins.smartpromise.defer(); const fileStream = await this.smartbucketRef.minioClient.getObject(this.name, pathArg).catch(e => console.log(e)); let completeFile: string = ''; @@ -73,7 +78,7 @@ export class Bucket { /** * removeObject */ - public async remove (pathArg: string) { + public async fastRemove (pathArg: string) { await this.smartbucketRef.minioClient.removeObject(this.name, pathArg); } } diff --git a/ts/smartbucket.classes.directory.ts b/ts/smartbucket.classes.directory.ts index cca3b9a..d980d41 100644 --- a/ts/smartbucket.classes.directory.ts +++ b/ts/smartbucket.classes.directory.ts @@ -1,3 +1,69 @@ import * as plugins from './smartbucket.plugins'; +import { Bucket } from './smartbucket.classes.bucket'; +import { File } from './smartbucket.classes.file'; -export class Directory {} +export class Directory { + public bucketRef: Bucket; + public basePath: string; + + public tree: string[]; + public files: string[]; + public folders: string[]; + + constructor(bucketRefArg: Bucket, basePathArg: string) { + this.bucketRef = bucketRefArg; + this.basePath = basePathArg; + } + + /** + * lists all files + */ + public async listFiles(): Promise { + const done = plugins.smartpromise.defer(); + const fileNameStream = await this.bucketRef.smartbucketRef.minioClient.listObjectsV2( + this.bucketRef.name, + this.basePath + ); + const fileArray: File[] = []; + const duplexStream = plugins.streamfunction.createDuplexStream(async fileName => { + fileArray.push(new File(this, fileName)); + }, async (tools) => { + done.resolve(); + }); + fileNameStream.pipe(duplexStream); + await done.promise; + return fileArray; + } + + /** + * lists all folders + */ + public async listDirectories() { + const done = plugins.smartpromise.defer(); + const completeDirStream = await this.bucketRef.smartbucketRef.minioClient.listObjectsV2( + this.bucketRef.name, + this.basePath, + true + ); + const fileArray: File[] = []; + const duplexStream = plugins.streamfunction.createDuplexStream(async fileName => { + fileArray.push(new File(this, fileName)); + }, async (tools) => { + done.resolve(); + }); + completeDirStream.pipe(duplexStream); + await done.promise; + return fileArray; + } + + /** + * gets an array that has all objects with a certain prefix; + */ + public async getTreeArray() { + const treeArray = await this.bucketRef.smartbucketRef.minioClient.listObjectsV2( + this.bucketRef.name, + this.basePath, + true + ); + } +} diff --git a/ts/smartbucket.classes.file.ts b/ts/smartbucket.classes.file.ts index df7f2cd..28277cc 100644 --- a/ts/smartbucket.classes.file.ts +++ b/ts/smartbucket.classes.file.ts @@ -1,3 +1,87 @@ import * as plugins from './smartbucket.plugins'; +import { Directory } from './smartbucket.classes.directory'; -export class File {} +export interface IFileMetaData { + name: string; + fileType: string; + size: string; +} + +export class File { + // STATIC + public static async createFileFromString( + dirArg: Directory, + fileName: string, + fileContent: string + ) { + await this.createFileFromBuffer( + dirArg, + fileName, + Buffer.from(fileContent) + ); + } + + public static async createFileFromBuffer( + directoryRef: Directory, + fileName: string, + fileContent: Buffer + ) { + const filePath = plugins.path.join(directoryRef.basePath, fileName); + const streamIntake = new plugins.streamfunction.Intake(); + const putPromise = directoryRef.bucketRef.smartbucketRef.minioClient + .putObject(this.name, filePath, streamIntake.getReadable()) + .catch(e => console.log(e)); + streamIntake.pushData(fileContent); + streamIntake.signalEnd(); + await putPromise; + } + + // INSTANCE + public directoryRef: Directory; + + public path: string; + public metaData: IFileMetaData; + + constructor(directoryRefArg: Directory, fileName: string) { + this.directoryRef = directoryRefArg; + } + + public async getContentAsString() { + const fileBuffer = await this.getContentAsBuffer(); + return fileBuffer.toString(); + } + + public async getContentAsBuffer() { + const done = plugins.smartpromise.defer(); + const fileStream = await this.directoryRef.bucketRef.smartbucketRef.minioClient.getObject(this.directoryRef.bucketRef.name, this.path).catch(e => console.log(e)); + let completeFile = new Buffer(''); + const duplexStream = plugins.streamfunction.createDuplexStream(async (chunk) => { + completeFile = Buffer.concat([chunk]); + return chunk; + }, async (cb) => { + done.resolve(); + return Buffer.from(''); + }); + + if (!fileStream) { + return null; + } + + fileStream.pipe(duplexStream); + await done.promise; + return completeFile; + } + + public async streamContent() { + throw new Error('not yet implemented'); + // TODO + } + + /** + * removes this file + */ + public async remove () { + await this.directoryRef.bucketRef.smartbucketRef.minioClient.removeObject(this.directoryRef.bucketRef.name, this.path); + await this.directoryRef.listFiles(); + } +} diff --git a/ts/smartbucket.plugins.ts b/ts/smartbucket.plugins.ts index 2982db9..19aa9a4 100644 --- a/ts/smartbucket.plugins.ts +++ b/ts/smartbucket.plugins.ts @@ -1,3 +1,10 @@ +// node native +import * as path from 'path'; + +export { + path +}; + import * as smartpromise from '@pushrocks/smartpromise'; import * as streamfunction from '@pushrocks/streamfunction';