fix(core): update

This commit is contained in:
Philipp Kunz 2019-10-16 19:11:28 +02:00
parent 27fb5f3291
commit 5896791b14
7 changed files with 116 additions and 14 deletions

2
package-lock.json generated
View File

@ -256,7 +256,7 @@
},
"@pushrocks/smartpath": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/@pushrocks/smartpath/-/smartpath-4.0.1.tgz",
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartpath/-/smartpath-4.0.1.tgz",
"integrity": "sha512-MaI0+uLQPCr2V3WGnbdgb0pWa9xkWyrP4qYcbsHIjeismGLbn9s3jmP/HIXU8LkgzRgaVb+BJxmZJHOwl32DyA=="
},
"@pushrocks/smartpromise": {

View File

@ -20,6 +20,7 @@
},
"dependencies": {
"@pushrocks/qenv": "^4.0.6",
"@pushrocks/smartpath": "^4.0.1",
"@pushrocks/smartpromise": "^3.0.6",
"@pushrocks/streamfunction": "^1.0.24",
"@types/minio": "^7.0.3",

View File

@ -7,6 +7,7 @@ const testQenv = new Qenv('./', './.nogit/');
let testSmartbucket: smartbucket.SmartBucket;
let myBucket: smartbucket.Bucket;
let baseDirectory: smartbucket.Directory;
tap.test('should create a valid smartbucket', async () => {
testSmartbucket = new smartbucket.SmartBucket({
@ -44,5 +45,31 @@ tap.test('should delete data in bucket', async () => {
await myBucket.fastRemove('hithere/socool.txt');
});
// fs operations
tap.test('prepare for directory style tests', async () => {
await myBucket.fastStore('dir1/file1.txt', 'dir1/file1.txt content');
await myBucket.fastStore('dir1/file2.txt', 'dir1/file2.txt content');
await myBucket.fastStore('dir2/file1.txt', 'dir2/file1.txt content');
await myBucket.fastStore('dir3/file1.txt', 'dir3/file1.txt content');
await myBucket.fastStore('file1.txt', 'file1 content');
});
tap.test('should get base directory', async () => {
baseDirectory = await myBucket.getBaseDirectory();
const directories = await baseDirectory.listDirectories();
expect(directories.length).to.equal(3);
const files = await baseDirectory.listFiles();
});
tap.test('clean up directory style tests', async () => {
await myBucket.fastRemove('dir1/file1.txt');
await myBucket.fastRemove('dir1/file2.txt');
await myBucket.fastRemove('dir2/file1.txt');
await myBucket.fastRemove('dir3/file1.txt');
await myBucket.fastRemove('file1.txt');
});
tap.start();

View File

@ -1,5 +1,6 @@
import * as plugins from './smartbucket.plugins';
import { SmartBucket } from './smartbucket.classes.smartbucket';
import { Directory } from './smartbucket.classes.directory';
export class Bucket {
public static async getBucketByName(smartbucketRef: SmartBucket, bucketNameArg: string) {
@ -34,6 +35,13 @@ export class Bucket {
this.name = bucketName;
}
/**
* gets the base directory of the bucket
*/
public async getBaseDirectory() {
return new Directory(this, null, '');
}
// ===============
// Fast Operations

View File

@ -4,15 +4,48 @@ import { File } from './smartbucket.classes.file';
export class Directory {
public bucketRef: Bucket;
public basePath: string;
public parentDirectory: Directory;
public name: string;
public tree: string[];
public files: string[];
public folders: string[];
constructor(bucketRefArg: Bucket, basePathArg: string) {
constructor(bucketRefArg: Bucket, parentDiretory: Directory, name: string) {
this.bucketRef = bucketRefArg;
this.basePath = basePathArg;
this.parentDirectory = parentDiretory;
this.name = name;
}
/**
* returns an array of parent directories
*/
public getParentDirectories(): Directory[] {
let parentDirectories: Directory[] = [];
if (this.parentDirectory) {
parentDirectories.push(this.parentDirectory);
parentDirectories = parentDirectories.concat(this.parentDirectory.getParentDirectories());
}
return parentDirectories;
}
/**
* returns the directory level
*/
public getDirectoryLevel(): number {
return this.getParentDirectories().length;
};
/**
* updates the base path
*/
public getBasePath(): string {
const parentDirectories = this.getParentDirectories();
let basePath = '';
for(const parentDir of parentDirectories) {
basePath = parentDir.name + '/' + basePath;
}
return basePath;
}
/**
@ -22,7 +55,7 @@ export class Directory {
const done = plugins.smartpromise.defer();
const fileNameStream = await this.bucketRef.smartbucketRef.minioClient.listObjectsV2(
this.bucketRef.name,
this.basePath
this.getBasePath()
);
const fileArray: File[] = [];
const duplexStream = plugins.streamfunction.createDuplexStream<string, void>(async fileName => {
@ -38,22 +71,30 @@ export class Directory {
/**
* lists all folders
*/
public async listDirectories() {
public async listDirectories(): Promise<Directory[]> {
const done = plugins.smartpromise.defer();
const completeDirStream = await this.bucketRef.smartbucketRef.minioClient.listObjectsV2(
this.bucketRef.name,
this.basePath,
this.getBasePath(),
true
);
const fileArray: File[] = [];
const duplexStream = plugins.streamfunction.createDuplexStream<string, void>(async fileName => {
fileArray.push(new File(this, fileName));
const directoryArray: Directory[] = [];
const duplexStream = plugins.streamfunction.createDuplexStream<plugins.minio.BucketItem, void>(async fileName => {
console.log(fileName);
const subtractedPath = fileName.name.replace(this.getBasePath(), '');
if (subtractedPath.includes('/')) {
const dirName = fileName.name.split('/')[0];
if (directoryArray.find(directory => directory.name === dirName)) {
return;
}
directoryArray.push(new Directory(this.bucketRef, this, dirName));
}
}, async (tools) => {
done.resolve();
});
completeDirStream.pipe(duplexStream);
await done.promise;
return fileArray;
return directoryArray;
}
/**
@ -62,8 +103,32 @@ export class Directory {
public async getTreeArray() {
const treeArray = await this.bucketRef.smartbucketRef.minioClient.listObjectsV2(
this.bucketRef.name,
this.basePath,
this.getBasePath(),
true
);
}
/**
* gets a sub directory
*/
public async getSubDirectory(): Promise<Directory> {
return this;
// TODO
}
/**
* moves the directory
*/
public async move () {
// TODO
}
/**
* creates a file within this directory
* @param relativePathArg
*/
public async createFile(relativePathArg) {
let completeFilePath: string = '';
}
}

View File

@ -26,7 +26,7 @@ export class File {
fileName: string,
fileContent: Buffer
) {
const filePath = plugins.path.join(directoryRef.basePath, fileName);
const filePath = plugins.path.join(directoryRef.getBasePath(), fileName);
const streamIntake = new plugins.streamfunction.Intake();
const putPromise = directoryRef.bucketRef.smartbucketRef.minioClient
.putObject(this.name, filePath, streamIntake.getReadable())

View File

@ -5,10 +5,11 @@ export {
path
};
import * as smartpath from '@pushrocks/smartpath';
import * as smartpromise from '@pushrocks/smartpromise';
import * as streamfunction from '@pushrocks/streamfunction';
export { smartpromise, streamfunction };
export { smartpath, smartpromise, streamfunction };
// third party scope
import * as minio from 'minio';