191 lines
5.4 KiB
TypeScript
191 lines
5.4 KiB
TypeScript
import * as plugins from './smartbucket.plugins';
|
|
import { Bucket } from './smartbucket.classes.bucket';
|
|
import { File } from './smartbucket.classes.file';
|
|
|
|
export class Directory {
|
|
public bucketRef: Bucket;
|
|
public parentDirectoryRef: Directory;
|
|
public name: string;
|
|
|
|
public tree: string[];
|
|
public files: string[];
|
|
public folders: string[];
|
|
|
|
constructor(bucketRefArg: Bucket, parentDiretory: Directory, name: string) {
|
|
this.bucketRef = bucketRefArg;
|
|
this.parentDirectoryRef = parentDiretory;
|
|
this.name = name;
|
|
}
|
|
|
|
/**
|
|
* returns an array of parent directories
|
|
*/
|
|
public getParentDirectories(): Directory[] {
|
|
let parentDirectories: Directory[] = [];
|
|
if (this.parentDirectoryRef) {
|
|
parentDirectories.push(this.parentDirectoryRef);
|
|
parentDirectories = parentDirectories.concat(this.parentDirectoryRef.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) {
|
|
if (parentDir.name === '') {
|
|
basePath = this.name;
|
|
continue;
|
|
}
|
|
basePath = parentDir.name + '/' + this.name;
|
|
}
|
|
return basePath;
|
|
}
|
|
|
|
/**
|
|
* 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.getBasePath()
|
|
);
|
|
const fileArray: File[] = [];
|
|
const duplexStream = plugins.streamfunction.createDuplexStream<plugins.minio.BucketItem, void>(
|
|
async bucketItem => {
|
|
if(!bucketItem.name) {
|
|
return;
|
|
}
|
|
let subtractedPath = bucketItem.name.replace(this.getBasePath(), '');
|
|
if (subtractedPath.startsWith('/')) {
|
|
subtractedPath = subtractedPath.substr(1);
|
|
}
|
|
if (!subtractedPath.includes('/')) {
|
|
fileArray.push(new File(this, subtractedPath));
|
|
}
|
|
|
|
},
|
|
async tools => {
|
|
done.resolve();
|
|
}
|
|
);
|
|
fileNameStream.pipe(duplexStream);
|
|
await done.promise;
|
|
return fileArray;
|
|
}
|
|
|
|
/**
|
|
* lists all folders
|
|
*/
|
|
public async listDirectories(): Promise<Directory[]> {
|
|
const done = plugins.smartpromise.defer();
|
|
const completeDirStream = await this.bucketRef.smartbucketRef.minioClient.listObjectsV2(
|
|
this.bucketRef.name,
|
|
this.getBasePath(),
|
|
true
|
|
);
|
|
const directoryArray: Directory[] = [];
|
|
const duplexStream = plugins.streamfunction.createDuplexStream<plugins.minio.BucketItem, void>(
|
|
async bucketItem => {
|
|
let subtractedPath = bucketItem.name.replace(this.getBasePath(), '');
|
|
if (subtractedPath.startsWith('/')) {
|
|
subtractedPath = subtractedPath.substr(1);
|
|
}
|
|
if (subtractedPath.includes('/')) {
|
|
const dirName = subtractedPath.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 directoryArray;
|
|
}
|
|
|
|
/**
|
|
* 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.getBasePath(),
|
|
true
|
|
);
|
|
}
|
|
|
|
/**
|
|
* gets a sub directory
|
|
*/
|
|
public async getSubDirectoryByName(dirNameArg: string): Promise<Directory> {
|
|
const dirNameArray = dirNameArg.split('/');
|
|
|
|
const getDirectory = async (directoryArg: Directory, dirNameToSearch: string) => {
|
|
const directories = await directoryArg.listDirectories();
|
|
return directories.find(directory => {
|
|
return directory.name === dirNameToSearch;
|
|
});
|
|
};
|
|
let wantedDirectory: Directory;
|
|
for (const dirNameToSearch of dirNameArray) {
|
|
const directoryToSearchIn = wantedDirectory ? wantedDirectory : this;
|
|
wantedDirectory = await getDirectory(directoryToSearchIn, dirNameToSearch);
|
|
}
|
|
return wantedDirectory;
|
|
}
|
|
|
|
/**
|
|
* moves the directory
|
|
*/
|
|
public async move() {
|
|
// TODO
|
|
}
|
|
|
|
/**
|
|
* creates a file within this directory
|
|
* @param relativePathArg
|
|
*/
|
|
public async createFile(relativePathArg) {
|
|
let completeFilePath: string = '';
|
|
}
|
|
|
|
// file operations
|
|
public async fastStore(pathArg: string, contentArg: string) {
|
|
const path = plugins.path.join(this.getBasePath(), pathArg);
|
|
await this.bucketRef.fastStore(path, contentArg);
|
|
}
|
|
|
|
public async fastGet(pathArg: string) {
|
|
const path = plugins.path.join(this.getBasePath(), pathArg);
|
|
const result = await this.bucketRef.fastGet(path);
|
|
return result;
|
|
}
|
|
|
|
public async fastGetStream(pathArg: string): Promise<plugins.smartrx.rxjs.ReplaySubject<string>> {
|
|
const path = plugins.path.join(this.getBasePath(), pathArg);
|
|
const result = await this.bucketRef.fastGetStream(path);
|
|
return result;
|
|
}
|
|
|
|
public async fastRemove(pathArg: string) {
|
|
const path = plugins.path.join(this.getBasePath(), pathArg);
|
|
await this.bucketRef.fastRemove(path);
|
|
}
|
|
}
|