drop.chapril.org-firefoxsend/server/storage/fs.js

51 lines
1.0 KiB
JavaScript
Raw Normal View History

2018-02-06 23:31:18 +01:00
const fs = require('fs');
const path = require('path');
const promisify = require('util').promisify;
const stat = promisify(fs.stat);
class FSStorage {
constructor(config, log) {
this.log = log;
this.dir = config.file_dir;
2022-07-26 19:23:19 +02:00
fs.mkdirSync(this.dir, {
recursive: true
});
2018-02-06 23:31:18 +01:00
}
async length(id) {
const result = await stat(path.join(this.dir, id));
return result.size;
}
getStream(id) {
return fs.createReadStream(path.join(this.dir, id));
}
set(id, file) {
return new Promise((resolve, reject) => {
const filepath = path.join(this.dir, id);
const fstream = fs.createWriteStream(filepath);
file.pipe(fstream);
file.on('error', err => {
fstream.destroy(err);
2018-02-06 23:31:18 +01:00
});
fstream.on('error', err => {
fs.unlinkSync(filepath);
reject(err);
});
fstream.on('finish', resolve);
});
}
del(id) {
return Promise.resolve(fs.unlinkSync(path.join(this.dir, id)));
}
ping() {
return Promise.resolve();
}
}
module.exports = FSStorage;