parent
85b7eb1fb8
commit
e37840d870
5 changed files with 37 additions and 29 deletions
|
|
@ -3,25 +3,27 @@ import * as Path from 'path';
|
|||
import config from '../../config';
|
||||
|
||||
export class InternalStorage {
|
||||
private static readonly path = Path.resolve(`${__dirname}/../../../files`);
|
||||
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.path}/${key}`);
|
||||
return fs.createReadStream(InternalStorage.resolvePath(key));
|
||||
}
|
||||
|
||||
public static saveFromPath(key: string, srcPath: string) {
|
||||
fs.mkdirSync(InternalStorage.path, { recursive: true });
|
||||
fs.copyFileSync(srcPath, `${InternalStorage.path}/${key}`);
|
||||
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.path}/${key}`, data);
|
||||
fs.writeFileSync(InternalStorage.resolvePath(key), data);
|
||||
return `${config.url}/files/${key}`;
|
||||
}
|
||||
|
||||
public static del(key: string) {
|
||||
fs.unlink(`${InternalStorage.path}/${key}`, () => {});
|
||||
fs.unlink(InternalStorage.resolvePath(key), () => {});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue