express-storage
Version:
A simple and powerful file upload and storage management package for Express.js applications. Supports multiple storage drivers including S3, GCS, OCI, and local storage with presigned URL support.
117 lines • 3.39 kB
JavaScript
import { LocalStorageDriver } from '../drivers/local.driver.js';
import { S3StorageDriver, S3PresignedStorageDriver } from '../drivers/s3.driver.js';
import { GCSStorageDriver, GCSPresignedStorageDriver } from '../drivers/gcs.driver.js';
import { OCIStorageDriver, OCIPresignedStorageDriver } from '../drivers/oci.driver.js';
/**
* Factory class for creating storage drivers
*/
export class StorageDriverFactory {
/**
* Create and return a storage driver based on configuration
*/
static createDriver(config) {
const driverKey = this.getDriverKey(config);
// Return cached driver if exists
if (this.drivers.has(driverKey)) {
return this.drivers.get(driverKey);
}
// Create new driver
const driver = this.createNewDriver(config);
// Cache the driver
this.drivers.set(driverKey, driver);
return driver;
}
/**
* Create a new driver instance
*/
static createNewDriver(config) {
switch (config.driver) {
case 'local':
return new LocalStorageDriver(config);
case 's3':
return this.createS3Driver(config);
case 's3-presigned':
return this.createS3PresignedDriver(config);
case 'gcs':
return this.createGCSDriver(config);
case 'gcs-presigned':
return this.createGCSPresignedDriver(config);
case 'oci':
return this.createOCIDriver(config);
case 'oci-presigned':
return this.createOCIPresignedDriver(config);
default:
throw new Error(`Unsupported storage driver: ${config.driver}`);
}
}
/**
* Create S3 driver
*/
static createS3Driver(config) {
return new S3StorageDriver(config);
}
/**
* Create S3 presigned driver
*/
static createS3PresignedDriver(config) {
return new S3PresignedStorageDriver(config);
}
/**
* Create GCS driver
*/
static createGCSDriver(config) {
return new GCSStorageDriver(config);
}
/**
* Create GCS presigned driver
*/
static createGCSPresignedDriver(config) {
return new GCSPresignedStorageDriver(config);
}
/**
* Create OCI driver
*/
static createOCIDriver(config) {
return new OCIStorageDriver(config);
}
/**
* Create OCI presigned driver
*/
static createOCIPresignedDriver(config) {
return new OCIPresignedStorageDriver(config);
}
/**
* Generate unique key for driver caching
*/
static getDriverKey(config) {
return `${config.driver}_${config.bucketName || 'local'}_${config.localPath || 'default'}`;
}
/**
* Clear cached drivers
*/
static clearCache() {
this.drivers.clear();
}
/**
* Get cached driver count
*/
static getCachedDriverCount() {
return this.drivers.size;
}
/**
* Get available drivers
*/
static getAvailableDrivers() {
return [
'local',
's3',
's3-presigned',
'gcs',
'gcs-presigned',
'oci',
'oci-presigned'
];
}
}
StorageDriverFactory.drivers = new Map();
//# sourceMappingURL=driver.factory.js.map