mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-23 18:26:32 -05:00
FilesystemBackend: Implement deleteFile
and getFileURL
.
We use `fs.unlink` instead of `fs.rm`, as the latter is only available in the fsPromises API since Node 14.14 Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
parent
9743018591
commit
3da16baeae
3 changed files with 35 additions and 13 deletions
|
@ -1,26 +1,39 @@
|
||||||
import { MediaBackend } from '../media-backend.interface';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { BackendData } from '../media-upload.entity';
|
|
||||||
import { promises as fs } from 'fs';
|
import { promises as fs } from 'fs';
|
||||||
import { join } from 'path';
|
import { join } from 'path';
|
||||||
|
import { ConsoleLoggerService } from '../../logger/console-logger.service';
|
||||||
|
import { MediaBackend } from '../media-backend.interface';
|
||||||
|
import { BackendData } from '../media-upload.entity';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
export class FilesystemBackend implements MediaBackend {
|
export class FilesystemBackend implements MediaBackend {
|
||||||
|
constructor(private readonly logger: ConsoleLoggerService) {
|
||||||
|
this.logger.setContext(FilesystemBackend.name);
|
||||||
|
}
|
||||||
|
|
||||||
async saveFile(
|
async saveFile(
|
||||||
buffer: Buffer,
|
buffer: Buffer,
|
||||||
fileName: string,
|
fileName: string,
|
||||||
): Promise<[string, BackendData]> {
|
): Promise<[string, BackendData]> {
|
||||||
// TODO: Get uploads directory from config
|
const filePath = FilesystemBackend.getFilePath(fileName);
|
||||||
const uploadDirectory = './uploads';
|
this.logger.debug(`Writing file to: ${filePath}`, 'saveFile');
|
||||||
// TODO: Add server address to url
|
|
||||||
const filePath = join(uploadDirectory, fileName);
|
|
||||||
await fs.writeFile(filePath, buffer, null);
|
await fs.writeFile(filePath, buffer, null);
|
||||||
return ['/' + filePath, null];
|
return ['/' + filePath, null];
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteFile(fileName: string, backendData: BackendData): Promise<void> {
|
async deleteFile(fileName: string, backendData: BackendData): Promise<void> {
|
||||||
return Promise.resolve(undefined);
|
return fs.unlink(FilesystemBackend.getFilePath(fileName));
|
||||||
}
|
}
|
||||||
|
|
||||||
getFileURL(fileNam: string, backendData: BackendData): Promise<string> {
|
getFileURL(fileName: string, backendData: BackendData): Promise<string> {
|
||||||
return Promise.resolve('');
|
const filePath = FilesystemBackend.getFilePath(fileName);
|
||||||
|
// TODO: Add server address to url
|
||||||
|
return Promise.resolve('/' + filePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static getFilePath(fileName: string): string {
|
||||||
|
// TODO: Get uploads directory from config
|
||||||
|
const uploadDirectory = './uploads';
|
||||||
|
return join(uploadDirectory, fileName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,20 @@
|
||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||||
|
import { LoggerModule } from '../logger/logger.module';
|
||||||
import { NotesModule } from '../notes/notes.module';
|
import { NotesModule } from '../notes/notes.module';
|
||||||
import { UsersModule } from '../users/users.module';
|
import { UsersModule } from '../users/users.module';
|
||||||
|
import { FilesystemBackend } from './backends/filesystem-backend';
|
||||||
import { MediaUpload } from './media-upload.entity';
|
import { MediaUpload } from './media-upload.entity';
|
||||||
import { MediaService } from './media.service';
|
import { MediaService } from './media.service';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [TypeOrmModule.forFeature([MediaUpload]), NotesModule, UsersModule],
|
imports: [
|
||||||
providers: [MediaService],
|
TypeOrmModule.forFeature([MediaUpload]),
|
||||||
|
NotesModule,
|
||||||
|
UsersModule,
|
||||||
|
LoggerModule,
|
||||||
|
],
|
||||||
|
providers: [MediaService, FilesystemBackend],
|
||||||
exports: [MediaService],
|
exports: [MediaService],
|
||||||
})
|
})
|
||||||
export class MediaModule {}
|
export class MediaModule {}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { ModuleRef } from '@nestjs/core';
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
import * as FileType from 'file-type';
|
import * as FileType from 'file-type';
|
||||||
import { Repository } from 'typeorm';
|
import { Repository } from 'typeorm';
|
||||||
|
@ -16,6 +17,7 @@ export class MediaService {
|
||||||
private mediaUploadRepository: Repository<MediaUpload>,
|
private mediaUploadRepository: Repository<MediaUpload>,
|
||||||
private notesService: NotesService,
|
private notesService: NotesService,
|
||||||
private usersService: UsersService,
|
private usersService: UsersService,
|
||||||
|
private moduleRef: ModuleRef,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public async saveFile(file: MulterFile, username: string, noteId: string) {
|
public async saveFile(file: MulterFile, username: string, noteId: string) {
|
||||||
|
@ -35,7 +37,7 @@ export class MediaService {
|
||||||
fileTypeResult.ext,
|
fileTypeResult.ext,
|
||||||
BackendType.FILEYSTEM,
|
BackendType.FILEYSTEM,
|
||||||
);
|
);
|
||||||
const backend = new FilesystemBackend();
|
const backend = this.moduleRef.get(FilesystemBackend);
|
||||||
const [url, backendData] = await backend.saveFile(
|
const [url, backendData] = await backend.saveFile(
|
||||||
file.buffer,
|
file.buffer,
|
||||||
mediaUpload.id,
|
mediaUpload.id,
|
||||||
|
|
Loading…
Reference in a new issue