import path from "path" ; import { Readable } from "stream" ; import { IStorage , AdapterConfig , JSON as TypeJSON } from "./types" ; const adapterClasses = { b2 : "AdapterBackblazeB2" , s3 : "AdapterAmazonS3" , gcs : "AdapterGoogleCloudStorage" , local : "AdapterLocal" , } ; const adapterFunctions = { b2f : "AdapterBackblazeB2F" , } ; const availableAdapters = Object . keys ( adapterClasses ) . concat ( Object . keys ( adapterFunctions ) ) . reduce ( ( acc , val ) => { if ( acc . findIndex ( v => v === val ) === - 1 ) { acc . push ( val ) ; } return acc ; } , [ ] ) . sort ( ) . join ( ", " ) ; export class Storage implements IStorage { private adapter : IStorage ; constructor ( config ) { this . switchAdapter ( config ) ; } public getType ( ) { return this . adapter . getType ( ) ; } public getConfiguration ( ) { return this . adapter . getConfiguration ( ) ; } public switchAdapter ( args ) { let type ; if ( typeof args === "string" ) { type = args . substring ( 0 , args . indexOf ( "://" ) ) ; } else { type = args . type ; } if ( ! adapterClasses [ type ] && ! adapterFunctions [ type ] ) { throw new Error ( `unsupported storage type, must be one of ${ availableAdapters }` ) ; } if ( adapterClasses [ type ] ) { const name = adapterClasses [ type ] ; const AdapterClass = require ( path . join ( __dirname , name ) ) [ name ] ; this . adapter = new AdapterClass ( args ) ; } else if ( adapterFunctions [ type ] ) { const name = adapterFunctions [ type ] ; const module = require ( path . join ( __dirname , name ) ) ; this . adapter = module . createAdapter ( args ) ; } } async init ( ) { return this . adapter . init ( ) ; } async test ( ) { return this . adapter . test ( ) ; } async addFileFromBuffer ( buffer , targetPath ) { return this . adapter . addFileFromBuffer ( buffer , targetPath ) ; } async addFileFromPath ( origPath , targetPath ) { return this . adapter . addFileFromPath ( origPath , targetPath ) ; } async addFileFromReadable ( stream , targetPath ) { return this . adapter . addFileFromReadable ( stream , targetPath ) ; } async createBucket ( name ? ) { return this . adapter . createBucket ( name ) ; } async clearBucket ( name ? ) { return this . adapter . clearBucket ( name ) ; } async deleteBucket ( name ? ) { return this . adapter . deleteBucket ( name ) ; } async listBuckets ( ) { return this . adapter . listBuckets ( ) ; } public getSelectedBucket ( ) { return this . adapter . getSelectedBucket ( ) ; } async getFileAsReadable ( name , options = { } ) { const { start = 0 , end } = options ; return this . adapter . getFileAsReadable ( name , { start , end } ) ; } async removeFile ( fileName ) { return this . adapter . removeFile ( fileName ) ; } async listFiles ( numFiles ? ) { return this . adapter . listFiles ( numFiles ) ; } async selectBucket ( name ? ) { return this . adapter . selectBucket ( name ) ; } async sizeOf ( name ) { return this . adapter . sizeOf ( name ) ; } async fileExists ( name ) { return this . adapter . fileExists ( name ) ; } }