paricafe/src/services/drive/internal-storage.ts
tamaina e37840d870 ドライブ関連の修正 (#5673)
* ✌️

* Update add-file.ts

* fix
2020-01-04 07:20:41 +09:00

29 lines
931 B
TypeScript

import * as fs from 'fs';
import * as Path from 'path';
import config from '../../config';
export class InternalStorage {
private static readonly path = Path.resolve(__dirname, '../../../files');
public static resolvePath = (key: string) => Path.resolve(InternalStorage.path, key);
public static read(key: string) {
return fs.createReadStream(InternalStorage.resolvePath(key));
}
public static saveFromPath(key: string, srcPath: string) {
fs.mkdirSync(InternalStorage.path, { recursive: true });
fs.copyFileSync(srcPath, InternalStorage.resolvePath(key));
return `${config.url}/files/${key}`;
}
public static saveFromBuffer(key: string, data: Buffer) {
fs.mkdirSync(InternalStorage.path, { recursive: true });
fs.writeFileSync(InternalStorage.resolvePath(key), data);
return `${config.url}/files/${key}`;
}
public static del(key: string) {
fs.unlink(InternalStorage.resolvePath(key), () => {});
}
}