2017-12-11 23:27:44 +08:00
|
|
|
const crypto = require('crypto');
|
|
|
|
const rethink = require('rethinkdbdash');
|
2017-12-10 08:34:00 +08:00
|
|
|
|
2017-12-11 23:27:44 +08:00
|
|
|
var RethinkDBStore = (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 08:34:00 +08:00
|
|
|
};
|
|
|
|
|
2017-12-11 23:27:44 +08:00
|
|
|
RethinkDBStore.md5 = (str) => {
|
|
|
|
const md5sum = crypto.createHash('md5');
|
2017-12-10 08:34:00 +08:00
|
|
|
md5sum.update(str);
|
|
|
|
return md5sum.digest('hex');
|
|
|
|
};
|
|
|
|
|
2017-12-11 23:27:44 +08:00
|
|
|
RethinkDBStore.prototype.set = (key, data, callback) => {
|
2017-12-10 08:34:00 +08:00
|
|
|
try {
|
2017-12-11 23:50:50 +08:00
|
|
|
this.client.table('uploads').insert({ id: RethinkDBStore.md5(key), data: data }).run((error) => {
|
2017-12-10 08:34:00 +08:00
|
|
|
if (error) return callback(false);
|
|
|
|
callback(true);
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
callback(false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-12-11 23:27:44 +08:00
|
|
|
RethinkDBStore.prototype.get = (key, callback) => {
|
2017-12-10 08:34:00 +08:00
|
|
|
this.client.table('uploads').get(RethinkDBStore.md5(key)).run((error, result) => {
|
|
|
|
if (error || !result) return callback(false);
|
|
|
|
callback(result.data);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = RethinkDBStore;
|