2017-12-11 16:27:44 +01:00
|
|
|
const crypto = require('crypto');
|
|
|
|
const rethink = require('rethinkdbdash');
|
2018-04-16 06:16:08 +02:00
|
|
|
const winston = require('winston');
|
2017-12-10 01:34:00 +01:00
|
|
|
|
2018-04-16 06:16:08 +02:00
|
|
|
class RethinkDBStore {
|
|
|
|
constructor(options) {
|
|
|
|
this.client = rethink({
|
|
|
|
silent: true,
|
|
|
|
host: options.host || '127.0.0.1',
|
|
|
|
port: options.port || 28015,
|
|
|
|
db: options.db || 'haste',
|
|
|
|
user: options.user || 'admin',
|
|
|
|
password: options.password || ''
|
|
|
|
});
|
|
|
|
}
|
2017-12-10 01:34:00 +01:00
|
|
|
|
2018-04-16 06:16:08 +02:00
|
|
|
set(key, data, callback) {
|
2018-04-16 06:16:39 +02:00
|
|
|
this.client.table('uploads').insert({ id: RethinkDBStore.md5(key), data: data }).run((error) => {
|
2018-04-16 06:16:08 +02:00
|
|
|
if (error) {
|
|
|
|
callback(false);
|
|
|
|
winston.error('failed to insert to table', error);
|
|
|
|
return;
|
|
|
|
}
|
2017-12-10 01:34:00 +01:00
|
|
|
callback(true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-16 06:16:08 +02:00
|
|
|
get(key, callback) {
|
2018-04-16 06:16:39 +02:00
|
|
|
this.client.table('uploads').get(RethinkDBStore.md5(key)).run((error, result) => {
|
2018-04-16 06:16:08 +02:00
|
|
|
if (error || !result) {
|
|
|
|
callback(false);
|
|
|
|
winston.error('failed to insert to table', error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
callback(result.data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-12-10 01:34:00 +01:00
|
|
|
|
|
|
|
module.exports = RethinkDBStore;
|
2018-04-16 06:16:08 +02:00
|
|
|
module.exports.md5 = (str) => {
|
|
|
|
const md5sum = crypto.createHash('md5');
|
|
|
|
md5sum.update(str);
|
|
|
|
return md5sum.digest('hex');
|
|
|
|
};
|