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

81 lines
1.9 KiB
JavaScript
Raw Normal View History

2018-02-06 23:31:18 +01:00
const config = require('../config');
const Metadata = require('../metadata');
const mozlog = require('../log');
const createRedisClient = require('./redis');
function getPrefix(seconds) {
return Math.max(Math.floor(seconds / 86400), 1);
}
2018-02-06 23:31:18 +01:00
class DB {
constructor(config) {
2018-10-05 20:01:58 +02:00
let Storage = null;
if (config.s3_bucket) {
Storage = require('./s3');
} else if (config.gcs_bucket) {
Storage = require('./gcs');
} else {
Storage = require('./fs');
}
2018-02-06 23:31:18 +01:00
this.log = mozlog('send.storage');
2018-08-08 20:07:09 +02:00
2018-08-09 23:49:52 +02:00
this.storage = new Storage(config, this.log);
2018-08-08 20:07:09 +02:00
2018-02-06 23:31:18 +01:00
this.redis = createRedisClient(config);
this.redis.on('error', err => {
this.log.error('Redis:', err);
});
}
async ttl(id) {
const result = await this.redis.ttlAsync(id);
return Math.ceil(result) * 1000;
}
2018-08-09 23:49:52 +02:00
async getPrefixedId(id) {
const prefix = await this.redis.hgetAsync(id, 'prefix');
return `${prefix}-${id}`;
2018-02-06 23:31:18 +01:00
}
2018-08-08 20:07:09 +02:00
async length(id) {
2018-08-09 23:49:52 +02:00
const filePath = await this.getPrefixedId(id);
return this.storage.length(filePath);
2018-02-06 23:31:18 +01:00
}
2018-08-08 20:07:09 +02:00
async get(id) {
2018-08-09 23:49:52 +02:00
const filePath = await this.getPrefixedId(id);
return this.storage.getStream(filePath);
2018-08-08 20:07:09 +02:00
}
async set(id, file, meta, expireSeconds = config.default_expire_seconds) {
const prefix = getPrefix(expireSeconds);
2018-08-09 23:49:52 +02:00
const filePath = `${prefix}-${id}`;
await this.storage.set(filePath, file);
this.redis.hset(id, 'prefix', prefix);
2018-02-06 23:31:18 +01:00
this.redis.hmset(id, meta);
2018-08-08 20:07:09 +02:00
this.redis.expire(id, expireSeconds);
2018-02-06 23:31:18 +01:00
}
setField(id, key, value) {
this.redis.hset(id, key, value);
}
2018-08-08 20:07:09 +02:00
async del(id) {
2018-08-09 23:49:52 +02:00
const filePath = await this.getPrefixedId(id);
this.storage.del(filePath);
2018-02-06 23:31:18 +01:00
this.redis.del(id);
}
async ping() {
await this.redis.pingAsync();
2018-08-09 23:49:52 +02:00
await this.storage.ping();
2018-02-06 23:31:18 +01:00
}
async metadata(id) {
const result = await this.redis.hgetallAsync(id);
return result && new Metadata(result);
}
}
module.exports = new DB(config);