diff --git a/backend/src/media/backends/azure-backend.ts b/backend/src/media/backends/azure-backend.ts index b007f1a47..9de51a3c7 100644 --- a/backend/src/media/backends/azure-backend.ts +++ b/backend/src/media/backends/azure-backend.ts @@ -35,16 +35,17 @@ export class AzureBackend implements MediaBackend { private mediaConfig: MediaConfig, ) { this.logger.setContext(AzureBackend.name); - this.config = (this.mediaConfig.backend as AzureMediaConfig).azure; - if (this.mediaConfig.backend.use === BackendType.AZURE) { - // only create the client if the backend is configured to azure - const blobServiceClient = BlobServiceClient.fromConnectionString( - this.config.connectionString, - ); - this.credential = - blobServiceClient.credential as StorageSharedKeyCredential; - this.client = blobServiceClient.getContainerClient(this.config.container); + // only create the backend if azure is configured + if (this.mediaConfig.backend.use !== BackendType.AZURE) { + return; } + this.config = this.mediaConfig.backend.azure; + const blobServiceClient = BlobServiceClient.fromConnectionString( + this.config.connectionString, + ); + this.credential = + blobServiceClient.credential as StorageSharedKeyCredential; + this.client = blobServiceClient.getContainerClient(this.config.container); } async saveFile( diff --git a/backend/src/media/backends/filesystem-backend.ts b/backend/src/media/backends/filesystem-backend.ts index 4434c8419..c0277a058 100644 --- a/backend/src/media/backends/filesystem-backend.ts +++ b/backend/src/media/backends/filesystem-backend.ts @@ -8,13 +8,11 @@ import { FileTypeResult } from 'file-type'; import { promises as fs } from 'fs'; import { join } from 'path'; -import mediaConfiguration, { - FilesystemMediaConfig, - MediaConfig, -} from '../../config/media.config'; +import mediaConfiguration, { MediaConfig } from '../../config/media.config'; import { MediaBackendError } from '../../errors/errors'; import { ConsoleLoggerService } from '../../logger/console-logger.service'; import { MediaBackend } from '../media-backend.interface'; +import { BackendType } from './backend-type.enum'; @Injectable() export class FilesystemBackend implements MediaBackend { @@ -26,9 +24,12 @@ export class FilesystemBackend implements MediaBackend { private mediaConfig: MediaConfig, ) { this.logger.setContext(FilesystemBackend.name); - this.uploadDirectory = ( - this.mediaConfig.backend as FilesystemMediaConfig - ).filesystem.uploadPath; + // only create the backend if local filesystem is configured + if (this.mediaConfig.backend.use !== BackendType.FILESYSTEM) { + this.uploadDirectory = ''; + return; + } + this.uploadDirectory = this.mediaConfig.backend.filesystem.uploadPath; } async saveFile( diff --git a/backend/src/media/backends/imgur-backend.ts b/backend/src/media/backends/imgur-backend.ts index 825cbe937..3fc11a31c 100644 --- a/backend/src/media/backends/imgur-backend.ts +++ b/backend/src/media/backends/imgur-backend.ts @@ -14,6 +14,7 @@ import mediaConfiguration, { import { MediaBackendError } from '../../errors/errors'; import { ConsoleLoggerService } from '../../logger/console-logger.service'; import { MediaBackend } from '../media-backend.interface'; +import { BackendType } from './backend-type.enum'; type UploadResult = { data: { @@ -37,7 +38,11 @@ export class ImgurBackend implements MediaBackend { private mediaConfig: MediaConfig, ) { this.logger.setContext(ImgurBackend.name); - this.config = (this.mediaConfig.backend as ImgurMediaConfig).imgur; + // only create the backend if imgur is configured + if (this.mediaConfig.backend.use !== BackendType.IMGUR) { + return; + } + this.config = this.mediaConfig.backend.imgur; } async saveFile(uuid: string, buffer: Buffer): Promise { diff --git a/backend/src/media/backends/s3-backend.ts b/backend/src/media/backends/s3-backend.ts index 7d28b1e26..373c16e04 100644 --- a/backend/src/media/backends/s3-backend.ts +++ b/backend/src/media/backends/s3-backend.ts @@ -33,6 +33,7 @@ export class S3Backend implements MediaBackend { private mediaConfig: MediaConfig, ) { this.logger.setContext(S3Backend.name); + // only create the backend if s3 is configured if (this.mediaConfig.backend.use !== BackendType.S3) { return; } diff --git a/backend/src/media/backends/webdav-backend.ts b/backend/src/media/backends/webdav-backend.ts index a98d5261f..2594e5d35 100644 --- a/backend/src/media/backends/webdav-backend.ts +++ b/backend/src/media/backends/webdav-backend.ts @@ -29,37 +29,37 @@ export class WebdavBackend implements MediaBackend { private mediaConfig: MediaConfig, ) { this.logger.setContext(WebdavBackend.name); - if (this.mediaConfig.backend.use === BackendType.WEBDAV) { - this.config = this.mediaConfig.backend.webdav; - const url = new URL(this.config.connectionString); - this.baseUrl = url.toString(); - if (this.config.uploadDir && this.config.uploadDir !== '') { - this.baseUrl = WebdavBackend.joinURL( - this.baseUrl, - this.config.uploadDir, - ); - } - this.authHeader = WebdavBackend.generateBasicAuthHeader( - url.username, - url.password, - ); - fetch(this.baseUrl, { - method: 'PROPFIND', - headers: { - Accept: 'text/plain', // eslint-disable-line @typescript-eslint/naming-convention - Authorization: this.authHeader, // eslint-disable-line @typescript-eslint/naming-convention - Depth: '0', // eslint-disable-line @typescript-eslint/naming-convention - }, - }) - .then((response) => { - if (!response.ok) { - throw new Error(`Can't access ${this.baseUrl}`); - } - }) - .catch(() => { - throw new Error(`Can't access ${this.baseUrl}`); - }); + // only create the backend if WebDAV is configured + if (this.mediaConfig.backend.use !== BackendType.WEBDAV) { + return; } + + this.config = this.mediaConfig.backend.webdav; + const url = new URL(this.config.connectionString); + this.baseUrl = url.toString(); + if (this.config.uploadDir && this.config.uploadDir !== '') { + this.baseUrl = WebdavBackend.joinURL(this.baseUrl, this.config.uploadDir); + } + this.authHeader = WebdavBackend.generateBasicAuthHeader( + url.username, + url.password, + ); + fetch(this.baseUrl, { + method: 'PROPFIND', + headers: { + Accept: 'text/plain', // eslint-disable-line @typescript-eslint/naming-convention + Authorization: this.authHeader, // eslint-disable-line @typescript-eslint/naming-convention + Depth: '0', // eslint-disable-line @typescript-eslint/naming-convention + }, + }) + .then((response) => { + if (!response.ok) { + throw new Error(`Can't access ${this.baseUrl}`); + } + }) + .catch(() => { + throw new Error(`Can't access ${this.baseUrl}`); + }); } async saveFile(