fix(media): initialize backends only if configured
Some checks are pending
Docker / build-and-push (backend) (push) Waiting to run
Docker / build-and-push (frontend) (push) Waiting to run
E2E Tests / backend-sqlite (push) Waiting to run
E2E Tests / backend-mariadb (push) Waiting to run
E2E Tests / backend-postgres (push) Waiting to run
E2E Tests / Build test build of frontend (push) Waiting to run
E2E Tests / frontend-cypress (1) (push) Blocked by required conditions
E2E Tests / frontend-cypress (2) (push) Blocked by required conditions
E2E Tests / frontend-cypress (3) (push) Blocked by required conditions
Lint and check format / Lint files and check formatting (push) Waiting to run
REUSE Compliance Check / reuse (push) Waiting to run
Scorecard supply-chain security / Scorecard analysis (push) Waiting to run
Static Analysis / Njsscan code scanning (push) Waiting to run
Static Analysis / CodeQL analysis (push) Waiting to run
Run tests & build / Test and build with NodeJS 20 (push) Waiting to run

Signed-off-by: Erik Michelson <erik@michelson.eu>
This commit is contained in:
Erik Michelson 2025-05-17 10:41:04 +00:00 committed by Erik Michelson
parent 902abf72e6
commit b771f1c1bf
5 changed files with 55 additions and 47 deletions

View file

@ -35,16 +35,17 @@ export class AzureBackend implements MediaBackend {
private mediaConfig: MediaConfig, private mediaConfig: MediaConfig,
) { ) {
this.logger.setContext(AzureBackend.name); this.logger.setContext(AzureBackend.name);
this.config = (this.mediaConfig.backend as AzureMediaConfig).azure; // only create the backend if azure is configured
if (this.mediaConfig.backend.use === BackendType.AZURE) { if (this.mediaConfig.backend.use !== BackendType.AZURE) {
// only create the client if the backend is configured to azure return;
const blobServiceClient = BlobServiceClient.fromConnectionString(
this.config.connectionString,
);
this.credential =
blobServiceClient.credential as StorageSharedKeyCredential;
this.client = blobServiceClient.getContainerClient(this.config.container);
} }
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( async saveFile(

View file

@ -8,13 +8,11 @@ import { FileTypeResult } from 'file-type';
import { promises as fs } from 'fs'; import { promises as fs } from 'fs';
import { join } from 'path'; import { join } from 'path';
import mediaConfiguration, { import mediaConfiguration, { MediaConfig } from '../../config/media.config';
FilesystemMediaConfig,
MediaConfig,
} from '../../config/media.config';
import { MediaBackendError } from '../../errors/errors'; import { MediaBackendError } from '../../errors/errors';
import { ConsoleLoggerService } from '../../logger/console-logger.service'; import { ConsoleLoggerService } from '../../logger/console-logger.service';
import { MediaBackend } from '../media-backend.interface'; import { MediaBackend } from '../media-backend.interface';
import { BackendType } from './backend-type.enum';
@Injectable() @Injectable()
export class FilesystemBackend implements MediaBackend { export class FilesystemBackend implements MediaBackend {
@ -26,9 +24,12 @@ export class FilesystemBackend implements MediaBackend {
private mediaConfig: MediaConfig, private mediaConfig: MediaConfig,
) { ) {
this.logger.setContext(FilesystemBackend.name); this.logger.setContext(FilesystemBackend.name);
this.uploadDirectory = ( // only create the backend if local filesystem is configured
this.mediaConfig.backend as FilesystemMediaConfig if (this.mediaConfig.backend.use !== BackendType.FILESYSTEM) {
).filesystem.uploadPath; this.uploadDirectory = '';
return;
}
this.uploadDirectory = this.mediaConfig.backend.filesystem.uploadPath;
} }
async saveFile( async saveFile(

View file

@ -14,6 +14,7 @@ import mediaConfiguration, {
import { MediaBackendError } from '../../errors/errors'; import { MediaBackendError } from '../../errors/errors';
import { ConsoleLoggerService } from '../../logger/console-logger.service'; import { ConsoleLoggerService } from '../../logger/console-logger.service';
import { MediaBackend } from '../media-backend.interface'; import { MediaBackend } from '../media-backend.interface';
import { BackendType } from './backend-type.enum';
type UploadResult = { type UploadResult = {
data: { data: {
@ -37,7 +38,11 @@ export class ImgurBackend implements MediaBackend {
private mediaConfig: MediaConfig, private mediaConfig: MediaConfig,
) { ) {
this.logger.setContext(ImgurBackend.name); 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<string> { async saveFile(uuid: string, buffer: Buffer): Promise<string> {

View file

@ -33,6 +33,7 @@ export class S3Backend implements MediaBackend {
private mediaConfig: MediaConfig, private mediaConfig: MediaConfig,
) { ) {
this.logger.setContext(S3Backend.name); this.logger.setContext(S3Backend.name);
// only create the backend if s3 is configured
if (this.mediaConfig.backend.use !== BackendType.S3) { if (this.mediaConfig.backend.use !== BackendType.S3) {
return; return;
} }

View file

@ -29,37 +29,37 @@ export class WebdavBackend implements MediaBackend {
private mediaConfig: MediaConfig, private mediaConfig: MediaConfig,
) { ) {
this.logger.setContext(WebdavBackend.name); this.logger.setContext(WebdavBackend.name);
if (this.mediaConfig.backend.use === BackendType.WEBDAV) { // only create the backend if WebDAV is configured
this.config = this.mediaConfig.backend.webdav; if (this.mediaConfig.backend.use !== BackendType.WEBDAV) {
const url = new URL(this.config.connectionString); return;
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}`);
});
} }
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( async saveFile(