smartbucket/ts/smartbucket.classes.directory.ts

190 lines
5.4 KiB
TypeScript
Raw Normal View History

2019-10-14 18:55:07 +00:00
import * as plugins from './smartbucket.plugins';
2019-10-16 16:12:18 +00:00
import { Bucket } from './smartbucket.classes.bucket';
import { File } from './smartbucket.classes.file';
2019-10-14 18:55:07 +00:00
2019-10-16 16:12:18 +00:00
export class Directory {
public bucketRef: Bucket;
2019-10-18 13:43:06 +00:00
public parentDirectoryRef: Directory;
2019-10-16 17:11:28 +00:00
public name: string;
2019-10-16 16:12:18 +00:00
public tree: string[];
public files: string[];
public folders: string[];
2019-10-16 17:11:28 +00:00
constructor(bucketRefArg: Bucket, parentDiretory: Directory, name: string) {
2019-10-16 16:12:18 +00:00
this.bucketRef = bucketRefArg;
2019-10-18 13:43:06 +00:00
this.parentDirectoryRef = parentDiretory;
2019-10-16 17:11:28 +00:00
this.name = name;
}
/**
* returns an array of parent directories
*/
public getParentDirectories(): Directory[] {
let parentDirectories: Directory[] = [];
2019-10-18 13:43:06 +00:00
if (this.parentDirectoryRef) {
parentDirectories.push(this.parentDirectoryRef);
parentDirectories = parentDirectories.concat(this.parentDirectoryRef.getParentDirectories());
2019-10-16 17:11:28 +00:00
}
return parentDirectories;
}
/**
* returns the directory level
*/
public getDirectoryLevel(): number {
return this.getParentDirectories().length;
2019-10-16 17:15:48 +00:00
}
2019-10-16 17:11:28 +00:00
/**
* updates the base path
*/
public getBasePath(): string {
const parentDirectories = this.getParentDirectories();
let basePath = '';
2019-10-16 17:15:48 +00:00
for (const parentDir of parentDirectories) {
2019-10-19 23:18:13 +00:00
if (parentDir.name === '') {
basePath = this.name;
continue;
}
2019-10-19 22:45:11 +00:00
basePath = parentDir.name + '/' + this.name;
2019-10-16 17:11:28 +00:00
}
return basePath;
2019-10-16 16:12:18 +00:00
}
/**
* lists all files
*/
public async listFiles(): Promise<File[]> {
const done = plugins.smartpromise.defer();
const fileNameStream = await this.bucketRef.smartbucketRef.minioClient.listObjectsV2(
this.bucketRef.name,
2019-10-16 17:11:28 +00:00
this.getBasePath()
2019-10-16 16:12:18 +00:00
);
const fileArray: File[] = [];
2019-10-18 13:43:06 +00:00
const duplexStream = plugins.streamfunction.createDuplexStream<plugins.minio.BucketItem, void>(
async bucketItem => {
2020-05-17 15:57:12 +00:00
if (!bucketItem.name) {
2019-10-18 13:43:06 +00:00
return;
}
2019-10-19 23:19:34 +00:00
let subtractedPath = bucketItem.name.replace(this.getBasePath(), '');
if (subtractedPath.startsWith('/')) {
subtractedPath = subtractedPath.substr(1);
}
2019-10-18 13:43:06 +00:00
if (!subtractedPath.includes('/')) {
2019-10-19 23:19:34 +00:00
fileArray.push(new File(this, subtractedPath));
2019-10-18 13:43:06 +00:00
}
2019-10-16 17:15:48 +00:00
},
async tools => {
done.resolve();
}
);
2019-10-16 16:12:18 +00:00
fileNameStream.pipe(duplexStream);
await done.promise;
return fileArray;
}
/**
* lists all folders
*/
2019-10-16 17:11:28 +00:00
public async listDirectories(): Promise<Directory[]> {
2019-10-16 16:12:18 +00:00
const done = plugins.smartpromise.defer();
const completeDirStream = await this.bucketRef.smartbucketRef.minioClient.listObjectsV2(
this.bucketRef.name,
2019-10-16 17:11:28 +00:00
this.getBasePath(),
2019-10-16 16:12:18 +00:00
true
);
2019-10-16 17:11:28 +00:00
const directoryArray: Directory[] = [];
2019-10-16 17:15:48 +00:00
const duplexStream = plugins.streamfunction.createDuplexStream<plugins.minio.BucketItem, void>(
2019-10-18 13:43:06 +00:00
async bucketItem => {
2019-10-19 23:18:13 +00:00
let subtractedPath = bucketItem.name.replace(this.getBasePath(), '');
if (subtractedPath.startsWith('/')) {
subtractedPath = subtractedPath.substr(1);
}
2019-10-16 17:15:48 +00:00
if (subtractedPath.includes('/')) {
2019-10-19 23:18:13 +00:00
const dirName = subtractedPath.split('/')[0];
2019-10-16 17:15:48 +00:00
if (directoryArray.find(directory => directory.name === dirName)) {
return;
}
directoryArray.push(new Directory(this.bucketRef, this, dirName));
2019-10-16 17:11:28 +00:00
}
2019-10-16 17:15:48 +00:00
},
async tools => {
done.resolve();
2019-10-16 17:11:28 +00:00
}
2019-10-16 17:15:48 +00:00
);
2019-10-16 16:12:18 +00:00
completeDirStream.pipe(duplexStream);
await done.promise;
2019-10-16 17:11:28 +00:00
return directoryArray;
2019-10-16 16:12:18 +00:00
}
/**
* 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,
2019-10-16 17:11:28 +00:00
this.getBasePath(),
2019-10-16 16:12:18 +00:00
true
);
}
2019-10-16 17:11:28 +00:00
/**
* gets a sub directory
*/
2019-10-18 16:37:43 +00:00
public async getSubDirectoryByName(dirNameArg: string): Promise<Directory> {
2019-10-19 23:18:13 +00:00
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;
2019-10-16 17:11:28 +00:00
}
/**
* moves the directory
*/
2019-10-16 17:15:48 +00:00
public async move() {
2019-10-16 17:11:28 +00:00
// TODO
}
/**
* creates a file within this directory
2019-10-16 17:15:48 +00:00
* @param relativePathArg
2019-10-16 17:11:28 +00:00
*/
public async createFile(relativePathArg) {
let completeFilePath: string = '';
}
2019-10-18 16:44:54 +00:00
// 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);
2019-10-19 20:57:36 +00:00
const result = await this.bucketRef.fastGet(path);
return result;
2019-10-18 16:44:54 +00:00
}
2019-10-20 10:27:58 +00:00
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;
}
2019-10-18 16:44:54 +00:00
public async fastRemove(pathArg: string) {
const path = plugins.path.join(this.getBasePath(), pathArg);
await this.bucketRef.fastRemove(path);
}
2019-10-16 16:12:18 +00:00
}