Skip to content

Commit

Permalink
remove variables and cached drivers from storage module
Browse files Browse the repository at this point in the history
  • Loading branch information
vinayak25 committed Nov 5, 2024
1 parent 72c06e0 commit 231217b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 46 deletions.
4 changes: 2 additions & 2 deletions lib/storage/drivers/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export class Local implements StorageDriver {
): Promise<boolean> {
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) {
Expand All @@ -207,7 +207,7 @@ export class Local implements StorageDriver {
): Promise<boolean> {
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;
Expand Down
4 changes: 2 additions & 2 deletions lib/storage/drivers/s3Storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export class S3Storage implements StorageDriver {
): Promise<boolean> {
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) {
Expand All @@ -254,7 +254,7 @@ export class S3Storage implements StorageDriver {
): Promise<boolean> {
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;
Expand Down
69 changes: 29 additions & 40 deletions lib/storage/service.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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);
}
}
4 changes: 2 additions & 2 deletions lib/storage/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Buffer> {
Expand Down

0 comments on commit 231217b

Please sign in to comment.