import { Readable } from "stream" ; import { generateSlug , generateSlugPath , validateName } from "./util" ; import { AdapterConfig , IStorage } from "./types" ; export abstract class AbstractAdapter implements IStorage { protected type : string ; protected config : AdapterConfig ; protected bucketName : string = "" ; protected initialized : boolean = false ; protected slug : boolean = true ; getType ( ) { return this . type ; } public getConfiguration ( ) { return this . config ; } protected generateSlug ( url , slug = this . slug ) { return generateSlug ( url , slug ) ; } protected validateName ( name ) { return validateName ( name ) ; } async test ( ) { if ( this . initialized === false ) { return Promise . reject ( "storage has not been initialized yet; call Storage.init() first" ) ; } if ( this . bucketName ) { try { await this . listFiles ( ) ; return Promise . resolve ( "ok" ) ; } catch ( e ) { throw new Error ( `Looks like the storage configuration is not correct (${ e . message })` ) ; } } try { await this . listBuckets ( ) ; return Promise . resolve ( "ok" ) ; } catch ( e ) { throw new Error ( `Looks like the storage configuration is not correct (${ e . message })` ) ; } } async addFileFromPath ( origPath , targetPath ) { await this . store ( origPath , generateSlugPath ( targetPath , this . slug ) ) ; } async addFileFromBuffer ( buffer , targetPath ) { await this . store ( buffer , generateSlugPath ( targetPath , this . slug ) ) ; } async addFileFromReadable ( stream , targetPath ) { await this . store ( stream , generateSlugPath ( targetPath , this . slug ) ) ; } public getSelectedBucket ( ) { return this . bucketName ; } protected abstract async store ( filePath , targetFileName ) ; protected abstract async store ( buffer , targetFileName ) ; protected abstract async store ( stream , targetFileName ) ; abstract async init ( ) ; abstract async selectBucket ( name ) ; abstract async createBucket ( name ) ; abstract async clearBucket ( name ? ) ; abstract async deleteBucket ( name ? ) ; abstract async listBuckets ( ) ; abstract async getFileAsReadable ( name , options ? ) ; abstract async removeFile ( fileName ) ; abstract async listFiles ( ) ; abstract async sizeOf ( name ) ; abstract async fileExists ( name ) ; }