feature(rest) add ?hash

This commit is contained in:
coderaiser 2013-11-18 16:13:34 +00:00
parent 4e49eb5095
commit 3dbcb08c97
3 changed files with 56 additions and 2 deletions

39
lib/server/hash.js Normal file
View file

@ -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);

View file

@ -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,

View file

@ -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())