FilesystemBackend: Use scoped appConfig

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2021-01-08 12:53:16 +01:00
parent 75b6d3cc2b
commit 9f170bca4c
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
3 changed files with 29 additions and 18 deletions

View file

@ -4,10 +4,10 @@
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
import { Injectable } from '@nestjs/common'; import { Inject, Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { promises as fs } from 'fs'; import { promises as fs } from 'fs';
import { join } from 'path'; import { join } from 'path';
import applicationConfig, { AppConfig } from '../../config/app.config';
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 { BackendData } from '../media-upload.entity'; import { BackendData } from '../media-upload.entity';
@ -18,24 +18,11 @@ export class FilesystemBackend implements MediaBackend {
constructor( constructor(
private readonly logger: ConsoleLoggerService, private readonly logger: ConsoleLoggerService,
private configService: ConfigService, @Inject(applicationConfig.KEY)
private appConfig: AppConfig,
) { ) {
this.logger.setContext(FilesystemBackend.name); this.logger.setContext(FilesystemBackend.name);
this.uploadDirectory = configService.get<string>( this.uploadDirectory = appConfig.media.backend.filesystem.uploadPath;
'media.backend.filesystem.uploadPath',
);
}
private getFilePath(fileName: string): string {
return join(this.uploadDirectory, fileName);
}
private async ensureDirectory() {
try {
await fs.access(this.uploadDirectory);
} catch (e) {
await fs.mkdir(this.uploadDirectory);
}
} }
async saveFile( async saveFile(
@ -58,4 +45,16 @@ export class FilesystemBackend implements MediaBackend {
// TODO: Add server address to url // TODO: Add server address to url
return Promise.resolve('/' + filePath); return Promise.resolve('/' + filePath);
} }
private getFilePath(fileName: string): string {
return join(this.uploadDirectory, fileName);
}
private async ensureDirectory() {
try {
await fs.access(this.uploadDirectory);
} catch (e) {
await fs.mkdir(this.uploadDirectory);
}
}
} }

View file

@ -4,12 +4,14 @@
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
import { ConfigModule, registerAs } from '@nestjs/config';
import { NestExpressApplication } from '@nestjs/platform-express'; import { NestExpressApplication } from '@nestjs/platform-express';
import { Test } from '@nestjs/testing'; import { Test } from '@nestjs/testing';
import { TypeOrmModule } from '@nestjs/typeorm'; import { TypeOrmModule } from '@nestjs/typeorm';
import { promises as fs } from 'fs'; import { promises as fs } from 'fs';
import * as request from 'supertest'; import * as request from 'supertest';
import { PublicApiModule } from '../../src/api/public/public-api.module'; import { PublicApiModule } from '../../src/api/public/public-api.module';
import appConfigMock from '../../src/config/app.config.mock';
import { GroupsModule } from '../../src/groups/groups.module'; import { GroupsModule } from '../../src/groups/groups.module';
import { LoggerModule } from '../../src/logger/logger.module'; import { LoggerModule } from '../../src/logger/logger.module';
import { NestConsoleLoggerService } from '../../src/logger/nest-console-logger.service'; import { NestConsoleLoggerService } from '../../src/logger/nest-console-logger.service';
@ -27,6 +29,10 @@ describe('Notes', () => {
beforeAll(async () => { beforeAll(async () => {
const moduleRef = await Test.createTestingModule({ const moduleRef = await Test.createTestingModule({
imports: [ imports: [
ConfigModule.forRoot({
isGlobal: true,
load: [appConfigMock],
}),
PublicApiModule, PublicApiModule,
MediaModule, MediaModule,
TypeOrmModule.forRoot({ TypeOrmModule.forRoot({

View file

@ -5,10 +5,12 @@
*/ */
import { INestApplication } from '@nestjs/common'; import { INestApplication } from '@nestjs/common';
import { ConfigModule, registerAs } from '@nestjs/config';
import { Test } from '@nestjs/testing'; import { Test } from '@nestjs/testing';
import { TypeOrmModule } from '@nestjs/typeorm'; import { TypeOrmModule } from '@nestjs/typeorm';
import * as request from 'supertest'; import * as request from 'supertest';
import { PublicApiModule } from '../../src/api/public/public-api.module'; import { PublicApiModule } from '../../src/api/public/public-api.module';
import appConfigMock from '../../src/config/app.config.mock';
import { NotInDBError } from '../../src/errors/errors'; import { NotInDBError } from '../../src/errors/errors';
import { GroupsModule } from '../../src/groups/groups.module'; import { GroupsModule } from '../../src/groups/groups.module';
import { LoggerModule } from '../../src/logger/logger.module'; import { LoggerModule } from '../../src/logger/logger.module';
@ -23,6 +25,10 @@ describe('Notes', () => {
beforeAll(async () => { beforeAll(async () => {
const moduleRef = await Test.createTestingModule({ const moduleRef = await Test.createTestingModule({
imports: [ imports: [
ConfigModule.forRoot({
isGlobal: true,
load: [appConfigMock],
}),
PublicApiModule, PublicApiModule,
NotesModule, NotesModule,
PermissionsModule, PermissionsModule,