diff --git a/alkemio.yml b/alkemio.yml index d83e2e9a07..293834480b 100644 --- a/alkemio.yml +++ b/alkemio.yml @@ -277,6 +277,9 @@ communications: ## storage ## # Alkemio uses multiple types of persistent storage, including SQL database, postgres database, file storage, redis. storage: + # + enabled: ${STORAGE_ENABLED}:true + # file: # 20MB max_file_size: ${STORAGE_MAX_FILE_SIZE}:20971520 diff --git a/package-lock.json b/package-lock.json index 9f14a98d82..c21a5616e5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "alkemio-server", - "version": "0.96.2", + "version": "0.97.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "alkemio-server", - "version": "0.96.2", + "version": "0.97.0", "license": "EUPL-1.2", "dependencies": { "@alkemio/matrix-adapter-lib": "^0.4.1", diff --git a/package.json b/package.json index 99381a3c59..9a81d2c33e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "alkemio-server", - "version": "0.96.2", + "version": "0.97.0", "description": "Alkemio server, responsible for managing the shared Alkemio platform", "author": "Alkemio Foundation", "private": false, diff --git a/src/common/enums/alkemio.error.status.ts b/src/common/enums/alkemio.error.status.ts index fadc368932..11b6834211 100644 --- a/src/common/enums/alkemio.error.status.ts +++ b/src/common/enums/alkemio.error.status.ts @@ -64,6 +64,7 @@ export enum AlkemioErrorStatus { EXCALIDRAW_REDIS_ADAPTER_INIT = 'EXCALIDRAW_REDIS_INIT', EXCALIDRAW_SERVER_INIT = 'EXCALIDRAW_SERVER_INIT', UNSPECIFIED = 'UNSPECIFIED', + STORAGE_DISABLED = 'STORAGE_DISABLED', LOCAL_STORAGE_SAVE_FAILED = 'LOCAL_STORAGE_SAVE_FAILED', LOCAL_STORAGE_READ_FAILED = 'LOCAL_STORAGE_READ_FAILED', LOCAL_STORAGE_DELETE_FAILED = 'LOCAL_STORAGE_DELETE_FAILED', diff --git a/src/common/exceptions/storage/index.ts b/src/common/exceptions/storage/index.ts index 1768088474..9ee2cd768f 100644 --- a/src/common/exceptions/storage/index.ts +++ b/src/common/exceptions/storage/index.ts @@ -1,3 +1,5 @@ export * from './local-storage/local.storage.delete.failed.exception'; export * from './local-storage/local.storage.save.failed.exception'; export * from './local-storage/local.storage.read.failed.exception'; + +export * from './storage.disabled.exception'; diff --git a/src/common/exceptions/storage/storage.disabled.exception.ts b/src/common/exceptions/storage/storage.disabled.exception.ts new file mode 100644 index 0000000000..8926601b3d --- /dev/null +++ b/src/common/exceptions/storage/storage.disabled.exception.ts @@ -0,0 +1,9 @@ +import { AlkemioErrorStatus, LogContext } from '@common/enums'; +import { BaseException } from '../../exceptions/base.exception'; +import { ExceptionDetails } from '../../exceptions/exception.details'; + +export class StorageDisabledException extends BaseException { + constructor(error: string, context: LogContext, details?: ExceptionDetails) { + super(error, context, AlkemioErrorStatus.STORAGE_DISABLED, details); + } +} diff --git a/src/services/adapters/storage/local-storage/local.storage.adapter.ts b/src/services/adapters/storage/local-storage/local.storage.adapter.ts index 299650b344..8ed98c5526 100644 --- a/src/services/adapters/storage/local-storage/local.storage.adapter.ts +++ b/src/services/adapters/storage/local-storage/local.storage.adapter.ts @@ -13,6 +13,7 @@ import { import { StorageService } from '../storage.service.interface'; import { StorageServiceType } from '../storage.service.type'; import { AlkemioConfig } from '@src/types'; +import { StorageDisabledException } from '@common/exceptions/storage/storage.disabled.exception'; const writeFileAsync = promisify(writeFile); const readFileAsync = promisify(readFile); @@ -20,9 +21,11 @@ const unlinkAsync = promisify(unlink); @Injectable() export class LocalStorageAdapter implements StorageService { + private readonly enabled: boolean; private readonly storagePath: string; constructor(private configService: ConfigService) { + this.enabled = this.configService.get('storage.enabled', { infer: true }); const pathFromConfig = this.configService.get( 'storage.local_storage.path', { infer: true } @@ -36,10 +39,24 @@ export class LocalStorageAdapter implements StorageService { } public save(data: Buffer) { + if (!this.enabled) { + throw new StorageDisabledException( + 'Storage is currently disabled', + LogContext.LOCAL_STORAGE + ); + } + return this.saveFromBuffer(data); } public async read(fileName: string): Promise | never { + if (!this.enabled) { + throw new StorageDisabledException( + 'Storage is currently disabled', + LogContext.LOCAL_STORAGE + ); + } + const filePath = this.getFilePath(fileName); try { return await readFileAsync(filePath); @@ -58,6 +75,13 @@ export class LocalStorageAdapter implements StorageService { } public async delete(fileName: string): Promise | never { + if (!this.enabled) { + throw new StorageDisabledException( + 'Storage is currently disabled', + LogContext.LOCAL_STORAGE + ); + } + const filePath = this.getFilePath(fileName); try { @@ -77,6 +101,13 @@ export class LocalStorageAdapter implements StorageService { } public exists(fileName: string): boolean { + if (!this.enabled) { + throw new StorageDisabledException( + 'Storage is currently disabled', + LogContext.LOCAL_STORAGE + ); + } + const filePath = this.getFilePath(fileName); return existsSync(filePath); } diff --git a/src/types/alkemio.config.ts b/src/types/alkemio.config.ts index 50d280cfcd..cf3bbaa45b 100644 --- a/src/types/alkemio.config.ts +++ b/src/types/alkemio.config.ts @@ -96,6 +96,7 @@ export type AlkemioConfig = { }; }; storage: { + enabled: boolean; file: { max_file_size: number; };