mirror of
https://github.com/SunRed/haste-server.git
synced 2024-11-01 01:30:21 +01:00
Fixed bug in RethinkDB document store and use classes
This commit is contained in:
parent
7f625e22f7
commit
dc0f151a7f
1 changed files with 38 additions and 32 deletions
|
@ -1,39 +1,45 @@
|
|||
const crypto = require('crypto');
|
||||
const rethink = require('rethinkdbdash');
|
||||
const winston = require('winston');
|
||||
|
||||
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 || ''
|
||||
});
|
||||
};
|
||||
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 || ''
|
||||
});
|
||||
}
|
||||
|
||||
RethinkDBStore.md5 = (str) => {
|
||||
set(key, data, callback) {
|
||||
this.client.table('pastes').insert({ id: RethinkDBStore.md5(key), data: data }).run((error) => {
|
||||
if (error) {
|
||||
callback(false);
|
||||
winston.error('failed to insert to table', error);
|
||||
return;
|
||||
}
|
||||
callback(true);
|
||||
});
|
||||
}
|
||||
|
||||
get(key, callback) {
|
||||
this.client.table('pastes').get(RethinkDBStore.md5(key)).run((error, result) => {
|
||||
if (error || !result) {
|
||||
callback(false);
|
||||
winston.error('failed to insert to table', error);
|
||||
return;
|
||||
}
|
||||
callback(result.data);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = RethinkDBStore;
|
||||
module.exports.md5 = (str) => {
|
||||
const md5sum = crypto.createHash('md5');
|
||||
md5sum.update(str);
|
||||
return md5sum.digest('hex');
|
||||
};
|
||||
|
||||
RethinkDBStore.prototype.set = (key, data, callback) => {
|
||||
try {
|
||||
this.client.table('uploads').insert({ id: RethinkDBStore.md5(key), data: data }).run((error) => {
|
||||
if (error) return callback(false);
|
||||
callback(true);
|
||||
});
|
||||
} catch (err) {
|
||||
callback(false);
|
||||
}
|
||||
};
|
||||
|
||||
RethinkDBStore.prototype.get = (key, callback) => {
|
||||
this.client.table('uploads').get(RethinkDBStore.md5(key)).run((error, result) => {
|
||||
if (error || !result) return callback(false);
|
||||
callback(result.data);
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = RethinkDBStore;
|
||||
};
|
Loading…
Reference in a new issue