Commit 2b579525 authored by Tristan Cavelier's avatar Tristan Cavelier

Add putAttachment method to JIO

parent 77e522dc
...@@ -24,6 +24,7 @@ module.exports = function(grunt) { ...@@ -24,6 +24,7 @@ module.exports = function(grunt) {
'<file_strip_banner:../../src/<%= pkg.name %>/commands/allDocsCommand.js>', '<file_strip_banner:../../src/<%= pkg.name %>/commands/allDocsCommand.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/commands/getCommand.js>', '<file_strip_banner:../../src/<%= pkg.name %>/commands/getCommand.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/commands/removeCommand.js>', '<file_strip_banner:../../src/<%= pkg.name %>/commands/removeCommand.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/commands/putAttachmentCommand.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/commands/putCommand.js>', '<file_strip_banner:../../src/<%= pkg.name %>/commands/putCommand.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/commands/postCommand.js>', '<file_strip_banner:../../src/<%= pkg.name %>/commands/postCommand.js>',
'<file_strip_banner:../../src/<%= pkg.name %>/jobs/status/jobStatus.js>', '<file_strip_banner:../../src/<%= pkg.name %>/jobs/status/jobStatus.js>',
......
...@@ -4,11 +4,14 @@ var command = function(spec, my) { ...@@ -4,11 +4,14 @@ var command = function(spec, my) {
my = my || {}; my = my || {};
// Attributes // // Attributes //
var priv = {}; var priv = {};
priv.commandlist = {'post':postCommand, priv.commandlist = {
'put':putCommand, 'post':postCommand,
'get':getCommand, 'put':putCommand,
'remove':removeCommand, 'get':getCommand,
'allDocs':allDocsCommand}; 'remove':removeCommand,
'allDocs':allDocsCommand,
'putAttachment':putAttachmentCommand
};
// creates the good command thanks to his label // creates the good command thanks to his label
if (spec.label && priv.commandlist[spec.label]) { if (spec.label && priv.commandlist[spec.label]) {
priv.label = spec.label; priv.label = spec.label;
...@@ -20,6 +23,9 @@ var command = function(spec, my) { ...@@ -20,6 +23,9 @@ var command = function(spec, my) {
priv.doc = spec.doc || {}; priv.doc = spec.doc || {};
priv.docid = spec.docid || ''; priv.docid = spec.docid || '';
priv.option = spec.options || {}; priv.option = spec.options || {};
priv.content = typeof spec.content === 'string'?
spec.content:
undefined;
priv.callbacks = spec.callbacks || {}; priv.callbacks = spec.callbacks || {};
priv.success = priv.callbacks.success || function (){}; priv.success = priv.callbacks.success || function (){};
priv.error = priv.callbacks.error || function (){}; priv.error = priv.callbacks.error || function (){};
...@@ -56,10 +62,13 @@ var command = function(spec, my) { ...@@ -56,10 +62,13 @@ var command = function(spec, my) {
}; };
that.getDocId = function () { that.getDocId = function () {
return priv.docid || priv.doc._id; return (priv.docid || priv.doc._id).split('/')[0];
};
that.getAttachmentId = function () {
return (priv.docid || priv.doc._id).split('/')[1];
}; };
that.getDocContent = function () { that.getContent = function () {
return priv.doc.content; return priv.content;
}; };
/** /**
...@@ -87,6 +96,15 @@ var command = function(spec, my) { ...@@ -87,6 +96,15 @@ var command = function(spec, my) {
* @param {object} storage The storage. * @param {object} storage The storage.
*/ */
that.validate = function (storage) { that.validate = function (storage) {
if (!(priv.docid || priv.doc._id).match(/^[^\/]+([\/][^\/]+)?$/)) {
that.error({
status:21,statusText:'Invalid Document Id',
error:'invalid_document_id',
message:'The document id must be like "abc" or "abc/def".',
reason:'The document id is no like "abc" or "abc/def"'
});
return false;
}
if (!that.validateState()) { if (!that.validateState()) {
return false; return false;
} }
...@@ -97,16 +115,6 @@ var command = function(spec, my) { ...@@ -97,16 +115,6 @@ var command = function(spec, my) {
* Extend this function * Extend this function
*/ */
that.validateState = function() { that.validateState = function() {
if (typeof priv.doc !== 'object') {
that.error({
status:20,
statusText:'Document_Id Required',
error:'document_id_required',
message:'No document id.',
reason:'no document id'
});
return false;
}
return true; return true;
}; };
......
var putAttachmentCommand = function(spec, my) {
var that = command(spec, my);
spec = spec || {};
my = my || {};
// Attributes //
// Methods //
that.getLabel = function () {
return 'putAttachment';
};
that.executeOn = function (storage) {
storage.putAttachment (that);
};
that.validateState = function () {
if (typeof that.getContent() !== 'string') {
that.error({
status:22,statusText:'Content Required',
error:'content_required',
message:'No data to put.',reason:'no data to put'
});
return false;
}
return true;
};
return that;
};
...@@ -318,5 +318,37 @@ ...@@ -318,5 +318,37 @@
} }
}); });
/**
* Put an attachment to a document.
* @method putAttachment
* @param {string} id The attachment id ("document/attachment").
* @param {string} rev The document revision.
* @param {string} doc Base64 attachment content.
* @param {string} mimetype The attachment mimetype
* @param {object} options (optional) Contains some options:
* - {number} max_retry The number max of retries, 0 = infinity.
* - {boolean} revs Include revision history of the document.
* - {boolean} revs_info Include revisions.
* - {boolean} conflicts Include conflicts.
* @param {function} callback (optional) The callback(err,respons)
* @param {function} error (optional) The callback on error, if this
* callback is given in parameter, "callback" is changed as "success",
* called on success.
*/
Object.defineProperty(that,"putAttachment",{
configurable:false,enumerable:false,writable:false,value:
function(id, rev, doc, mimetype, options, success, error) {
var param = priv.parametersToObject(
[options, success, error],
{max_retry: 0}
);
priv.addJob(putAttachmentCommand,{
doc:{_id:id,content:doc},
options:param.options,
callbacks:{success:param.success,error:param.error}
});
}
});
return that; return that;
}; // End Class jio }; // End Class jio
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment