use async IO for InternalStorageService

This commit is contained in:
Hazelnoot 2024-10-16 11:52:22 -04:00
parent ba17776b19
commit 2deb64486b
2 changed files with 26 additions and 23 deletions

View file

@ -4,6 +4,7 @@
*/
import * as fs from 'node:fs';
import { copyFile, mkdir, unlink, writeFile } from 'node:fs/promises';
import * as Path from 'node:path';
import { fileURLToPath } from 'node:url';
import { dirname } from 'node:path';
@ -36,21 +37,21 @@ export class InternalStorageService {
}
@bindThis
public saveFromPath(key: string, srcPath: string) {
fs.mkdirSync(path, { recursive: true });
fs.copyFileSync(srcPath, this.resolvePath(key));
public async saveFromPath(key: string, srcPath: string): Promise<string> {
await mkdir(path, { recursive: true });
await copyFile(srcPath, this.resolvePath(key));
return `${this.config.url}/files/${key}`;
}
@bindThis
public saveFromBuffer(key: string, data: Buffer) {
fs.mkdirSync(path, { recursive: true });
fs.writeFileSync(this.resolvePath(key), data);
public async saveFromBuffer(key: string, data: Buffer): Promise<string> {
await mkdir(path, { recursive: true });
await writeFile(this.resolvePath(key), data);
return `${this.config.url}/files/${key}`;
}
@bindThis
public del(key: string) {
fs.unlink(this.resolvePath(key), () => {});
public async del(key: string): Promise<void> {
await unlink(this.resolvePath(key));
}
}