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 <git@herrmehren.de>
This commit is contained in:
David Mehren 2020-10-16 22:30:57 +02:00
parent db15afcb88
commit dc49bfcccb
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
2 changed files with 32 additions and 0 deletions

View file

@ -0,0 +1,6 @@
export enum BackendType {
FILEYSTEM = 'filesystem',
S3 = 's3',
IMGUR = 'imgur',
AZURE = 'azure',
}

View file

@ -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<void> {
return Promise.resolve(undefined);
}
getFileURL(fileNam: string, backendData: BackendData): Promise<string> {
return Promise.resolve('');
}
}