From 60dc5cebc3b3144c25c605ed4566587297d466a0 Mon Sep 17 00:00:00 2001 From: stekrause Date: Mon, 19 Jun 2023 18:14:35 +0200 Subject: [PATCH] add s3Client and s3ClientAdapter to module and config --- .../h5p-editor/h5p-editor-test.module.ts | 12 +++++- .../modules/h5p-editor/h5p-editor.config.ts | 9 +++++ .../modules/h5p-editor/h5p-editor.module.ts | 39 ++++++++++++++++--- .../modules/h5p-editor/interface/config.ts | 7 ++++ 4 files changed, 59 insertions(+), 8 deletions(-) create mode 100644 apps/server/src/modules/h5p-editor/interface/config.ts diff --git a/apps/server/src/modules/h5p-editor/h5p-editor-test.module.ts b/apps/server/src/modules/h5p-editor/h5p-editor-test.module.ts index 0f9607c95c3..447aae7ede8 100644 --- a/apps/server/src/modules/h5p-editor/h5p-editor-test.module.ts +++ b/apps/server/src/modules/h5p-editor/h5p-editor-test.module.ts @@ -19,9 +19,10 @@ import { LibraryStorage } from './libraryStorage/libraryStorage'; import { H5PAjaxEndpointService, H5PEditorService, H5PPlayerService } from './service'; import { TemporaryFileStorage } from './temporary-file-storage/temporary-file-storage'; import { H5PEditorUc } from './uc/h5p.uc'; +import { S3ClientAdapter } from '../files-storage/client/s3-client.adapter'; const storages = [ - { provide: ContentStorage, useValue: new ContentStorage(path.join(os.tmpdir(), '/h5p_content')) }, + ContentStorage, { provide: LibraryStorage, useValue: new LibraryStorage(path.join(os.tmpdir(), '/h5p_libraries')) }, { provide: TemporaryFileStorage, useValue: new TemporaryFileStorage(path.join(os.tmpdir(), '/h5p_temporary')) }, ]; @@ -37,7 +38,14 @@ const imports = [ RabbitMQWrapperTestModule, ]; const controllers = [H5PEditorController]; -const providers = [H5PEditorUc, H5PPlayerService, H5PEditorService, H5PAjaxEndpointService, ...storages]; +const providers = [ + H5PEditorUc, + H5PPlayerService, + H5PEditorService, + H5PAjaxEndpointService, + S3ClientAdapter, + ...storages, +]; @Module({ imports, diff --git a/apps/server/src/modules/h5p-editor/h5p-editor.config.ts b/apps/server/src/modules/h5p-editor/h5p-editor.config.ts index a5b667897b3..a1dd9e74200 100644 --- a/apps/server/src/modules/h5p-editor/h5p-editor.config.ts +++ b/apps/server/src/modules/h5p-editor/h5p-editor.config.ts @@ -1,8 +1,17 @@ import { Configuration } from '@hpi-schul-cloud/commons'; +import { S3Config } from './interface/config'; const h5pEditorConfig = { NEST_LOG_LEVEL: Configuration.get('NEST_LOG_LEVEL') as string, INCOMING_REQUEST_TIMEOUT: Configuration.get('INCOMING_REQUEST_TIMEOUT_API') as number, }; +export const s3Config: S3Config = { + endpoint: Configuration.get('H5P_EDITOR__S3_ENDPOINT') as string, + region: Configuration.get('H5P_EDITOR__S3_REGION') as string, + bucket: Configuration.get('H5P_EDITOR__S3_BUCKET') as string, + accessKeyId: Configuration.get('H5P_EDITOR__S3_ACCESS_KEY') as string, + secretAccessKey: Configuration.get('H5P_EDITOR__S3_SECRET_KEY') as string, +}; + export const config = () => h5pEditorConfig; diff --git a/apps/server/src/modules/h5p-editor/h5p-editor.module.ts b/apps/server/src/modules/h5p-editor/h5p-editor.module.ts index 6427af36e19..672129973d4 100644 --- a/apps/server/src/modules/h5p-editor/h5p-editor.module.ts +++ b/apps/server/src/modules/h5p-editor/h5p-editor.module.ts @@ -3,7 +3,7 @@ import path from 'node:path'; import { Dictionary, IPrimaryKey } from '@mikro-orm/core'; import { MikroOrmModule, MikroOrmModuleSyncOptions } from '@mikro-orm/nestjs'; -import { Module, NotFoundException } from '@nestjs/common'; +import { Module, NotFoundException, Scope } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; import { Account, Role, School, SchoolYear, System, User } from '@shared/domain'; import { RabbitMQWrapperModule } from '@shared/infra/rabbitmq'; @@ -12,14 +12,14 @@ import { CoreModule } from '@src/core'; import { Logger } from '@src/core/logger'; import { AuthenticationModule } from '@src/modules/authentication/authentication.module'; import { AuthorizationModule } from '@src/modules/authorization'; - +import { S3Client } from '@aws-sdk/client-s3'; import { H5PEditorController } from './controller/h5p-editor.controller'; -import { config } from './h5p-editor.config'; +import { config, s3Config } from './h5p-editor.config'; +import { S3Config } from './interface/config'; import { H5PAjaxEndpointService } from './service'; import { H5PEditorService } from './service/h5p-editor.service'; import { H5PPlayerService } from './service/h5p-player.service'; import { H5PEditorUc } from './uc/h5p.uc'; - import { ContentStorage } from './contentStorage/contentStorage'; import { LibraryStorage } from './libraryStorage/libraryStorage'; import { TemporaryFileStorage } from './temporary-file-storage/temporary-file-storage'; @@ -31,7 +31,7 @@ const defaultMikroOrmOptions: MikroOrmModuleSyncOptions = { }; const storages = [ - { provide: ContentStorage, useValue: new ContentStorage(path.join(os.tmpdir(), '/h5p_content')) }, + ContentStorage, { provide: LibraryStorage, useValue: new LibraryStorage(path.join(os.tmpdir(), '/h5p_libraries')) }, { provide: TemporaryFileStorage, useValue: new TemporaryFileStorage(path.join(os.tmpdir(), '/h5p_temporary')) }, ]; @@ -57,7 +57,34 @@ const imports = [ const controllers = [H5PEditorController]; -const providers = [Logger, H5PEditorUc, H5PEditorService, H5PPlayerService, H5PAjaxEndpointService, ...storages]; +const providers = [ + { + provide: 'S3_Client', + scope: Scope.REQUEST, + useFactory: (configProvider: S3Config) => + new S3Client({ + region: configProvider.region, + credentials: { + accessKeyId: configProvider.accessKeyId, + secretAccessKey: configProvider.secretAccessKey, + }, + endpoint: configProvider.endpoint, + forcePathStyle: true, + tls: true, + }), + inject: ['S3_Config'], + }, + { + provide: 'S3_Config', + useValue: s3Config, + }, + Logger, + H5PEditorUc, + H5PEditorService, + H5PPlayerService, + H5PAjaxEndpointService, + ...storages, +]; @Module({ imports, diff --git a/apps/server/src/modules/h5p-editor/interface/config.ts b/apps/server/src/modules/h5p-editor/interface/config.ts new file mode 100644 index 00000000000..93577407903 --- /dev/null +++ b/apps/server/src/modules/h5p-editor/interface/config.ts @@ -0,0 +1,7 @@ +export interface S3Config { + endpoint: string; + region: string; + bucket: string; + accessKeyId: string; + secretAccessKey: string; +}