implemented download tokens

This commit is contained in:
Danny Coates
2020-07-27 11:18:52 -07:00
parent 87d46f7ef5
commit 81e9d81dab
26 changed files with 271 additions and 126 deletions

View File

@@ -1,10 +1,8 @@
const fs = require('fs');
const fss = require('fs');
const fs = fss.promises;
const path = require('path');
const promisify = require('util').promisify;
const mkdirp = require('mkdirp');
const stat = promisify(fs.stat);
class FSStorage {
constructor(config, log) {
this.log = log;
@@ -13,32 +11,36 @@ class FSStorage {
}
async length(id) {
const result = await stat(path.join(this.dir, id));
const result = await fs.stat(path.join(this.dir, id));
return result.size;
}
getStream(id) {
return fs.createReadStream(path.join(this.dir, id));
return fss.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);
const fstream = fss.createWriteStream(filepath);
file.pipe(fstream);
file.on('error', err => {
fstream.destroy(err);
});
fstream.on('error', err => {
fs.unlinkSync(filepath);
this.del(id);
reject(err);
});
fstream.on('finish', resolve);
});
}
del(id) {
return Promise.resolve(fs.unlinkSync(path.join(this.dir, id)));
async del(id) {
try {
await fs.unlink(path.join(this.dir, id));
} catch (e) {
// ignore local fs issues
}
}
ping() {

View File

@@ -56,7 +56,8 @@ class DB {
if (info.dead || info.flagged) {
throw new Error(info.flagged ? 'flagged' : 'dead');
}
return this.storage.getStream(info.filePath);
const length = await this.storage.length(info.filePath);
return { length, stream: this.storage.getStream(info.filePath) };
}
async set(id, file, meta, expireSeconds = config.default_expire_seconds) {
@@ -75,15 +76,15 @@ class DB {
this.redis.hset(id, key, value);
}
incrementField(id, key, increment = 1) {
this.redis.hincrby(id, key, increment);
async incrementField(id, key, increment = 1) {
return await this.redis.hincrbyAsync(id, key, increment);
}
async kill(id) {
const { filePath, dead } = await this.getPrefixedInfo(id);
if (!dead) {
this.storage.del(filePath);
this.redis.hset(id, 'dead', 1);
this.storage.del(filePath);
}
}
@@ -94,8 +95,8 @@ class DB {
async del(id) {
const { filePath } = await this.getPrefixedInfo(id);
this.storage.del(filePath);
this.redis.del(id);
this.storage.del(filePath);
}
async ping() {
@@ -105,7 +106,7 @@ class DB {
async metadata(id) {
const result = await this.redis.hgetallAsync(id);
return result && new Metadata(result);
return result && new Metadata({ id, ...result }, this);
}
}

View File

@@ -2,7 +2,7 @@ const promisify = require('util').promisify;
module.exports = function(config) {
const redis_lib =
config.env === 'development' && config.redis_host === 'localhost'
config.env === 'development' && config.redis_host === 'mock'
? 'redis-mock'
: 'redis';
@@ -23,6 +23,7 @@ module.exports = function(config) {
client.ttlAsync = promisify(client.ttl);
client.hgetallAsync = promisify(client.hgetall);
client.hgetAsync = promisify(client.hget);
client.hincrbyAsync = promisify(client.hincrby);
client.hmgetAsync = promisify(client.hmget);
client.pingAsync = promisify(client.ping);
client.existsAsync = promisify(client.exists);