93 lines
2.7 KiB
TypeScript
93 lines
2.7 KiB
TypeScript
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) {
|
|
const buckets = await smartbucketRef.minioClient.listBuckets();
|
|
const foundBucket = buckets.find(bucket => {
|
|
return bucket.name === bucketNameArg;
|
|
});
|
|
|
|
if (foundBucket) {
|
|
console.log(`bucket with name ${bucketNameArg} exists.`)
|
|
console.log(`Taking this as base for new Bucket instance`);
|
|
return new this(smartbucketRef, bucketNameArg);
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static async createBucketByName(smartbucketRef: SmartBucket, bucketName: string) {
|
|
await smartbucketRef.minioClient.makeBucket(bucketName, 'ams3').catch(e => console.log(e));
|
|
return new Bucket(smartbucketRef, bucketName);
|
|
}
|
|
|
|
public static async removeBucketByName(smartbucketRef: SmartBucket, bucketName: string) {
|
|
await smartbucketRef.minioClient.removeBucket(bucketName).catch(e => console.log(e));
|
|
}
|
|
|
|
public smartbucketRef: SmartBucket;
|
|
public name: string;
|
|
|
|
constructor(smartbucketRef: SmartBucket, bucketName: string) {
|
|
this.smartbucketRef = smartbucketRef;
|
|
this.name = bucketName;
|
|
}
|
|
|
|
/**
|
|
* gets the base directory of the bucket
|
|
*/
|
|
public async getBaseDirectory() {
|
|
return new Directory(this, null, '');
|
|
}
|
|
|
|
|
|
// ===============
|
|
// Fast Operations
|
|
// ===============
|
|
|
|
/**
|
|
* store file
|
|
*/
|
|
public async fastStore(pathArg: string, fileContent: string) {
|
|
const streamIntake = new plugins.streamfunction.Intake();
|
|
const putPromise = this.smartbucketRef.minioClient.putObject(this.name, pathArg, streamIntake.getReadable()).catch(e => console.log(e));
|
|
streamIntake.pushData(fileContent);
|
|
streamIntake.signalEnd();
|
|
await putPromise;
|
|
}
|
|
|
|
/**
|
|
* get file
|
|
*/
|
|
public async fastGet(pathArg: string) {
|
|
const done = plugins.smartpromise.defer();
|
|
const fileStream = await this.smartbucketRef.minioClient.getObject(this.name, pathArg).catch(e => console.log(e));
|
|
let completeFile: string = '';
|
|
const duplexStream = plugins.streamfunction.createDuplexStream<Buffer, Buffer>(async (chunk) => {
|
|
const chunkString = chunk.toString();
|
|
completeFile += chunkString;
|
|
return chunk;
|
|
}, async (cb) => {
|
|
done.resolve();
|
|
return Buffer.from('');
|
|
});
|
|
|
|
if (!fileStream) {
|
|
return null;
|
|
}
|
|
|
|
fileStream.pipe(duplexStream);
|
|
await done.promise;
|
|
return completeFile;
|
|
}
|
|
|
|
/**
|
|
* removeObject
|
|
*/
|
|
public async fastRemove (pathArg: string) {
|
|
await this.smartbucketRef.minioClient.removeObject(this.name, pathArg);
|
|
}
|
|
}
|