mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-22 17:56:30 -05:00
FilesystemBackend: Throw MediaBackendError where appropriate
Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
parent
8515d824b7
commit
8e7be737fa
1 changed files with 26 additions and 4 deletions
|
@ -12,6 +12,7 @@ import { ConsoleLoggerService } from '../../logger/console-logger.service';
|
|||
import { MediaBackend } from '../media-backend.interface';
|
||||
import { BackendData } from '../media-upload.entity';
|
||||
import { MediaConfig } from '../../config/media.config';
|
||||
import { MediaBackendError } from '../../errors/errors';
|
||||
|
||||
@Injectable()
|
||||
export class FilesystemBackend implements MediaBackend {
|
||||
|
@ -33,12 +34,23 @@ export class FilesystemBackend implements MediaBackend {
|
|||
const filePath = this.getFilePath(fileName);
|
||||
this.logger.debug(`Writing file to: ${filePath}`, 'saveFile');
|
||||
await this.ensureDirectory();
|
||||
await fs.writeFile(filePath, buffer, null);
|
||||
return ['/' + filePath, null];
|
||||
try {
|
||||
await fs.writeFile(filePath, buffer, null);
|
||||
return ['/' + filePath, null];
|
||||
} catch (e) {
|
||||
this.logger.error(e.message, e.stack, 'saveFile');
|
||||
throw new MediaBackendError(`Could not save '${filePath}'`);
|
||||
}
|
||||
}
|
||||
|
||||
async deleteFile(fileName: string, _: BackendData): Promise<void> {
|
||||
return fs.unlink(this.getFilePath(fileName));
|
||||
const filePath = this.getFilePath(fileName);
|
||||
try {
|
||||
return fs.unlink(filePath);
|
||||
} catch (e) {
|
||||
this.logger.error(e.message, e.stack, 'deleteFile');
|
||||
throw new MediaBackendError(`Could not delete '${filePath}'`);
|
||||
}
|
||||
}
|
||||
|
||||
getFileURL(fileName: string, _: BackendData): Promise<string> {
|
||||
|
@ -55,7 +67,17 @@ export class FilesystemBackend implements MediaBackend {
|
|||
try {
|
||||
await fs.access(this.uploadDirectory);
|
||||
} catch (e) {
|
||||
await fs.mkdir(this.uploadDirectory);
|
||||
try {
|
||||
this.logger.debug(
|
||||
`The directory '${this.uploadDirectory}' can't be accessed. Trying to create the directory`,
|
||||
);
|
||||
await fs.mkdir(this.uploadDirectory);
|
||||
} catch (e) {
|
||||
this.logger.error(e.message, e.stack, 'deleteFile');
|
||||
throw new MediaBackendError(
|
||||
`Could not create '${this.uploadDirectory}'`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue