Commit 33f6cd32 authored by Sven Franck's avatar Sven Franck

webDav Storage: bugfixes, jslint passing

parent cf646d92
...@@ -87,10 +87,31 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -87,10 +87,31 @@ jIO.addStorageType('dav', function (spec, my) {
return async; return async;
}; };
priv.checkCors = function(){ /**
* Checks if a browser supports cors (cross domain ajax requests)
* @method checkCors
* @return {boolean} true if supported, else false
*/
priv.checkCors = function () {
return $.support.cors; return $.support.cors;
}; };
/**
* Replaces last "." with "_." in document filenames
* @method underscoreFileExtenisons
* @param {string} url url to clean up
* @return {string} clean_url cleaned up URL
*/
priv.underscoreFileExtenisons = function (url) {
var clean_url = url.replace(/,\s(\w+)$/, "_.$1");
return clean_url;
};
priv.restoreDots = function (url) {
var clean_url = url.replace(/_\./g, '.');
return clean_url;
};
// wedDav methods rfc4918 (short summary) // wedDav methods rfc4918 (short summary)
// COPY Reproduces single resources (files) and collections (directory // COPY Reproduces single resources (files) and collections (directory
// trees). Will overwrite files (if specified by request) but will // trees). Will overwrite files (if specified by request) but will
...@@ -118,36 +139,36 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -118,36 +139,36 @@ jIO.addStorageType('dav', function (spec, my) {
priv.putOrPost = function (command, type) { priv.putOrPost = function (command, type) {
var doc = command.getDocId(), var docid = command.getDocId(),
secured_docid; secured_docid,
url;
// no docId // no docId
if (!(typeof doc === "string" && doc !== "")) { if (!(typeof docid === "string" && docid !== "")) {
that.error({ that.error({
"status": 405, "status": 405,
"statusText": "Method Not Allowed", "statusText": "Method Not Allowed",
"error": "method_not_allowed", "error": "method_not_allowed",
"message": "Cannot create document which id is undefined", "message": "Cannot create document which id is undefined",
"reason": "Document id is undefined" "reason": "Document id is undefined"
}); });
return; return;
} }
// no cross domain ajax // no cross domain ajax
if (priv.checkCors === false) { if (priv.checkCors === false) {
that.error({ that.error({
"status": 405, "status": 405,
"statusText": "Method Not Allowed", "statusText": "Method Not Allowed",
"error": "method_not_allowed", "error": "method_not_allowed",
"message": "Browser does not support cross domain ajax requests", "message": "Browser does not support cross domain ajax requests",
"reason": "cors is undefined" "reason": "cors is undefined"
}); });
return; return;
} }
secured_docid = priv.secureDocId(command.getDocId()); secured_docid = priv.secureDocId(command.getDocId());
url = priv.url + '/' + secured_docid; url = priv.url + '/' + priv.underscoreFileExtenisons(secured_docid);
// see if the document exists // see if the document exists
$.ajax({ $.ajax({
url: url + '?_=' + Date.now(), url: url + '?_=' + Date.now(),
...@@ -156,77 +177,75 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -156,77 +177,75 @@ jIO.addStorageType('dav', function (spec, my) {
dataType: 'text', dataType: 'text',
crossdomain : true, crossdomain : true,
headers : { headers : {
Authorization: 'Basic ' + Base64.encode( Authorization: 'Basic ' + Base64.encode(
priv.username + ':' + priv.password priv.username + ':' + priv.password
) )
}, },
// xhrFields: {withCredentials: 'true'}, // xhrFields: {withCredentials: 'true'},
success: function (content) { success: function () {
if (type === 'POST') { if (type === 'POST') {
// POST the document already exists // POST the document already exists
that.error({ that.error({
"status": 409,
"statusText": "Conflicts",
"error": "conflicts",
"message": "Cannot create a new document",
"reason": "Document already exists"
});
return;
}
// PUT update document
$.ajax({
url: url,
type: type,
data: JSON.stringify(command.getDoc()),
async: true,
crossdomain: true,
headers : {
Authorization: 'Basic ' + Base64.encode(
priv.username + ':' + priv.password
)
},
// xhrFields: {withCredentials: 'true'},
success: function () {
that.success({
ok: true,
id: command.getDocId()
});
},
error: function () {
that.error({
"status": 409, "status": 409,
"statusText": "Conflicts", "statusText": "Conflicts",
"error": "conflicts", "error": "conflicts",
"message": "Cannot create a new document", "message": "Cannot modify document",
"reason": "Document already exists" "reason": "Error trying to write to remote storage"
}); });
return; }
} else { });
// PUT update document
$.ajax({
url: url + '?_=' + Date.now(),
type: type,
data: command.getDoc(),
async: true,
crossdomain: true,
headers : {
Authorization: 'Basic ' + Base64.encode(
priv.username + ':' + priv.password
)
},
// xhrFields: {withCredentials: 'true'},
success: function () {
that.success({
ok: true,
id: command.getDocId()
});
},
error: function (type) {
that.error({
"status": 409,
"statusText": "Conflicts",
"error": "conflicts",
"message": "Cannot modify document",
"reason": "Error trying to write to remote storage"
});
return;
}
});
}
}, },
error: function (err) { error: function (err) {
if (err.status === 404) { // Firefox returns 0 instead of 404 on CORS?
if (err.status === 404 || err.status === 0) {
$.ajax({ $.ajax({
url: url + '?_=' + Date.now(), url: url,
// must always use put, POST only seems to work on collections
type: 'PUT', type: 'PUT',
data: command.getDoc(), data: JSON.stringify(command.getDoc()),
async: true, async: true,
crossdomain: true, crossdomain: true,
headers : { headers : {
Authorization: 'Basic ' + Base64.encode( Authorization: 'Basic ' + Base64.encode(
priv.username + ':' + priv.password priv.username + ':' + priv.password
) )
}, },
// xhrFields: {withCredentials: 'true'}, // xhrFields: {withCredentials: 'true'},
success: function (response) { success: function () {
that.success({ that.success({
ok: true, ok: true,
id: command.getDocId() id: command.getDocId()
}); });
}, },
error: function (type) { error: function () {
that.error({ that.error({
"status": 409, "status": 409,
"statusText": "Conflicts", "statusText": "Conflicts",
...@@ -234,7 +253,6 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -234,7 +253,6 @@ jIO.addStorageType('dav', function (spec, my) {
"message": "Cannot modify document", "message": "Cannot modify document",
"reason": "Error trying to write to remote storage" "reason": "Error trying to write to remote storage"
}); });
return;
} }
}); });
...@@ -247,7 +265,6 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -247,7 +265,6 @@ jIO.addStorageType('dav', function (spec, my) {
"message": err.message, "message": err.message,
"reason": "Failed to access remote storage" "reason": "Failed to access remote storage"
}); });
return;
} }
} }
}); });
...@@ -277,36 +294,40 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -277,36 +294,40 @@ jIO.addStorageType('dav', function (spec, my) {
* @param {object} command The JIO command * @param {object} command The JIO command
*/ */
that.putAttachment = function (command) { that.putAttachment = function (command) {
var docid = command.getDocId(), doc, var docid = command.getDocId(),
secured_docid, secured_attachmentid, attachment_url; doc,
url,
secured_docid,
secured_attachmentid,
attachment_url;
// no docId // no docId
if (!(typeof docid === "string" && docid !== "")) { if (!(typeof docid === "string" && docid !== "")) {
that.error({ that.error({
"status": 405, "status": 405,
"statusText": "Method Not Allowed", "statusText": "Method Not Allowed",
"error": "method_not_allowed", "error": "method_not_allowed",
"message": "Cannot create document which id is undefined", "message": "Cannot create document which id is undefined",
"reason": "Document id is undefined" "reason": "Document id is undefined"
}); });
return; return;
} }
// no cross domain ajax // no cross domain ajax
if (priv.checkCors === false) { if (priv.checkCors === false) {
that.error({ that.error({
"status": 405, "status": 405,
"statusText": "Method Not Allowed", "statusText": "Method Not Allowed",
"error": "method_not_allowed", "error": "method_not_allowed",
"message": "Browser does not support cross domain ajax requests", "message": "Browser does not support cross domain ajax requests",
"reason": "cors is undefined" "reason": "cors is undefined"
}); });
return; return;
} }
secured_docid = priv.secureDocId(docid); secured_docid = priv.secureDocId(docid);
url = priv.url + '/' + secured_docid; url = priv.url + '/' + priv.underscoreFileExtenisons(secured_docid);
// see if the underlying document exists || // see if the underlying document exists
$.ajax({ $.ajax({
url: url + '?_=' + Date.now(), url: url + '?_=' + Date.now(),
type: 'GET', type: 'GET',
...@@ -314,10 +335,10 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -314,10 +335,10 @@ jIO.addStorageType('dav', function (spec, my) {
dataType: 'text', dataType: 'text',
crossdomain : true, crossdomain : true,
headers : { headers : {
Authorization: 'Basic ' + Base64.encode( Authorization: 'Basic ' + Base64.encode(
priv.username + ':' + priv.password priv.username + ':' + priv.password
) )
}, },
success: function (response) { success: function (response) {
doc = JSON.parse(response); doc = JSON.parse(response);
...@@ -328,43 +349,42 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -328,43 +349,42 @@ jIO.addStorageType('dav', function (spec, my) {
"digest": "md5-" + command.md5SumAttachmentData(), "digest": "md5-" + command.md5SumAttachmentData(),
"length": command.getAttachmentLength() "length": command.getAttachmentLength()
}; };
// put updated document data // put updated document data
$.ajax({ $.ajax({
url: url + '?_=' + Date.now(), url: url + '?_=' + Date.now(),
type: 'PUT', type: 'PUT',
data: doc, data: JSON.stringify(doc),
async: true, async: true,
crossdomain: true, crossdomain: true,
headers : { headers : {
Authorization: 'Basic ' + Base64.encode( Authorization: 'Basic ' + Base64.encode(
priv.username + ':' + priv.password priv.username + ':' + priv.password
) )
}, },
// xhrFields: {withCredentials: 'true'}, // xhrFields: {withCredentials: 'true'},
success: function () { success: function () {
secured_attachmentid = priv.secureDocId(command.getAttachmentId()); secured_attachmentid = priv.secureDocId(command.getAttachmentId());
attachment_url = url + '/' + secured_attachmentid; attachment_url = url + '.' +
priv.underscoreFileExtenisons(secured_attachmentid);
$.ajax({ $.ajax({
url: attachment_url + '?_=' + Date.now(), url: attachment_url + '?_=' + Date.now(),
type: 'PUT', type: 'PUT',
data: command.getDoc(), data: JSON.stringify(command.getDoc()),
async: true, async: true,
crossdomain: true, crossdomain: true,
headers : { headers : {
Authorization: 'Basic ' + Base64.encode( Authorization: 'Basic ' + Base64.encode(
priv.username + ':' + priv.password priv.username + ':' + priv.password
) )
}, },
// xhrFields: {withCredentials: 'true'}, // xhrFields: {withCredentials: 'true'},
success: function (response) { success: function () {
that.success({ that.success({
ok: true, ok: true,
id: command.getDocId()+'/'+command.getAttachmentId() id: command.getDocId() + '/' + command.getAttachmentId()
}); });
}, },
error: function (type) { error: function () {
that.error({ that.error({
"status": 409, "status": 409,
"statusText": "Conflicts", "statusText": "Conflicts",
...@@ -376,7 +396,7 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -376,7 +396,7 @@ jIO.addStorageType('dav', function (spec, my) {
} }
}); });
}, },
error: function (type) { error: function () {
that.error({ that.error({
"status": 409, "status": 409,
"statusText": "Conflicts", "statusText": "Conflicts",
...@@ -408,35 +428,44 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -408,35 +428,44 @@ jIO.addStorageType('dav', function (spec, my) {
* @param {object} command The JIO command * @param {object} command The JIO command
*/ */
that.get = function (command) { that.get = function (command) {
var docid = command.getDocId(), doc, var docid = command.getDocId(),
secured_docid, secured_attachmentid, attachment_url; doc,
url,
secured_docid,
secured_attachmentid,
attachment_url;
// no docId // no docId
if (!(typeof docid === "string" && docid !== "")) { if (!(typeof docid === "string" && docid !== "")) {
that.error({ that.error({
"status": 405, "status": 405,
"statusText": "Method Not Allowed", "statusText": "Method Not Allowed",
"error": "method_not_allowed", "error": "method_not_allowed",
"message": "Cannot create document which id is undefined", "message": "Cannot create document which id is undefined",
"reason": "Document id is undefined" "reason": "Document id is undefined"
}); });
return; return;
} }
// no cors support // no cors support
if (priv.checkCors === false) { if (priv.checkCors === false) {
that.error({ that.error({
"status": 405, "status": 405,
"statusText": "Method Not Allowed", "statusText": "Method Not Allowed",
"error": "method_not_allowed", "error": "method_not_allowed",
"message": "Browser does not support cross domain ajax requests", "message": "Browser does not support cross domain ajax requests",
"reason": "cors is undefined" "reason": "cors is undefined"
}); });
return; return;
} }
secured_docid = priv.secureDocId(command.getDocId());
url = priv.url + '/' + priv.underscoreFileExtenisons(secured_docid);
if (typeof command.getAttachmentId() === "string") { if (typeof command.getAttachmentId() === "string") {
secured_attachmentid = priv.secureDocId(command.getAttachmentId()); secured_attachmentid = priv.secureDocId(command.getAttachmentId());
attachment_url = url + '/' + secured_attachmentid; attachment_url = url + '.' + priv.underscoreFileExtenisons(
secured_attachmentid
);
// get attachment // get attachment
$.ajax({ $.ajax({
url: attachment_url + '?_=' + Date.now(), url: attachment_url + '?_=' + Date.now(),
...@@ -447,13 +476,13 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -447,13 +476,13 @@ jIO.addStorageType('dav', function (spec, my) {
headers : { headers : {
Authorization: 'Basic ' + Base64.encode( Authorization: 'Basic ' + Base64.encode(
priv.username + ':' + priv.password priv.username + ':' + priv.password
) )
}, },
success: function (response) { success: function (response) {
doc = JSON.parse(response); doc = JSON.parse(response);
that.success(doc); that.success(doc);
}, },
error: function (type) { error: function () {
that.error({ that.error({
"status": 404, "status": 404,
"statusText": "Not Found", "statusText": "Not Found",
...@@ -464,8 +493,6 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -464,8 +493,6 @@ jIO.addStorageType('dav', function (spec, my) {
} }
}); });
} else { } else {
secured_docid = priv.secureDocId(command.getDocId());
url = priv.url + '/' + secured_docid;
// get document // get document
$.ajax({ $.ajax({
...@@ -477,8 +504,8 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -477,8 +504,8 @@ jIO.addStorageType('dav', function (spec, my) {
headers : { headers : {
Authorization: 'Basic ' + Base64.encode( Authorization: 'Basic ' + Base64.encode(
priv.username + ':' + priv.password priv.username + ':' + priv.password
) )
}, },
success: function (response) { success: function (response) {
// metadata_only should not be handled by jIO, as it is a // metadata_only should not be handled by jIO, as it is a
// webDav only option, shouldn't it? // webDav only option, shouldn't it?
...@@ -486,7 +513,7 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -486,7 +513,7 @@ jIO.addStorageType('dav', function (spec, my) {
doc = JSON.parse(response); doc = JSON.parse(response);
that.success(doc); that.success(doc);
}, },
error: function (type) { error: function () {
that.error({ that.error({
"status": 404, "status": 404,
"statusText": "Not Found", "statusText": "Not Found",
...@@ -505,51 +532,52 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -505,51 +532,52 @@ jIO.addStorageType('dav', function (spec, my) {
* @param {object} command The JIO command * @param {object} command The JIO command
*/ */
that.remove = function (command) { that.remove = function (command) {
var docid = command.getDocId(), doc, var docid = command.getDocId(), doc, url,
secured_docid, secured_attachmentid, attachment_url, secured_docid, secured_attachmentid, attachment_url,
attachment_list = [], i, j, k = 1; attachment_list = [], i, j, k = 1, deleteAttachment;
// no docId // no docId
if (!(typeof docid === "string" && docid !== "")) { if (!(typeof docid === "string" && docid !== "")) {
that.error({ that.error({
"status": 405, "status": 405,
"statusText": "Method Not Allowed", "statusText": "Method Not Allowed",
"error": "method_not_allowed", "error": "method_not_allowed",
"message": "Cannot create document which id is undefined", "message": "Cannot create document which id is undefined",
"reason": "Document id is undefined" "reason": "Document id is undefined"
}); });
return;
} }
// no cors support // no cors support
if (priv.checkCors === false) { if (priv.checkCors === false) {
that.error({ that.error({
"status": 405, "status": 405,
"statusText": "Method Not Allowed", "statusText": "Method Not Allowed",
"error": "method_not_allowed", "error": "method_not_allowed",
"message": "Browser does not support cross domain ajax requests", "message": "Browser does not support cross domain ajax requests",
"reason": "cors is undefined" "reason": "cors is undefined"
}); });
return;
} }
secured_docid = priv.secureDocId(command.getDocId()); secured_docid = priv.secureDocId(command.getDocId());
url = priv.url + '/' + secured_docid; url = priv.url + '/' + priv.underscoreFileExtenisons(secured_docid);
// remove attachment
if (typeof command.getAttachmentId() === "string") { if (typeof command.getAttachmentId() === "string") {
secured_attachmentid = priv.secureDocId(command.getAttachmentId()); secured_attachmentid = priv.secureDocId(command.getAttachmentId());
attachment_url = url + '/' + secured_attachmentid; attachment_url = url + '.' + priv.underscoreFileExtenisons(
// remove attachment secured_attachmentid
);
$.ajax({ $.ajax({
url: attachment_url + '?_=' + Date.now(), url: attachment_url + '?_=' + Date.now(),
type: 'REMOVE', type: 'DELETE',
async: true, async: true,
crossdomain : true, crossdomain : true,
headers : { headers : {
Authorization: 'Basic ' + Base64.encode( Authorization: 'Basic ' + Base64.encode(
priv.username + ':' + priv.password priv.username + ':' + priv.password
) )
}, },
success: function (response) { success: function () {
// retrieve underlying document // retrieve underlying document
$.ajax({ $.ajax({
url: url + '?_=' + Date.now(), url: url + '?_=' + Date.now(),
...@@ -560,8 +588,8 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -560,8 +588,8 @@ jIO.addStorageType('dav', function (spec, my) {
headers : { headers : {
Authorization: 'Basic ' + Base64.encode( Authorization: 'Basic ' + Base64.encode(
priv.username + ':' + priv.password priv.username + ':' + priv.password
) )
}, },
success: function (response) { success: function (response) {
// underlying document // underlying document
doc = JSON.parse(response); doc = JSON.parse(response);
...@@ -578,22 +606,23 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -578,22 +606,23 @@ jIO.addStorageType('dav', function (spec, my) {
$.ajax({ $.ajax({
url: url + '?_=' + Date.now(), url: url + '?_=' + Date.now(),
type: 'PUT', type: 'PUT',
data: doc, data: JSON.stringify(doc),
async: true, async: true,
crossdomain: true, crossdomain: true,
headers : { headers : {
Authorization: 'Basic ' + Base64.encode( Authorization: 'Basic ' + Base64.encode(
priv.username + ':' + priv.password priv.username + ':' + priv.password
) )
}, },
// xhrFields: {withCredentials: 'true'}, // xhrFields: {withCredentials: 'true'},
success: function (response) { success: function () {
that.success({ that.success({
"ok": true, "ok": true,
"id": command.getDocId()+'/'+command.getAttachmentId() "id": command.getDocId() + '/' +
command.getAttachmentId()
}); });
}, },
error: function (type) { error: function () {
that.error({ that.error({
"status": 409, "status": 409,
"statusText": "Conflicts", "statusText": "Conflicts",
...@@ -601,12 +630,11 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -601,12 +630,11 @@ jIO.addStorageType('dav', function (spec, my) {
"message": "Cannot modify document", "message": "Cannot modify document",
"reason": "Error trying to update document attachments" "reason": "Error trying to update document attachments"
}); });
return;
} }
}); });
} else { } else {
// sure this if-else is needed? // sure this if-else is needed?
that.error({ that.error({
"status": 404, "status": 404,
"statusText": "Not Found", "statusText": "Not Found",
"error": "not_found", "error": "not_found",
...@@ -622,7 +650,7 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -622,7 +650,7 @@ jIO.addStorageType('dav', function (spec, my) {
}); });
} }
}, },
error: function (type) { error: function () {
that.error({ that.error({
"status": 404, "status": 404,
"statusText": "Not Found", "statusText": "Not Found",
...@@ -633,7 +661,7 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -633,7 +661,7 @@ jIO.addStorageType('dav', function (spec, my) {
} }
}); });
}, },
error: function (type) { error: function () {
that.error({ that.error({
"status": 404, "status": 404,
"statusText": "Not Found", "statusText": "Not Found",
...@@ -643,9 +671,8 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -643,9 +671,8 @@ jIO.addStorageType('dav', function (spec, my) {
}); });
} }
}); });
// remove document
} else { } else {
secured_docid = priv.secureDocId(command.getDocId());
url = priv.url + '/' + secured_docid;
// get document to also remove all attachments // get document to also remove all attachments
$.ajax({ $.ajax({
...@@ -657,31 +684,32 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -657,31 +684,32 @@ jIO.addStorageType('dav', function (spec, my) {
headers : { headers : {
Authorization: 'Basic ' + Base64.encode( Authorization: 'Basic ' + Base64.encode(
priv.username + ':' + priv.password priv.username + ':' + priv.password
) )
}, },
success: function (response) { success: function (response) {
var x;
doc = JSON.parse(response); doc = JSON.parse(response);
// prepare attachment loop // prepare attachment loop
if (typeof doc._attachments === "object") { if (typeof doc._attachments === "object") {
// prepare list of attachments // prepare list of attachments
for (i in doc._attachments) { for (x in doc._attachments) {
if (doc._attachments.hasOwnProperty(i)) { if (doc._attachments.hasOwnProperty(x)) {
attachment_list.push(i); attachment_list.push(x);
} }
} }
} }
// delete document // delete document
$.ajax({ $.ajax({
url: url + '?_=' + Date.now(), url: url + '?_=' + Date.now(),
type: 'REMOVE', type: 'DELETE',
async: true, async: true,
crossdomain : true, crossdomain : true,
headers : { headers : {
Authorization: 'Basic ' + Base64.encode( Authorization: 'Basic ' + Base64.encode(
priv.username + ':' + priv.password priv.username + ':' + priv.password
) )
}, },
success: function (response) { success: function () {
j = attachment_list.length; j = attachment_list.length;
// no attachments, done // no attachments, done
if (j === 0) { if (j === 0) {
...@@ -690,23 +718,20 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -690,23 +718,20 @@ jIO.addStorageType('dav', function (spec, my) {
"id": command.getDocId() "id": command.getDocId()
}); });
} else { } else {
for (i = 0; i < j; i += 1) { deleteAttachment = function (attachment_url, j, k) {
secured_attachmentid = priv.secureDocId(attachment_list[i]);
attachment_url = url + '/' + secured_attachmentid;
$.ajax({ $.ajax({
url: attachment_url + '?_=' + Date.now(), url: attachment_url + '?_=' + Date.now(),
type: 'REMOVE', type: 'DELETE',
async: true, async: true,
crossdomain : true, crossdomain : true,
headers : { headers : {
Authorization: 'Basic ' + Base64.encode( Authorization: 'Basic ' + Base64.encode(
priv.username + ':' + priv.password priv.username + ':' + priv.password
) )
}, },
success: function (response) { success: function () {
// all deleted, return response, need k as async couter // all deleted, return response, need k as async couter
if (j === k){ if (j === k) {
that.success({ that.success({
"ok": true, "ok": true,
"id": command.getDocId() "id": command.getDocId()
...@@ -715,8 +740,8 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -715,8 +740,8 @@ jIO.addStorageType('dav', function (spec, my) {
k += 1; k += 1;
} }
}, },
error: function (type) { error: function () {
that.error({ that.error({
"status": 404, "status": 404,
"statusText": "Not Found", "statusText": "Not Found",
"error": "not_found", "error": "not_found",
...@@ -726,9 +751,16 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -726,9 +751,16 @@ jIO.addStorageType('dav', function (spec, my) {
} }
}); });
}; };
for (i = 0; i < j; i += 1) {
secured_attachmentid = priv.secureDocId(attachment_list[i]);
attachment_url = url + '.' + priv.underscoreFileExtenisons(
secured_attachmentid
);
deleteAttachment(attachment_url, j, k);
}
} }
}, },
error: function (type) { error: function () {
that.error({ that.error({
"status": 404, "status": 404,
"statusText": "Not Found", "statusText": "Not Found",
...@@ -739,7 +771,7 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -739,7 +771,7 @@ jIO.addStorageType('dav', function (spec, my) {
} }
}); });
}, },
error: function (type) { error: function () {
that.error({ that.error({
"status": 404, "status": 404,
"statusText": "Not Found", "statusText": "Not Found",
...@@ -769,24 +801,27 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -769,24 +801,27 @@ jIO.addStorageType('dav', function (spec, my) {
// },{...} // },{...}
// ] // ]
//} //}
that.allDocs = function (command) { that.allDocs = function (command) {
var rows = [], url, var rows = [], url,
am = priv.newAsyncModule(), am = priv.newAsyncModule(),
o = {}; o = {};
o.getContent = function (file) { o.getContent = function (file) {
var docid = priv.secureDocId(file.id),
url = priv.url + '/' + docid;
$.ajax({ $.ajax({
url: priv.url + '/' + priv.secureDocId(file.id) + '?_=' + Date.now(), url: url + '?_=' + Date.now(),
type: 'GET', type: 'GET',
async: true, async: true,
dataType: 'text', dataType: 'text',
headers: { headers: {
'Authorization': 'Basic ' + Base64.encode( 'Authorization': 'Basic ' + Base64.encode(
priv.username + ':' + priv.password) priv.username + ':' + priv.password
)
}, },
success: function (content) { success: function (content) {
file.value.content = content; file.doc = JSON.parse(content);
rows.push(file); rows.push(file);
am.call(o, 'success'); am.call(o, 'success');
}, },
...@@ -809,16 +844,16 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -809,16 +844,16 @@ jIO.addStorageType('dav', function (spec, my) {
url: url + '?_=' + Date.now(), url: url + '?_=' + Date.now(),
type: 'PROPFIND', type: 'PROPFIND',
async: true, async: true,
dataType: 'xml', dataType: "xml",
crossdomain : true, crossdomain : true,
headers : { headers : {
Authorization: 'Basic ' + Base64.encode( Authorization: 'Basic ' + Base64.encode(
priv.username + ':' + priv.password priv.username + ':' + priv.password
), ),
Depth: '1' Depth: '1'
}, },
success: function (xmlData) { success: function (xml) {
var response = $(xmlData).find('D\\:response, response'), var response = $(xml).find('D\\:response, response'),
len = response.length; len = response.length;
if (len === 1) { if (len === 1) {
...@@ -836,10 +871,7 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -836,10 +871,7 @@ jIO.addStorageType('dav', function (spec, my) {
file.id = priv.restoreSlashes(file.id); file.id = priv.restoreSlashes(file.id);
file.key = file.id; file.key = file.id;
}); });
// this should probably also filter for the "." in case if (command.getOption('include_docs')) {
// there is a title.revision. Then we could fill value in
// allDocs, too!
if (command.getOption('include_content')) {
am.call(o, 'getContent', [file]); am.call(o, 'getContent', [file]);
} else { } else {
rows.push(file); rows.push(file);
...@@ -886,4 +918,4 @@ jIO.addStorageType('dav', function (spec, my) { ...@@ -886,4 +918,4 @@ jIO.addStorageType('dav', function (spec, my) {
}; };
return that; return that;
}); });
\ No newline at end of file
...@@ -2044,23 +2044,7 @@ test ("Post", function () { ...@@ -2044,23 +2044,7 @@ test ("Post", function () {
o.clock.tick(5000); o.clock.tick(5000);
o.server.respond(); o.server.respond();
// as all custom headers trigger preflight requests, the test also
// need to simulate CORS (cross domain ajax with preflight)
// custom header may be authentication for example
o.jio.stop(); o.jio.stop();
// do the same tests live webDav-Server simulating CORS!
/* also check for equality
deepEqual(
localstorage.getItem("jio/localstorage/uput/aput/put1"),
{
"_id": "put1",
"title": "myPut1"
},
"Check document"
);
*/
}); });
test ("Put", function(){ test ("Put", function(){
...@@ -2100,10 +2084,6 @@ test ("Put", function(){ ...@@ -2100,10 +2084,6 @@ test ("Put", function(){
o.server.respond(); o.server.respond();
o.jio.stop(); o.jio.stop();
// do the same tests live webDav-Server/simulate CORS
// check for credentials in sinon
}); });
test ("PutAttachment", function(){ test ("PutAttachment", function(){
...@@ -2128,9 +2108,9 @@ test ("PutAttachment", function(){ ...@@ -2128,9 +2108,9 @@ test ("PutAttachment", function(){
o.clock.tick(5000); o.clock.tick(5000);
// putAttachment without underlying document => not found // putAttachment without underlying document => not found
o.addFakeServerResponse("GET", "putattmtx", 404, "HTML RESPONSE"); o.addFakeServerResponse("GET", "putattmtx", 22, "HTML RESPONSE");
o.spy(o, "status", 404, "PutAttachment without document"); o.spy(o, "status", 22, "PutAttachment without document");
o.jio.putAttachment({"id": "putattmtx/putattmt2"}, o.f); o.jio.putAttachment({"id": "putattmtx.putattmt2"}, o.f);
o.clock.tick(5000); o.clock.tick(5000);
o.server.respond(); o.server.respond();
...@@ -2138,34 +2118,25 @@ test ("PutAttachment", function(){ ...@@ -2138,34 +2118,25 @@ test ("PutAttachment", function(){
o.answer = JSON.stringify({"_id": "putattmt1", "title": "myPutAttm1"}); o.answer = JSON.stringify({"_id": "putattmt1", "title": "myPutAttm1"});
o.addFakeServerResponse("GET", "putattmt1", 200, o.answer); o.addFakeServerResponse("GET", "putattmt1", 200, o.answer);
o.addFakeServerResponse("PUT", "putattmt1", 201, "HTML RESPONSE"); o.addFakeServerResponse("PUT", "putattmt1", 201, "HTML RESPONSE");
o.addFakeServerResponse("PUT", "putattmt1/putattmt2", 201,"HTML RESPONSE"); o.addFakeServerResponse("PUT", "putattmt1.putattmt2", 201,"HTML RESPONSE");
o.spy(o, "value", {"ok": true, "id": "putattmt1/putattmt2"}, o.spy(o, "value", {"ok": true, "id": "putattmt1/putattmt2"},
"PutAttachment with document, without data"); "PutAttachment with document, without data");
o.jio.putAttachment({"id": "putattmt1/putattmt2"}, o.f); o.jio.putAttachment({"id": "putattmt1/putattmt2"}, o.f);
o.clock.tick(5000); o.clock.tick(5000);
o.server.respond(); o.server.respond();
// check document
// check attachment
// update attachment // update attachment
o.answer = JSON.stringify({"_id": "putattmt1", "title": "myPutAttm1"}); o.answer = JSON.stringify({"_id": "putattmt1", "title": "myPutAttm1"});
o.addFakeServerResponse("GET", "putattmt1", 200, o.answer); o.addFakeServerResponse("GET", "putattmt1", 200, o.answer);
o.addFakeServerResponse("PUT", "putattmt1", 201, "HTML RESPONSE"); o.addFakeServerResponse("PUT", "putattmt1", 201, "HTML RESPONSE");
o.addFakeServerResponse("PUT", "putattmt1/putattmt2", 201,"HTML RESPONSE"); o.addFakeServerResponse("PUT", "putattmt1.putattmt2", 201,"HTML RESPONSE");
o.spy(o, "value", {"ok": true, "id": "putattmt1/putattmt2"}, o.spy(o, "value", {"ok": true, "id": "putattmt1/putattmt2"},
"Update Attachment, with data"); "Update Attachment, with data");
o.jio.putAttachment({"id": "putattmt1/putattmt2", "data": "abc"}, o.f); o.jio.putAttachment({"id": "putattmt1/putattmt2", "data": "abc"}, o.f);
o.clock.tick(5000); o.clock.tick(5000);
o.server.respond(); o.server.respond();
// check document
// check attachment
o.jio.stop(); o.jio.stop();
// do the same tests live webDav-Server/simulate CORS
// check for credentials in sinon
}); });
test ("Get", function(){ test ("Get", function(){
...@@ -2187,7 +2158,7 @@ test ("Get", function(){ ...@@ -2187,7 +2158,7 @@ test ("Get", function(){
o.server.respond(); o.server.respond();
// get inexistent attachment // get inexistent attachment
o.addFakeServerResponse("GET", "get1/get2", 404, "HTML RESPONSE"); o.addFakeServerResponse("GET", "get1.get2", 404, "HTML RESPONSE");
o.spy(o, "status", 404, "Get non existing attachment"); o.spy(o, "status", 404, "Get non existing attachment");
o.jio.get("get1/get2", o.f); o.jio.get("get1/get2", o.f);
o.clock.tick(5000); o.clock.tick(5000);
...@@ -2202,7 +2173,7 @@ test ("Get", function(){ ...@@ -2202,7 +2173,7 @@ test ("Get", function(){
o.server.respond(); o.server.respond();
// get inexistent attachment (document exists) // get inexistent attachment (document exists)
o.addFakeServerResponse("GET", "get3/getx", 404, "HTML RESPONSE"); o.addFakeServerResponse("GET", "get3.getx", 404, "HTML RESPONSE");
o.spy(o, "status", 404, "Get non existing attachment (doc exists)"); o.spy(o, "status", 404, "Get non existing attachment (doc exists)");
o.jio.get("get3/getx", o.f); o.jio.get("get3/getx", o.f);
o.clock.tick(5000); o.clock.tick(5000);
...@@ -2210,7 +2181,7 @@ test ("Get", function(){ ...@@ -2210,7 +2181,7 @@ test ("Get", function(){
// get attachment // get attachment
o.answer = JSON.stringify({"_id": "get4", "title": "some attachment"}); o.answer = JSON.stringify({"_id": "get4", "title": "some attachment"});
o.addFakeServerResponse("GET", "get3/get4", 200, o.answer); o.addFakeServerResponse("GET", "get3.get4", 200, o.answer);
o.spy(o, "value", {"_id": "get4", "title": "some attachment"}, o.spy(o, "value", {"_id": "get4", "title": "some attachment"},
"Get attachment"); "Get attachment");
o.jio.get("get3/get4", o.f); o.jio.get("get3/get4", o.f);
...@@ -2218,9 +2189,6 @@ test ("Get", function(){ ...@@ -2218,9 +2189,6 @@ test ("Get", function(){
o.server.respond(); o.server.respond();
o.jio.stop(); o.jio.stop();
// do the same tests live webDav-Server/simulate CORS
// check for credentials in sinon
}); });
test ("Remove", function(){ test ("Remove", function(){
...@@ -2242,7 +2210,7 @@ test ("Remove", function(){ ...@@ -2242,7 +2210,7 @@ test ("Remove", function(){
o.server.respond(); o.server.respond();
// remove inexistent document/attachment // remove inexistent document/attachment
o.addFakeServerResponse("GET", "remove1/remove2", 404, "HTML RESPONSE"); o.addFakeServerResponse("GET", "remove1.remove2", 404, "HTML RESPONSE");
o.spy(o, "status", 404, "Remove inexistent document/attachment"); o.spy(o, "status", 404, "Remove inexistent document/attachment");
o.jio.remove({"_id": "remove1/remove2"}, o.f); o.jio.remove({"_id": "remove1/remove2"}, o.f);
o.clock.tick(5000); o.clock.tick(5000);
...@@ -2251,7 +2219,7 @@ test ("Remove", function(){ ...@@ -2251,7 +2219,7 @@ test ("Remove", function(){
// remove document // remove document
o.answer = JSON.stringify({"_id": "remove3", "title": "some doc"}); o.answer = JSON.stringify({"_id": "remove3", "title": "some doc"});
o.addFakeServerResponse("GET", "remove3", 200, o.answer); o.addFakeServerResponse("GET", "remove3", 200, o.answer);
o.addFakeServerResponse("REMOVE", "remove3", 200, "HTML RESPONSE"); o.addFakeServerResponse("DELETE", "remove3", 200, "HTML RESPONSE");
o.spy(o, "value", {"ok": true, "id": "remove3"}, "Remove document"); o.spy(o, "value", {"ok": true, "id": "remove3"}, "Remove document");
o.jio.remove({"_id": "remove3"}, o.f); o.jio.remove({"_id": "remove3"}, o.f);
o.clock.tick(5000); o.clock.tick(5000);
...@@ -2270,7 +2238,7 @@ test ("Remove", function(){ ...@@ -2270,7 +2238,7 @@ test ("Remove", function(){
// remove attachment // remove attachment
o.addFakeServerResponse("GET", "remove4", 200, o.answer); o.addFakeServerResponse("GET", "remove4", 200, o.answer);
o.addFakeServerResponse("PUT", "remove4", 201, "HTML RESPONSE"); o.addFakeServerResponse("PUT", "remove4", 201, "HTML RESPONSE");
o.addFakeServerResponse("REMOVE", "remove4/remove5", 200, "HTML RESPONSE"); o.addFakeServerResponse("DELETE", "remove4.remove5", 200, "HTML RESPONSE");
o.spy(o, "value", {"ok": true, "id": "remove4/remove5"}, o.spy(o, "value", {"ok": true, "id": "remove4/remove5"},
"Remove attachment"); "Remove attachment");
o.jio.remove({"_id": "remove4/remove5"}, o.f); o.jio.remove({"_id": "remove4/remove5"}, o.f);
...@@ -2297,10 +2265,10 @@ test ("Remove", function(){ ...@@ -2297,10 +2265,10 @@ test ("Remove", function(){
}); });
// remove document with multiple attachments // remove document with multiple attachments
o.addFakeServerResponse("GET", "remove6", 200, o.answer); o.addFakeServerResponse("GET", "remove6", 200, o.answer);
o.addFakeServerResponse("REMOVE", "remove6/remove7", 200, "HTML RESPONSE"); o.addFakeServerResponse("DELETE", "remove6.remove7", 200, "HTML RESPONSE");
o.addFakeServerResponse("REMOVE", "remove6/remove8", 200, "HTML RESPONSE"); o.addFakeServerResponse("DELETE", "remove6.remove8", 200, "HTML RESPONSE");
o.addFakeServerResponse("REMOVE", "remove6/remove9", 200, "HTML RESPONSE"); o.addFakeServerResponse("DELETE", "remove6.remove9", 200, "HTML RESPONSE");
o.addFakeServerResponse("REMOVE", "remove6", 200, "HTML RESPONSE"); o.addFakeServerResponse("DELETE", "remove6", 200, "HTML RESPONSE");
o.spy(o, "value", {"ok": true, "id": "remove6"}, o.spy(o, "value", {"ok": true, "id": "remove6"},
"Remove document with multiple attachments"); "Remove document with multiple attachments");
o.jio.remove({"_id": "remove6"}, o.f); o.jio.remove({"_id": "remove6"}, o.f);
...@@ -2308,9 +2276,6 @@ test ("Remove", function(){ ...@@ -2308,9 +2276,6 @@ test ("Remove", function(){
o.server.respond(); o.server.respond();
o.jio.stop(); o.jio.stop();
// do the same tests live webDav-Server/simulate CORS
// check for credentials in sinon
}); });
test ("AllDocs", function () { test ("AllDocs", function () {
...@@ -2341,21 +2306,82 @@ test ("AllDocs", function () { ...@@ -2341,21 +2306,82 @@ test ("AllDocs", function () {
o.server.respond(); o.server.respond();
// allDocs with option include // allDocs with option include
o.all1 = JSON.stringify({"_id": "allDocs1", "title": "a doc title"}); o.all1 = {"_id": "allDocs1", "title": "a doc title"};
o.all2 = JSON.stringify({"_id": "allDocs2", "title": "another doc title"}); o.all2 = {"_id": "allDocs2", "title": "another doc title"};
o.addFakeServerResponse("GET", "alldocs1", 200, o.all1); o.thisShouldBeTheAnswer = {
o.addFakeServerResponse("GET", "alldocs2", 200, o.all2); "rows": [
{"id": "alldocs1", "key": "alldocs1", "value": {}, "doc": o.all1},
{"id": "alldocs2", "key": "alldocs2", "value": {}, "doc": o.all2}
],
"total_rows": 2
}
o.addFakeServerResponse("GET", "alldocs1", 200, JSON.stringify(o.all1));
o.addFakeServerResponse("GET", "alldocs2", 200, JSON.stringify(o.all2));
o.spy(o, "value", o.thisShouldBeTheAnswer, "allDocs (include_docs)"); o.spy(o, "value", o.thisShouldBeTheAnswer, "allDocs (include_docs)");
o.jio.allDocs({"include_docs":true}, o.f); o.jio.allDocs({"include_docs":true}, o.f);
o.clock.tick(5000); o.clock.tick(5000);
o.server.respond(); o.server.respond();
// do the same tests live webDav-Server/simulate CORS
// check for credentials in sinon
o.jio.stop(); o.jio.stop();
}); });
// NOTES: this test is for a live webDav server on localstorage
// see the documentation how to setup an apache2 webDav-server
// tests cannot be run subsequently, so only do one test at a time
/*
test ("webDav Live Server setup", function () {
var o = generateTools(this);
// turn off fakeserver - otherwise no requests will be made
o.server.restore();
o.jio = JIO.newJio({
"type": "dav",
"username": "davlive",
"password": "checkpwd",
"url": "http://127.0.1.1/dav"
});
// not used, check console for responses
// o.spy(o, "value", {"id": "_id_", "ok": true}, "Live Webdav");
// post a new document
o.jio.post({"_id": "one.json", "title": "hello"}), o.f);
o.clock.tick(5000);
// modify document
o.jio.put({"_id": "one.json", "title": "hello modified"}), o.f);
o.clock.tick(5000);
// add attachment
o.jio.putAttachment({
"id": "one.json/att.txt",
"mimetype": "text/plain",
"content":"there2"
}, o.f);
// test allDocs
o.jio.allDocs({"include_docs":true},
function(s){console.log(s);},
function ( e ) {console.log(e);
}, o.f);
o.clock.tick(5000);
// get Attachment
o.jio.get("one.json/att.txt", o.f);
o.clock.tick(5000);
// remove Attachment
o.jio.remove("one.json/att.txt", o.f.);
o.clock.tick(5000);
// remove Document
o.jio.remove("one.json", o.f.);
o.clock.tick(5000);
o.jio.stop();
});
*/
/* /*
module ('Jio ReplicateStorage'); module ('Jio ReplicateStorage');
......
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