From 5896791b1455cabeeb836690208fcc2506f1e7fd Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Wed, 16 Oct 2019 19:11:28 +0200 Subject: [PATCH] fix(core): update --- package-lock.json | 2 +- package.json | 1 + test/test.ts | 27 +++++++++ ts/smartbucket.classes.bucket.ts | 8 +++ ts/smartbucket.classes.directory.ts | 87 +++++++++++++++++++++++++---- ts/smartbucket.classes.file.ts | 2 +- ts/smartbucket.plugins.ts | 3 +- 7 files changed, 116 insertions(+), 14 deletions(-) diff --git a/package-lock.json b/package-lock.json index 392871d..8b5e3e3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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": { diff --git a/package.json b/package.json index c12b774..1423603 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/test.ts b/test/test.ts index c4917e7..a000117 100644 --- a/test/test.ts +++ b/test/test.ts @@ -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(); diff --git a/ts/smartbucket.classes.bucket.ts b/ts/smartbucket.classes.bucket.ts index 7185811..c5d74d0 100644 --- a/ts/smartbucket.classes.bucket.ts +++ b/ts/smartbucket.classes.bucket.ts @@ -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 diff --git a/ts/smartbucket.classes.directory.ts b/ts/smartbucket.classes.directory.ts index d980d41..9485b4a 100644 --- a/ts/smartbucket.classes.directory.ts +++ b/ts/smartbucket.classes.directory.ts @@ -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(async fileName => { @@ -38,22 +71,30 @@ export class Directory { /** * lists all folders */ - public async listDirectories() { + public async listDirectories(): Promise { 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(async fileName => { - fileArray.push(new File(this, fileName)); + const directoryArray: Directory[] = []; + const duplexStream = plugins.streamfunction.createDuplexStream(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 { + 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 = ''; + } } diff --git a/ts/smartbucket.classes.file.ts b/ts/smartbucket.classes.file.ts index 28277cc..c551c34 100644 --- a/ts/smartbucket.classes.file.ts +++ b/ts/smartbucket.classes.file.ts @@ -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()) diff --git a/ts/smartbucket.plugins.ts b/ts/smartbucket.plugins.ts index 19aa9a4..c8b526a 100644 --- a/ts/smartbucket.plugins.ts +++ b/ts/smartbucket.plugins.ts @@ -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';