Unverified Commit 56b93912 authored by Yuan Gao's avatar Yuan Gao Committed by GitHub
Browse files

Merge pull request #2 from seejohnrun/master

update from source
parents 7f625e22 ee1c1c08
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -36,7 +36,7 @@ DocumentHandler.prototype.handleRawGet = function(key, response, skipExpire) {
  this.store.get(key, function(ret) {
    if (ret) {
      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);
    }
    else {
+32 −25
Original line number Diff line number Diff line
const crypto = require('crypto');
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({
      silent: true,
      host: options.host || '127.0.0.1',
@@ -10,30 +18,29 @@ var RethinkDBStore = (options) => {
      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) => {
  try {
    this.client.table('uploads').insert({ id: RethinkDBStore.md5(key), data: data }).run((error) => {
      if (error) return callback(false);
  set(key, data, callback) {
    this.client.table('uploads').insert({ id: md5(key), data: data }).run((error) => {
      if (error) {
        callback(false);
        winston.error('failed to insert to table', error);
        return;
      }
      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);
  get(key, callback) {
    this.client.table('uploads').get(md5(key)).run((error, result) => {
      if (error || !result) {
        callback(false);
        if (error) winston.error('failed to insert to table', error);
        return;
      }
      callback(result.data);
    });
};
  }
}

module.exports = RethinkDBStore;