parent
7f625e22f7
commit
dc0f151a7f
1 changed files with 35 additions and 29 deletions
@ -1,39 +1,45 @@ |
|||||||
const crypto = require('crypto'); |
const crypto = require('crypto'); |
||||||
const rethink = require('rethinkdbdash'); |
const rethink = require('rethinkdbdash'); |
||||||
|
const winston = require('winston'); |
||||||
|
|
||||||
var RethinkDBStore = (options) => { |
class RethinkDBStore { |
||||||
this.client = rethink({ |
constructor(options) { |
||||||
silent: true, |
this.client = rethink({ |
||||||
host: options.host || '127.0.0.1', |
silent: true, |
||||||
port: options.port || 28015, |
host: options.host || '127.0.0.1', |
||||||
db: options.db || 'haste', |
port: options.port || 28015, |
||||||
user: options.user || 'admin', |
db: options.db || 'haste', |
||||||
password: options.password || '' |
user: options.user || 'admin', |
||||||
}); |
password: options.password || '' |
||||||
}; |
}); |
||||||
|
} |
||||||
RethinkDBStore.md5 = (str) => { |
|
||||||
const md5sum = crypto.createHash('md5'); |
|
||||||
md5sum.update(str); |
|
||||||
return md5sum.digest('hex'); |
|
||||||
}; |
|
||||||
|
|
||||||
RethinkDBStore.prototype.set = (key, data, callback) => { |
set(key, data, callback) { |
||||||
try { |
this.client.table('pastes').insert({ id: RethinkDBStore.md5(key), data: data }).run((error) => { |
||||||
this.client.table('uploads').insert({ id: RethinkDBStore.md5(key), data: data }).run((error) => { |
if (error) { |
||||||
if (error) return callback(false); |
callback(false); |
||||||
|
winston.error('failed to insert to table', error); |
||||||
|
return; |
||||||
|
} |
||||||
callback(true); |
callback(true); |
||||||
}); |
}); |
||||||
} catch (err) { |
|
||||||
callback(false); |
|
||||||
} |
} |
||||||
}; |
|
||||||
|
|
||||||
RethinkDBStore.prototype.get = (key, callback) => { |
get(key, callback) { |
||||||
this.client.table('uploads').get(RethinkDBStore.md5(key)).run((error, result) => { |
this.client.table('pastes').get(RethinkDBStore.md5(key)).run((error, result) => { |
||||||
if (error || !result) return callback(false); |
if (error || !result) { |
||||||
callback(result.data); |
callback(false); |
||||||
}); |
winston.error('failed to insert to table', error); |
||||||
}; |
return; |
||||||
|
} |
||||||
|
callback(result.data); |
||||||
|
}); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
module.exports = RethinkDBStore; |
module.exports = RethinkDBStore; |
||||||
|
module.exports.md5 = (str) => { |
||||||
|
const md5sum = crypto.createHash('md5'); |
||||||
|
md5sum.update(str); |
||||||
|
return md5sum.digest('hex'); |
||||||
|
}; |
Loading…
Reference in new issue