Fix saving for node-library content

This commit is contained in:
Nick O'Leary 2015-02-26 17:08:20 +00:00
parent dfed4963ed
commit 393fc349b9
3 changed files with 31 additions and 26 deletions

View File

@ -294,19 +294,26 @@ RED.library = (function() {
//} //}
} }
var queryArgs = []; var queryArgs = [];
var data = {};
for (var i=0;i<options.fields.length;i++) { for (var i=0;i<options.fields.length;i++) {
var field = options.fields[i]; var field = options.fields[i];
if (field == "name") { if (field == "name") {
queryArgs.push("name="+encodeURIComponent(name)); data.name = name;
} else { } else {
queryArgs.push(encodeURIComponent(field)+"="+encodeURIComponent($("#node-input-"+field).val())); data[field] = $("#node-input-"+field).val();
} }
} }
var queryString = queryArgs.join("&");
var text = options.editor.getText(); data.text = options.editor.getText();
$.post("library/"+options.url+'/'+fullpath+"?"+queryString,text,function() { $.ajax({
RED.notify("Saved "+options.type,"success"); url:"library/"+options.url+'/'+fullpath,
type: "POST",
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8"
}).done(function(data,textStatus,xhr) {
RED.notify("Saved "+options.type,"success");
}).fail(function(xhr,textStatus,err) {
RED.notify("Saved failed: "+xhr.responseText,"error");
}); });
} }
$( "#node-dialog-library-save-confirm" ).dialog({ $( "#node-dialog-library-save-confirm" ).dialog({

View File

@ -45,21 +45,19 @@ function createLibrary(type) {
redApp.post(new RegExp("/library/"+type+"\/(.*)"),needsPermission("library.write"),function(req,res) { redApp.post(new RegExp("/library/"+type+"\/(.*)"),needsPermission("library.write"),function(req,res) {
var path = req.params[0]; var path = req.params[0];
var fullBody = ''; var meta = req.body;
req.on('data', function(chunk) { var text = meta.text;
fullBody += chunk.toString(); delete meta.text;
});
req.on('end', function() { storage.saveLibraryEntry(type,path,meta,text).then(function() {
storage.saveLibraryEntry(type,path,req.query,fullBody).then(function() { res.send(204);
res.send(204); }).otherwise(function(err) {
}).otherwise(function(err) { log.warn("Error saving library entry '"+path+"' : "+err);
log.warn("Error saving library entry '"+path+"' : "+err); if (err.message.indexOf('forbidden') === 0) {
if (err.message.indexOf('forbidden') === 0) { res.send(403);
res.send(403); return;
return; }
} res.send(500);
res.send(500);
});
}); });
}); });
} }

View File

@ -195,10 +195,10 @@ describe("library api", function() {
it('can store and retrieve item', function(done) { it('can store and retrieve item', function(done) {
initStorage({},{'test':{}}); initStorage({},{'test':{}});
var flow = '[]'; var flow = {text:"test content"};
request(app) request(app)
.post('/library/test/foo') .post('/library/test/foo')
.set('Content-Type', 'text/plain') .set('Content-Type', 'application/json')
.send(flow) .send(flow)
.expect(204).end(function (err, res) { .expect(204).end(function (err, res) {
if (err) { if (err) {
@ -211,16 +211,16 @@ describe("library api", function() {
if (err) { if (err) {
throw err; throw err;
} }
res.text.should.equal(flow); res.text.should.equal(flow.text);
done(); done();
}); });
}); });
}); });
it('lists a stored item', function(done) { it('lists a stored item', function(done) {
initStorage({},{'test':{'':['abc','def']}}); initStorage({},{'test':{'a':['abc','def']}});
request(app) request(app)
.get('/library/test') .get('/library/test/a')
.expect(200) .expect(200)
.end(function(err,res) { .end(function(err,res) {
if (err) { if (err) {