smarts3/ts/index.ts

64 lines
1.6 KiB
TypeScript
Raw Normal View History

2021-12-18 00:41:50 +00:00
import * as plugins from './smarts3.plugins';
import * as paths from './paths';
export interface ISmarts3ContructorOptions {
port?: number;
cleanSlate?: boolean;
}
export class Smarts3 {
2021-12-20 16:06:42 +00:00
// STATIC
public static async createAndStart(optionsArg: ConstructorParameters<typeof Smarts3>[0]) {
const smartS3Instance = new Smarts3(optionsArg);
await smartS3Instance.start();
return smartS3Instance;
}
// INSTANCE
2021-12-18 00:41:50 +00:00
public options: ISmarts3ContructorOptions;
2021-12-18 01:17:42 +00:00
public dataForClient = {
s3AccessKey: 'S3RVER',
s3AccessSecret: 'S3RVER',
port: 3000,
useSsl: false
}
2021-12-18 00:41:50 +00:00
public s3Instance: plugins.s3rver;
constructor(optionsArg: ISmarts3ContructorOptions) {
this.options = optionsArg;
2021-12-18 01:17:42 +00:00
this.options = {
...this.options,
...optionsArg
}
2021-12-18 00:41:50 +00:00
}
public async start() {
if (this.options.cleanSlate) {
await plugins.smartfile.fs.ensureEmptyDir(paths.bucketsDir);
} else {
await plugins.smartfile.fs.ensureDir(paths.bucketsDir);
}
this.s3Instance = new plugins.s3rver({
port: this.options.port || 3000,
address: '0.0.0.0',
silent: false,
2021-12-18 00:48:03 +00:00
directory: paths.bucketsDir
2021-12-18 00:41:50 +00:00
})
await this.s3Instance.run();
console.log('s3 server is running');
}
2021-12-20 15:38:51 +00:00
public async getS3Descriptor(): Promise<plugins.smartbucket.ISmartBucketConfig> {
2021-12-20 15:33:13 +00:00
return {
accessKey: this.dataForClient.s3AccessKey,
accessSecret: this.dataForClient.s3AccessSecret,
endpoint: 'localhost',
port: this.dataForClient.port,
useSsl: this.dataForClient.useSsl,
}
}
2021-12-18 00:41:50 +00:00
public async stop() {
await this.s3Instance.close();
}
}