refactor: introduce bindThis decorator to bind this automaticaly

This commit is contained in:
syuilo
2022-12-04 15:03:09 +09:00
parent e73581f715
commit bbb49457f9
199 changed files with 969 additions and 96 deletions

View File

@@ -10,6 +10,7 @@ const _filename = fileURLToPath(import.meta.url);
const _dirname = dirname(_filename);
const path = Path.resolve(_dirname, '../../../../files');
import { bindThis } from '@/decorators.js';
@Injectable()
export class InternalStorageService {
@@ -19,26 +20,31 @@ export class InternalStorageService {
) {
}
@bindThis
public resolvePath(key: string) {
return Path.resolve(path, key);
}
@bindThis
public read(key: string) {
return fs.createReadStream(this.resolvePath(key));
}
@bindThis
public saveFromPath(key: string, srcPath: string) {
fs.mkdirSync(path, { recursive: true });
fs.copyFileSync(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);
return `${this.config.url}/files/${key}`;
}
@bindThis
public del(key: string) {
fs.unlink(this.resolvePath(key), () => {});
}