fix(core): update
This commit is contained in:
@ -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
|
||||
|
@ -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 = '';
|
||||
}
|
||||
}
|
||||
|
@ -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())
|
||||
|
@ -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';
|
||||
|
Reference in New Issue
Block a user