1
0
Fork 0
mirror of https://github.com/seejohnrun/haste-server.git synced 2024-11-01 03:21:21 +00:00

Merge pull request #2 from seejohnrun/master

update from source
This commit is contained in:
Yuan Gao 2018-08-27 22:58:29 +01:00 committed by GitHub
commit 56b939124e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 26 deletions

View file

@ -36,7 +36,7 @@ DocumentHandler.prototype.handleRawGet = function(key, response, skipExpire) {
this.store.get(key, function(ret) { this.store.get(key, function(ret) {
if (ret) { if (ret) {
winston.verbose('retrieved raw document', { key: key }); winston.verbose('retrieved raw document', { key: key });
response.writeHead(200, { 'content-type': 'text/plain' }); response.writeHead(200, { 'content-type': 'text/plain; charset=UTF-8' });
response.end(ret); response.end(ret);
} }
else { else {

View file

@ -1,7 +1,15 @@
const crypto = require('crypto'); const crypto = require('crypto');
const rethink = require('rethinkdbdash'); const rethink = require('rethinkdbdash');
const winston = require('winston');
var RethinkDBStore = (options) => { const md5 = (str) => {
const md5sum = crypto.createHash('md5');
md5sum.update(str);
return md5sum.digest('hex');
};
class RethinkDBStore {
constructor(options) {
this.client = rethink({ this.client = rethink({
silent: true, silent: true,
host: options.host || '127.0.0.1', host: options.host || '127.0.0.1',
@ -10,30 +18,29 @@ var RethinkDBStore = (options) => {
user: options.user || 'admin', user: options.user || 'admin',
password: options.password || '' password: options.password || ''
}); });
}; }
RethinkDBStore.md5 = (str) => { set(key, data, callback) {
const md5sum = crypto.createHash('md5'); this.client.table('uploads').insert({ id: md5(key), data: data }).run((error) => {
md5sum.update(str); if (error) {
return md5sum.digest('hex'); callback(false);
}; winston.error('failed to insert to table', error);
return;
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); 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('uploads').get(md5(key)).run((error, result) => {
if (error || !result) return callback(false); if (error || !result) {
callback(false);
if (error) winston.error('failed to insert to table', error);
return;
}
callback(result.data); callback(result.data);
}); });
}; }
}
module.exports = RethinkDBStore; module.exports = RethinkDBStore;