fix(core): update

This commit is contained in:
Philipp Kunz 2019-10-16 18:12:18 +02:00
parent fbac742db2
commit 34d7ef2686
5 changed files with 172 additions and 9 deletions

View File

@ -30,17 +30,18 @@ tap.test('should get a bucket', async () => {
expect(myBucket.name).to.equal('smartbucket'); expect(myBucket.name).to.equal('smartbucket');
}); });
tap.test('should store data in bucket', async () => { // Fast operations
await myBucket.store('hithere/socool.txt', 'hi there!'); 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 () => { 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); console.log(fileString);
}); });
tap.test('should delete data in bucket', async () => { tap.test('should delete data in bucket', async () => {
await myBucket.remove('hithere/socool.txt'); await myBucket.fastRemove('hithere/socool.txt');
}); });

View File

@ -34,10 +34,15 @@ export class Bucket {
this.name = bucketName; this.name = bucketName;
} }
// ===============
// Fast Operations
// ===============
/** /**
* store file * store file
*/ */
public async store(pathArg: string, fileContent: string) { public async fastStore(pathArg: string, fileContent: string) {
const streamIntake = new plugins.streamfunction.Intake(); const streamIntake = new plugins.streamfunction.Intake();
const putPromise = this.smartbucketRef.minioClient.putObject(this.name, pathArg, streamIntake.getReadable()).catch(e => console.log(e)); const putPromise = this.smartbucketRef.minioClient.putObject(this.name, pathArg, streamIntake.getReadable()).catch(e => console.log(e));
streamIntake.pushData(fileContent); streamIntake.pushData(fileContent);
@ -48,7 +53,7 @@ export class Bucket {
/** /**
* get file * get file
*/ */
public async get(pathArg: string) { public async fastGet(pathArg: string) {
const done = plugins.smartpromise.defer(); const done = plugins.smartpromise.defer();
const fileStream = await this.smartbucketRef.minioClient.getObject(this.name, pathArg).catch(e => console.log(e)); const fileStream = await this.smartbucketRef.minioClient.getObject(this.name, pathArg).catch(e => console.log(e));
let completeFile: string = ''; let completeFile: string = '';
@ -73,7 +78,7 @@ export class Bucket {
/** /**
* removeObject * removeObject
*/ */
public async remove (pathArg: string) { public async fastRemove (pathArg: string) {
await this.smartbucketRef.minioClient.removeObject(this.name, pathArg); await this.smartbucketRef.minioClient.removeObject(this.name, pathArg);
} }
} }

View File

@ -1,3 +1,69 @@
import * as plugins from './smartbucket.plugins'; 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<File[]> {
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<string, void>(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<string, void>(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
);
}
}

View File

@ -1,3 +1,87 @@
import * as plugins from './smartbucket.plugins'; 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<Buffer, Buffer>(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();
}
}

View File

@ -1,3 +1,10 @@
// node native
import * as path from 'path';
export {
path
};
import * as smartpromise from '@pushrocks/smartpromise'; import * as smartpromise from '@pushrocks/smartpromise';
import * as streamfunction from '@pushrocks/streamfunction'; import * as streamfunction from '@pushrocks/streamfunction';