28 lines
971 B
JavaScript
28 lines
971 B
JavaScript
import * as fs from "node:fs";
|
|
import * as Path from "node:path";
|
|
import { pipeline } from "node:stream/promises";
|
|
import config from "../../config/index.js";
|
|
export class InternalStorage {
|
|
static resolvePath = (key)=>Path.resolve(config.mediaDir, key);
|
|
static read(key) {
|
|
return fs.createReadStream(InternalStorage.resolvePath(key));
|
|
}
|
|
static async saveFromPath(key, srcPath) {
|
|
fs.mkdirSync(config.mediaDir, {
|
|
recursive: true
|
|
});
|
|
await pipeline(fs.createReadStream(srcPath), fs.createWriteStream(InternalStorage.resolvePath(key)));
|
|
return `${config.url}/files/${key}`;
|
|
}
|
|
static saveFromBuffer(key, data) {
|
|
fs.mkdirSync(config.mediaDir, {
|
|
recursive: true
|
|
});
|
|
fs.writeFileSync(InternalStorage.resolvePath(key), data);
|
|
return `${config.url}/files/${key}`;
|
|
}
|
|
static del(key) {
|
|
fs.unlink(InternalStorage.resolvePath(key), ()=>{});
|
|
}
|
|
}
|