From 3dbcb08c97ac7ed5a20755beda1e0b2184a47532 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Mon, 18 Nov 2013 16:13:34 +0000 Subject: [PATCH] feature(rest) add ?hash --- lib/server/hash.js | 39 +++++++++++++++++++++++++++++++++++++++ lib/server/main.js | 1 + lib/server/rest.js | 18 ++++++++++++++++-- 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 lib/server/hash.js diff --git a/lib/server/hash.js b/lib/server/hash.js new file mode 100644 index 00000000..6aecf778 --- /dev/null +++ b/lib/server/hash.js @@ -0,0 +1,39 @@ +(function (object) { + 'use strict'; + + var util = require('util'), + Writable = require('stream').Writable; + + util.inherits(HashProto, Writable); + + object.Hash = new HashProto(); + + function HashProto() { + var shasum, + crypto = require('crypto'); + + Writable.call(this); + this.create = function() { + var ws = new Writable(); + + ws._write = write; + ws.get = get; + + shasum = crypto.createHash('sha1'); + + return ws; + }; + + function get() { + var hex = shasum.digest('hex'); + + return hex; + } + + function write (chunk, enc, next) { + shasum.update(chunk); + next(); + } + } + +})(this); diff --git a/lib/server/main.js b/lib/server/main.js index fe9cbec6..0edf8d0f 100644 --- a/lib/server/main.js +++ b/lib/server/main.js @@ -108,6 +108,7 @@ exports.auth = srvrequire('auth').auth, exports.appcache = srvrequire('appcache'), exports.dir = srvrequire('dir'), + exports.hash = srvrequire('hash').Hash; diffPatch = librequire('diff/diff-match-patch').diff_match_patch, exports.diff = new (librequire('diff').DiffProto)(diffPatch), exports.rest = srvrequire('rest').api, diff --git a/lib/server/rest.js b/lib/server/rest.js index d34d2290..176110bd 100644 --- a/lib/server/rest.js +++ b/lib/server/rest.js @@ -16,6 +16,7 @@ var main = global.cloudcmd.main, fs = main.fs, path = main.path, + Hash = main.hash, Util = main.util, pipe = main.pipe, CloudFunc = main.cloudfunc, @@ -98,7 +99,7 @@ } function onFS(pParams) { - var p, lError, lMsg, lSize, lQuery, + var p, lError, lMsg, lSize, lQuery, hash, lRet = main.checkParams(pParams); if (lRet){ @@ -115,7 +116,20 @@ sendResponse(p, lSize); }); }); - else + else if (Util.strCmp(lQuery, 'hash')) { + hash = Hash.create(); + + pipe.create({ + from : p.name, + write : hash, + callback : function (error) { + checkSendError(error, p, function() { + var hex = hash.get(); + sendResponse(p, hex); + }); + } + }); + } else fs.stat(p.name, function(pError, pStat) { checkSendError(pError, p, function() { if (!pStat.isDirectory())