From dc49bfcccb6e05c97d8d7221a9cddbc1744d004d Mon Sep 17 00:00:00 2001 From: David Mehren Date: Fri, 16 Oct 2020 22:30:57 +0200 Subject: [PATCH] Implement filesystem media backend This backend stores uploaded media into files on the local filesystem. This commit also adds a `BackendType` enum, which can be used to distinguish different media backends. Signed-off-by: David Mehren --- src/media/backends/backend-type.enum.ts | 6 ++++++ src/media/backends/filesystem-backend.ts | 26 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 src/media/backends/backend-type.enum.ts create mode 100644 src/media/backends/filesystem-backend.ts diff --git a/src/media/backends/backend-type.enum.ts b/src/media/backends/backend-type.enum.ts new file mode 100644 index 000000000..60117b256 --- /dev/null +++ b/src/media/backends/backend-type.enum.ts @@ -0,0 +1,6 @@ +export enum BackendType { + FILEYSTEM = 'filesystem', + S3 = 's3', + IMGUR = 'imgur', + AZURE = 'azure', +} diff --git a/src/media/backends/filesystem-backend.ts b/src/media/backends/filesystem-backend.ts new file mode 100644 index 000000000..37cb440dd --- /dev/null +++ b/src/media/backends/filesystem-backend.ts @@ -0,0 +1,26 @@ +import { MediaBackend } from '../media-backend.interface'; +import { BackendData } from '../media-upload.entity'; +import { promises as fs } from 'fs'; +import { join } from 'path'; + +export class FilesystemBackend implements MediaBackend { + async saveFile( + buffer: Buffer, + fileName: string, + ): Promise<[string, BackendData]> { + // TODO: Get uploads directory from config + const uploadDirectory = './uploads'; + // TODO: Add server address to url + const filePath = join(uploadDirectory, fileName); + await fs.writeFile(filePath, buffer, null); + return ['/' + filePath, null]; + } + + deleteFile(fileName: string, backendData: BackendData): Promise { + return Promise.resolve(undefined); + } + + getFileURL(fileNam: string, backendData: BackendData): Promise { + return Promise.resolve(''); + } +}