mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-18 17:25:16 -04:00
FilesystemBackend: Ensure uploads directory exists
Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
parent
4f3ef414d4
commit
d42bc83e38
1 changed files with 19 additions and 9 deletions
|
@ -7,33 +7,43 @@ import { BackendData } from '../media-upload.entity';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class FilesystemBackend implements MediaBackend {
|
export class FilesystemBackend implements MediaBackend {
|
||||||
|
// TODO: Get uploads directory from config
|
||||||
|
uploadDirectory = './uploads';
|
||||||
|
|
||||||
constructor(private readonly logger: ConsoleLoggerService) {
|
constructor(private readonly logger: ConsoleLoggerService) {
|
||||||
this.logger.setContext(FilesystemBackend.name);
|
this.logger.setContext(FilesystemBackend.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private getFilePath(fileName: string): string {
|
||||||
|
return join(this.uploadDirectory, fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async ensureDirectory() {
|
||||||
|
try {
|
||||||
|
await fs.access(this.uploadDirectory);
|
||||||
|
} catch (e) {
|
||||||
|
await fs.mkdir(this.uploadDirectory);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async saveFile(
|
async saveFile(
|
||||||
buffer: Buffer,
|
buffer: Buffer,
|
||||||
fileName: string,
|
fileName: string,
|
||||||
): Promise<[string, BackendData]> {
|
): Promise<[string, BackendData]> {
|
||||||
const filePath = FilesystemBackend.getFilePath(fileName);
|
const filePath = this.getFilePath(fileName);
|
||||||
this.logger.debug(`Writing file to: ${filePath}`, 'saveFile');
|
this.logger.debug(`Writing file to: ${filePath}`, 'saveFile');
|
||||||
|
await this.ensureDirectory();
|
||||||
await fs.writeFile(filePath, buffer, null);
|
await fs.writeFile(filePath, buffer, null);
|
||||||
return ['/' + filePath, null];
|
return ['/' + filePath, null];
|
||||||
}
|
}
|
||||||
|
|
||||||
async deleteFile(fileName: string, _: BackendData): Promise<void> {
|
async deleteFile(fileName: string, _: BackendData): Promise<void> {
|
||||||
return fs.unlink(FilesystemBackend.getFilePath(fileName));
|
return fs.unlink(this.getFilePath(fileName));
|
||||||
}
|
}
|
||||||
|
|
||||||
getFileURL(fileName: string, _: BackendData): Promise<string> {
|
getFileURL(fileName: string, _: BackendData): Promise<string> {
|
||||||
const filePath = FilesystemBackend.getFilePath(fileName);
|
const filePath = this.getFilePath(fileName);
|
||||||
// TODO: Add server address to url
|
// TODO: Add server address to url
|
||||||
return Promise.resolve('/' + filePath);
|
return Promise.resolve('/' + filePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static getFilePath(fileName: string): string {
|
|
||||||
// TODO: Get uploads directory from config
|
|
||||||
const uploadDirectory = './uploads';
|
|
||||||
return join(uploadDirectory, fileName);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue