feature(rest) add put patch

This commit is contained in:
coderaiser 2013-07-15 13:28:05 +00:00
parent d2ba63e050
commit ee5d3bc7e9
5 changed files with 57 additions and 21 deletions

View file

@ -116,6 +116,8 @@ getJSONfromFileTable.
* feature(cloudcmd) add bin
* feature(rest) add put patch
2012.04.22, v0.2.0

View file

@ -116,10 +116,10 @@ var CloudCmd, Util, DOM, CloudFunc;
});
};
this.save = function(pUrl, pData, pCallBack){
this.save = function(pUrl, pData, pCallBack, pQuery){
sendRequest({
method : 'PUT',
url : CloudFunc.FS + pUrl,
url : CloudFunc.FS + pUrl + (pQuery || ''),
data : pData,
callback : pCallBack,
imgPosition : { top: true }

View file

@ -73,10 +73,26 @@ var CloudCmd, Util, DOM, JsDiff, ace;
exec : function (pEditor) {
var lCurrent = DOM.getCurrentFile(),
lPath = DOM.getCurrentPath(lCurrent),
lValue = Ace.getValue();
lName = DOM.getCurrentName(lCurrent),
lValue = Ace.getValue(),
lLength = lValue.length;
DOM.setCurrentSize( lValue.length, lCurrent );
DOM.RESTfull.save( lPath, lValue );
DOM.setCurrentSize( lLength, lCurrent );
diff(lName, lValue, function(pDiff) {
var lData,
lPatch = '',
lDiffLength = pDiff.length;
if (lDiffLength >= lLength)
lData = lValue;
else {
lData = pDiff;
lPatch = '?patch';
}
DOM.RESTfull.save( lPath, lData, null, lPatch);
});
}
});
}
@ -112,16 +128,13 @@ var CloudCmd, Util, DOM, JsDiff, ace;
Ace.moveCursorTo(0, 0);
}
this.diff = function(pName){
var lName = DOM.getCurrentName(),
lNewValue = Ace.getValue();
function diff(pName, pNewValue, pCallBack){
DOM.jsload('/lib/util/jsdiff/diff.js', function(){
var lPatch = JsDiff.createPatch(lName, Value, lNewValue);
var lPatch = JsDiff.createPatch(pName, Value, pNewValue);
Util.log(lPatch);
Util.exec(pCallBack, lPatch);
});
};
}
function load(pCallBack){
Util.time(Name + ' load');

View file

@ -77,7 +77,7 @@
/* we can not use librequare here */
exports.util = Util = require(LIBDIR + UTIL),
exports.diff = mrequire(LIBDIR + UTIL + '/jsdiff/diff').JsDiff,
exports.diff = mrequire(LIBDIR + UTIL + '/jsdiff/diff'),
exports.zlib = zlib = mrequire('zlib'),

View file

@ -18,6 +18,7 @@
Util = main.util,
CloudFunc = main.cloudfunc,
dir = main.dir,
diff = main.diff,
OK = 200,
Header = main.generateHeaders({
name:'api.json'
@ -62,14 +63,14 @@
* @param pParams {command, method, body, requrest, response}
*/
function sendData(pParams){
var lRet = main.checkParams(pParams);
var p, lRet = main.checkParams(pParams);
if(lRet){
var p = pParams;
p = pParams;
lRet = Util.isContainStrAtBegin(p.name, CloudFunc.FS);
if( lRet)
if (lRet)
onFS(pParams);
else{
else {
if(p.name[0] === '/')
p.command = Util.removeStrOneTime(p.name, '/');
@ -79,9 +80,9 @@
break;
case 'PUT':
getBody(pParams.request, function(pBody){
pParams.body = pBody;
onPUT(pParams);
getBody(p.request, function(pBody){
p.body = pBody;
onPUT(p);
});
break;
}
@ -132,13 +133,33 @@
break;
case 'PUT':
if(lQuery === 'dir')
if (lQuery === 'dir')
fs.mkdir(p.name, function(pError){
if(!pError)
main.sendResponse(pParams, 'Folder ' + p.name + ' created.');
else
main.sendError(pParams, pError);
});
else if(lQuery === 'patch') {
getBody(p.request, function(pPatch) {
fs.readFile(p.name, function(pError, pData) {
var lDiff, lStr = pData.toString();
if (pError)
main.sendError(pParams, pError);
else {
lDiff = diff.applyPatch(lStr, pPatch);
fs.writeFile(p.name, lDiff, function(){
if (pError)
main.sendError(pParams, pError);
else
main.sendResponse(pParams, 'writed: ' + p.name);
});
}
});
});
}
else {
lWriteStream = fs.createWriteStream(p.name);