From 231217b8c7d730c93721cea42dfbd9188f0d9376 Mon Sep 17 00:00:00 2001 From: Vinayak Sarawagi Date: Wed, 6 Nov 2024 01:07:42 +0530 Subject: [PATCH] remove variables and cached drivers from storage module --- lib/storage/drivers/local.ts | 4 +- lib/storage/drivers/s3Storage.ts | 4 +- lib/storage/service.ts | 69 ++++++++++++++------------------ lib/storage/storage.ts | 4 +- 4 files changed, 35 insertions(+), 46 deletions(-) diff --git a/lib/storage/drivers/local.ts b/lib/storage/drivers/local.ts index fe8442e..5bc21e9 100644 --- a/lib/storage/drivers/local.ts +++ b/lib/storage/drivers/local.ts @@ -186,7 +186,7 @@ export class Local implements StorageDriver { ): Promise { try { const buffer = await this.get(sourcePath); - const driver = StorageService.getDriver(destinationDisk); + const driver = StorageService.getDisk(destinationDisk); await driver.put(destinationPath, buffer); return true; } catch (e) { @@ -207,7 +207,7 @@ export class Local implements StorageDriver { ): Promise { try { const buffer = await this.get(sourcePath); - const driver = StorageService.getDriver(destinationDisk); + const driver = StorageService.getDisk(destinationDisk); await driver.put(destinationPath, buffer); await this.delete(sourcePath); return true; diff --git a/lib/storage/drivers/s3Storage.ts b/lib/storage/drivers/s3Storage.ts index 7b7f50e..453f2bc 100644 --- a/lib/storage/drivers/s3Storage.ts +++ b/lib/storage/drivers/s3Storage.ts @@ -233,7 +233,7 @@ export class S3Storage implements StorageDriver { ): Promise { try { const buffer = await this.get(sourcePath); - const driver = StorageService.getDriver(destinationDisk); + const driver = StorageService.getDisk(destinationDisk); await driver.put(destinationPath, buffer); return true; } catch (e) { @@ -254,7 +254,7 @@ export class S3Storage implements StorageDriver { ): Promise { try { const buffer = await this.get(sourcePath); - const driver = StorageService.getDriver(destinationDisk); + const driver = StorageService.getDisk(destinationDisk); await driver.put(destinationPath, buffer); await this.delete(sourcePath); return true; diff --git a/lib/storage/service.ts b/lib/storage/service.ts index 5d86f24..0156230 100644 --- a/lib/storage/service.ts +++ b/lib/storage/service.ts @@ -1,15 +1,9 @@ import { Injectable, Type } from '@nestjs/common'; import { ConfigService } from '../config/service'; -import { logTime } from '../utils/helpers'; import { InternalLogger } from '../utils/logger'; import { Local, S3Storage } from './drivers'; import { DiskNotFoundException } from './exceptions/diskNotFound'; -import { - LocalDiskOptions, - S3DiskOptions, - StorageOptions, - StorageDriver, -} from './interfaces'; +import { LocalDiskOptions, S3DiskOptions, StorageDriver } from './interfaces'; @Injectable() export class StorageService { @@ -19,45 +13,40 @@ export class StorageService { }; private static disks: { [key: string]: any }; - private static options: StorageOptions; - - constructor(private config: ConfigService) { - StorageService.options = this.config.get('filesystem') as StorageOptions; - const disksConfig = StorageService.options.disks; - StorageService.disks = {}; - for (const diskName in StorageService.options.disks) { - const time = Date.now(); - const diskConfig = disksConfig[diskName]; - const driver = StorageService.driverMap[diskConfig.driver]; - if (!driver) { - InternalLogger.error( - 'StorageService', - `We couldn't find any disk driver associated with the [${diskName}].`, - ); - continue; - } - - StorageService.disks[diskName] = new driver(diskName, diskConfig); - InternalLogger.success( - 'StorageService', - `Disk [${diskName}] successfully initiailized ${logTime( - Date.now() - time, - )}`, - ); - } - } - static buildDriver(config: S3DiskOptions | LocalDiskOptions): StorageDriver { + constructor() {} + + static newDisk(config: S3DiskOptions | LocalDiskOptions): StorageDriver { const driver = StorageService.driverMap[config.driver]; if (!driver) throw new DiskNotFoundException(config); return new driver('', config); } - static getDriver(disk?: string): StorageDriver { - disk = disk || this.options.default; - if (StorageService.disks[disk]) { - return StorageService.disks[disk]; + static getDisk(disk?: string): StorageDriver { + const options = ConfigService.get('filesystem'); + + disk = disk || options.default; + if (this.disks.has(disk)) return this.disks.get(disk); + + const diskConfig = options.disks[disk]; + if (!diskConfig) { + InternalLogger.error( + 'StorageService', + `We couldn't find any configuration defined for ${disk} disk.`, + ); + return; } - throw new DiskNotFoundException({ disk }); + + const driver = this.driverMap[diskConfig.driver]; + if (!driver) { + InternalLogger.error( + 'StorageService', + `We couldn't find any disk driver associated with the [${disk}].`, + ); + return; + } + + this.disks.set(disk, this.newDisk(diskConfig)); + return this.disks.get(disk); } } diff --git a/lib/storage/storage.ts b/lib/storage/storage.ts index 2b69a94..4811f95 100644 --- a/lib/storage/storage.ts +++ b/lib/storage/storage.ts @@ -6,11 +6,11 @@ import { StorageService } from './service'; @Injectable() export class Storage { static build(config: S3DiskOptions | LocalDiskOptions): StorageDriver { - return StorageService.buildDriver(config); + return StorageService.newDisk(config); } static disk(disk?: string) { - return StorageService.getDriver(disk); + return StorageService.getDisk(disk); } static async download(url: string): Promise {