smartbucket/ts/smartbucket.classes.bucket.ts

118 lines
3.3 KiB
TypeScript
Raw Normal View History

2019-10-14 18:55:07 +00:00
import * as plugins from './smartbucket.plugins';
2019-10-15 12:16:28 +00:00
import { SmartBucket } from './smartbucket.classes.smartbucket';
2019-10-16 17:11:28 +00:00
import { Directory } from './smartbucket.classes.directory';
2019-10-20 10:27:58 +00:00
import { Observable } from 'rxjs';
2019-10-14 18:55:07 +00:00
2019-10-15 12:16:28 +00:00
export class Bucket {
2019-10-15 17:23:06 +00:00
public static async getBucketByName(smartbucketRef: SmartBucket, bucketNameArg: string) {
2019-10-15 12:16:28 +00:00
const buckets = await smartbucketRef.minioClient.listBuckets();
2020-10-12 00:37:50 +00:00
const foundBucket = buckets.find((bucket) => {
2019-10-15 12:16:28 +00:00
return bucket.name === bucketNameArg;
});
if (foundBucket) {
2019-10-16 17:15:48 +00:00
console.log(`bucket with name ${bucketNameArg} exists.`);
2019-10-15 12:16:28 +00:00
console.log(`Taking this as base for new Bucket instance`);
return new this(smartbucketRef, bucketNameArg);
2019-10-15 17:23:06 +00:00
} else {
return null;
2019-10-15 12:16:28 +00:00
}
}
2019-10-15 17:23:06 +00:00
public static async createBucketByName(smartbucketRef: SmartBucket, bucketName: string) {
2020-10-12 00:37:50 +00:00
await smartbucketRef.minioClient.makeBucket(bucketName, 'ams3').catch((e) => console.log(e));
2019-10-15 17:23:06 +00:00
return new Bucket(smartbucketRef, bucketName);
}
public static async removeBucketByName(smartbucketRef: SmartBucket, bucketName: string) {
2020-10-12 00:37:50 +00:00
await smartbucketRef.minioClient.removeBucket(bucketName).catch((e) => console.log(e));
2019-10-15 17:23:06 +00:00
}
2019-10-15 12:16:28 +00:00
public smartbucketRef: SmartBucket;
public name: string;
2019-10-15 17:23:06 +00:00
2019-10-15 12:16:28 +00:00
constructor(smartbucketRef: SmartBucket, bucketName: string) {
this.smartbucketRef = smartbucketRef;
this.name = bucketName;
}
2019-10-15 17:23:06 +00:00
2019-10-16 17:11:28 +00:00
/**
* gets the base directory of the bucket
*/
public async getBaseDirectory() {
return new Directory(this, null, '');
}
2019-10-16 16:12:18 +00:00
// ===============
// Fast Operations
// ===============
2019-10-15 17:23:06 +00:00
/**
* store file
*/
2019-10-16 16:12:18 +00:00
public async fastStore(pathArg: string, fileContent: string) {
2019-10-16 13:21:02 +00:00
const streamIntake = new plugins.streamfunction.Intake();
2019-10-16 17:15:48 +00:00
const putPromise = this.smartbucketRef.minioClient
.putObject(this.name, pathArg, streamIntake.getReadable())
2020-10-12 00:37:50 +00:00
.catch((e) => console.log(e));
2019-10-16 13:21:02 +00:00
streamIntake.pushData(fileContent);
streamIntake.signalEnd();
await putPromise;
}
/**
* get file
*/
2019-10-16 16:12:18 +00:00
public async fastGet(pathArg: string) {
2019-10-16 13:21:02 +00:00
const done = plugins.smartpromise.defer();
2020-06-19 00:42:26 +00:00
let completeFile: Buffer;
2019-10-20 10:27:58 +00:00
const replaySubject = await this.fastGetStream(pathArg);
replaySubject.subscribe(
2020-10-12 00:37:50 +00:00
(chunk) => {
2020-06-19 00:42:26 +00:00
if (completeFile) {
completeFile = Buffer.concat([completeFile, chunk]);
}
2019-10-20 10:27:58 +00:00
},
2020-10-12 00:37:50 +00:00
(err) => {
2019-10-20 10:27:58 +00:00
console.log(err);
},
() => {
done.resolve();
}
);
await done.promise;
return completeFile;
}
2020-06-19 00:42:26 +00:00
public async fastGetStream(pathArg: string): Promise<plugins.smartrx.rxjs.ReplaySubject<Buffer>> {
2019-10-16 17:15:48 +00:00
const fileStream = await this.smartbucketRef.minioClient
.getObject(this.name, pathArg)
2020-10-12 00:37:50 +00:00
.catch((e) => console.log(e));
2020-06-19 00:42:26 +00:00
const replaySubject = new plugins.smartrx.rxjs.ReplaySubject<Buffer>();
2019-10-16 17:15:48 +00:00
const duplexStream = plugins.streamfunction.createDuplexStream<Buffer, Buffer>(
2020-10-12 00:37:50 +00:00
async (chunk) => {
2020-06-19 00:42:26 +00:00
replaySubject.next(chunk);
2019-10-16 17:15:48 +00:00
return chunk;
},
2020-10-12 00:37:50 +00:00
async (cb) => {
2019-10-20 10:27:58 +00:00
replaySubject.complete();
2019-10-16 17:15:48 +00:00
return Buffer.from('');
}
);
2019-10-16 13:21:02 +00:00
if (!fileStream) {
return null;
}
fileStream.pipe(duplexStream);
2019-10-20 10:27:58 +00:00
return replaySubject;
2019-10-16 13:21:02 +00:00
}
/**
* removeObject
*/
2019-10-16 17:15:48 +00:00
public async fastRemove(pathArg: string) {
2019-10-16 13:21:02 +00:00
await this.smartbucketRef.minioClient.removeObject(this.name, pathArg);
2019-10-15 17:23:06 +00:00
}
2019-10-15 12:16:28 +00:00
}