Commit 3dbe62ac authored by Tristan Cavelier's avatar Tristan Cavelier

Trailling spaces removed

parent 52f4a62d
/* /*
* A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
* in FIPS PUB 180-1 * in FIPS PUB 180-1
* Version 2.1a Copyright Paul Johnston 2000 - 2002. * Version 2.1a Copyright Paul Johnston 2000 - 2002.
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
* Distributed under the BSD License * Distributed under the BSD License
* See http://pajhome.org.uk/crypt/md5 for details. * See http://pajhome.org.uk/crypt/md5 for details.
*/ */
/* /*
* Configurable variables. You may need to tweak these to be compatible with * Configurable variables. You may need to tweak these to be compatible with
* the server-side, but the defaults work in most cases. * the server-side, but the defaults work in most cases.
*/ */
var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */ var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
var b64pad = "="; /* base-64 pad character. "=" for strict RFC compliance */ var b64pad = "="; /* base-64 pad character. "=" for strict RFC compliance */
var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */ var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */
/* /*
* These are the functions you'll usually want to call * These are the functions you'll usually want to call
* They take string arguments and return either hex or base-64 encoded strings * They take string arguments and return either hex or base-64 encoded strings
*/ */
function hex_sha1(s){return binb2hex(core_sha1(str2binb(s),s.length * chrsz));} function hex_sha1(s){return binb2hex(core_sha1(str2binb(s),s.length * chrsz));}
function b64_sha1(s){return binb2b64(core_sha1(str2binb(s),s.length * chrsz));} function b64_sha1(s){return binb2b64(core_sha1(str2binb(s),s.length * chrsz));}
function str_sha1(s){return binb2str(core_sha1(str2binb(s),s.length * chrsz));} function str_sha1(s){return binb2str(core_sha1(str2binb(s),s.length * chrsz));}
function hex_hmac_sha1(key, data){ return binb2hex(core_hmac_sha1(key, data));} function hex_hmac_sha1(key, data){ return binb2hex(core_hmac_sha1(key, data));}
function b64_hmac_sha1(key, data){ return binb2b64(core_hmac_sha1(key, data));} function b64_hmac_sha1(key, data){ return binb2b64(core_hmac_sha1(key, data));}
function str_hmac_sha1(key, data){ return binb2str(core_hmac_sha1(key, data));} function str_hmac_sha1(key, data){ return binb2str(core_hmac_sha1(key, data));}
/* /*
* Perform a simple self-test to see if the VM is working * Perform a simple self-test to see if the VM is working
*/ */
function sha1_vm_test() function sha1_vm_test()
{ {
return hex_sha1("abc") == "a9993e364706816aba3e25717850c26c9cd0d89d"; return hex_sha1("abc") == "a9993e364706816aba3e25717850c26c9cd0d89d";
} }
/* /*
* Calculate the SHA-1 of an array of big-endian words, and a bit length * Calculate the SHA-1 of an array of big-endian words, and a bit length
*/ */
function core_sha1(x, len) function core_sha1(x, len)
{ {
/* append padding */ /* append padding */
x[len >> 5] |= 0x80 << (24 - len % 32); x[len >> 5] |= 0x80 << (24 - len % 32);
x[((len + 64 >> 9) << 4) + 15] = len; x[((len + 64 >> 9) << 4) + 15] = len;
var w = Array(80); var w = Array(80);
var a = 1732584193; var a = 1732584193;
var b = -271733879; var b = -271733879;
var c = -1732584194; var c = -1732584194;
var d = 271733878; var d = 271733878;
var e = -1009589776; var e = -1009589776;
for(var i = 0; i < x.length; i += 16) for(var i = 0; i < x.length; i += 16)
{ {
var olda = a; var olda = a;
var oldb = b; var oldb = b;
var oldc = c; var oldc = c;
var oldd = d; var oldd = d;
var olde = e; var olde = e;
for(var j = 0; j < 80; j++) for(var j = 0; j < 80; j++)
{ {
if(j < 16) w[j] = x[i + j]; if(j < 16) w[j] = x[i + j];
else w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1); else w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
var t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)), var t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)),
safe_add(safe_add(e, w[j]), sha1_kt(j))); safe_add(safe_add(e, w[j]), sha1_kt(j)));
e = d; e = d;
d = c; d = c;
c = rol(b, 30); c = rol(b, 30);
b = a; b = a;
a = t; a = t;
} }
a = safe_add(a, olda); a = safe_add(a, olda);
b = safe_add(b, oldb); b = safe_add(b, oldb);
c = safe_add(c, oldc); c = safe_add(c, oldc);
d = safe_add(d, oldd); d = safe_add(d, oldd);
e = safe_add(e, olde); e = safe_add(e, olde);
} }
return Array(a, b, c, d, e); return Array(a, b, c, d, e);
} }
/* /*
* Perform the appropriate triplet combination function for the current * Perform the appropriate triplet combination function for the current
* iteration * iteration
*/ */
function sha1_ft(t, b, c, d) function sha1_ft(t, b, c, d)
{ {
if(t < 20) return (b & c) | ((~b) & d); if(t < 20) return (b & c) | ((~b) & d);
if(t < 40) return b ^ c ^ d; if(t < 40) return b ^ c ^ d;
if(t < 60) return (b & c) | (b & d) | (c & d); if(t < 60) return (b & c) | (b & d) | (c & d);
return b ^ c ^ d; return b ^ c ^ d;
} }
/* /*
* Determine the appropriate additive constant for the current iteration * Determine the appropriate additive constant for the current iteration
*/ */
function sha1_kt(t) function sha1_kt(t)
{ {
return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 : return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 :
(t < 60) ? -1894007588 : -899497514; (t < 60) ? -1894007588 : -899497514;
} }
/* /*
* Calculate the HMAC-SHA1 of a key and some data * Calculate the HMAC-SHA1 of a key and some data
*/ */
function core_hmac_sha1(key, data) function core_hmac_sha1(key, data)
{ {
var bkey = str2binb(key); var bkey = str2binb(key);
if(bkey.length > 16) bkey = core_sha1(bkey, key.length * chrsz); if(bkey.length > 16) bkey = core_sha1(bkey, key.length * chrsz);
var ipad = Array(16), opad = Array(16); var ipad = Array(16), opad = Array(16);
for(var i = 0; i < 16; i++) for(var i = 0; i < 16; i++)
{ {
ipad[i] = bkey[i] ^ 0x36363636; ipad[i] = bkey[i] ^ 0x36363636;
opad[i] = bkey[i] ^ 0x5C5C5C5C; opad[i] = bkey[i] ^ 0x5C5C5C5C;
} }
var hash = core_sha1(ipad.concat(str2binb(data)), 512 + data.length * chrsz); var hash = core_sha1(ipad.concat(str2binb(data)), 512 + data.length * chrsz);
return core_sha1(opad.concat(hash), 512 + 160); return core_sha1(opad.concat(hash), 512 + 160);
} }
/* /*
* Add integers, wrapping at 2^32. This uses 16-bit operations internally * Add integers, wrapping at 2^32. This uses 16-bit operations internally
* to work around bugs in some JS interpreters. * to work around bugs in some JS interpreters.
*/ */
function safe_add(x, y) function safe_add(x, y)
{ {
var lsw = (x & 0xFFFF) + (y & 0xFFFF); var lsw = (x & 0xFFFF) + (y & 0xFFFF);
var msw = (x >> 16) + (y >> 16) + (lsw >> 16); var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
return (msw << 16) | (lsw & 0xFFFF); return (msw << 16) | (lsw & 0xFFFF);
} }
/* /*
* Bitwise rotate a 32-bit number to the left. * Bitwise rotate a 32-bit number to the left.
*/ */
function rol(num, cnt) function rol(num, cnt)
{ {
return (num << cnt) | (num >>> (32 - cnt)); return (num << cnt) | (num >>> (32 - cnt));
} }
/* /*
* Convert an 8-bit or 16-bit string to an array of big-endian words * Convert an 8-bit or 16-bit string to an array of big-endian words
* In 8-bit function, characters >255 have their hi-byte silently ignored. * In 8-bit function, characters >255 have their hi-byte silently ignored.
*/ */
function str2binb(str) function str2binb(str)
{ {
var bin = Array(); var bin = Array();
var mask = (1 << chrsz) - 1; var mask = (1 << chrsz) - 1;
for(var i = 0; i < str.length * chrsz; i += chrsz) for(var i = 0; i < str.length * chrsz; i += chrsz)
bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (32 - chrsz - i%32); bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (32 - chrsz - i%32);
return bin; return bin;
} }
/* /*
* Convert an array of big-endian words to a string * Convert an array of big-endian words to a string
*/ */
function binb2str(bin) function binb2str(bin)
{ {
var str = ""; var str = "";
var mask = (1 << chrsz) - 1; var mask = (1 << chrsz) - 1;
for(var i = 0; i < bin.length * 32; i += chrsz) for(var i = 0; i < bin.length * 32; i += chrsz)
str += String.fromCharCode((bin[i>>5] >>> (32 - chrsz - i%32)) & mask); str += String.fromCharCode((bin[i>>5] >>> (32 - chrsz - i%32)) & mask);
return str; return str;
} }
/* /*
* Convert an array of big-endian words to a hex string. * Convert an array of big-endian words to a hex string.
*/ */
function binb2hex(binarray) function binb2hex(binarray)
{ {
var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef"; var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
var str = ""; var str = "";
for(var i = 0; i < binarray.length * 4; i++) for(var i = 0; i < binarray.length * 4; i++)
{ {
str += hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xF) + str += hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xF) +
hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8 )) & 0xF); hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8 )) & 0xF);
} }
return str; return str;
} }
/* /*
* Convert an array of big-endian words to a base-64 string * Convert an array of big-endian words to a base-64 string
*/ */
function binb2b64(binarray) function binb2b64(binarray)
{ {
var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var str = ""; var str = "";
for(var i = 0; i < binarray.length * 4; i += 3) for(var i = 0; i < binarray.length * 4; i += 3)
{ {
var triplet = (((binarray[i >> 2] >> 8 * (3 - i %4)) & 0xFF) << 16) var triplet = (((binarray[i >> 2] >> 8 * (3 - i %4)) & 0xFF) << 16)
| (((binarray[i+1 >> 2] >> 8 * (3 - (i+1)%4)) & 0xFF) << 8 ) | (((binarray[i+1 >> 2] >> 8 * (3 - (i+1)%4)) & 0xFF) << 8 )
| ((binarray[i+2 >> 2] >> 8 * (3 - (i+2)%4)) & 0xFF); | ((binarray[i+2 >> 2] >> 8 * (3 - (i+2)%4)) & 0xFF);
for(var j = 0; j < 4; j++) for(var j = 0; j < 4; j++)
{ {
if(i * 8 + j * 6 > binarray.length * 32) str += b64pad; if(i * 8 + j * 6 > binarray.length * 32) str += b64pad;
else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F); else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F);
} }
} }
return str; return str;
} }
\ No newline at end of file
/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */ /*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */
/*global jIO: true, btoa: true, b64_hmac_sha1: true */ /*global jIO: true, btoa: true, b64_hmac_sha1: true */
/*global XMLHttpRequest: true, XHRwrapper: true, FormData: true, $: true*/ /*global XMLHttpRequest: true, XHRwrapper: true, FormData: true, $: true*/
/** /**
* JIO S3 Storage. Type = "s3". * JIO S3 Storage. Type = "s3".
* Amazon S3 "database" storage. * Amazon S3 "database" storage.
*/ */
jIO.addStorageType("s3", function (spec, my) { jIO.addStorageType("s3", function (spec, my) {
var evt, that, priv = {}; var evt, that, priv = {};
spec = spec || {}; spec = spec || {};
that = my.basicStorage(spec, my); that = my.basicStorage(spec, my);
// attributes // attributes
priv.username = spec.username || ''; priv.username = spec.username || '';
priv.AWSIdentifier = spec.AWSIdentifier || ''; priv.AWSIdentifier = spec.AWSIdentifier || '';
priv.password = spec.password || ''; priv.password = spec.password || '';
priv.server = spec.server || ''; /*|| jiobucket ||*/ priv.server = spec.server || ''; /*|| jiobucket ||*/
priv.url = spec.url || ''; /*||> https://s3-eu-west-1.amazonaws.com <||*/ priv.url = spec.url || ''; /*||> https://s3-eu-west-1.amazonaws.com <||*/
priv.acl = spec.acl || ''; priv.acl = spec.acl || '';
/*||> "private, /*||> "private,
public-read, public-read,
public-read-write, public-read-write,
authenticated-read, authenticated-read,
bucket-owner-read, bucket-owner-read,
bucket-owner-full-control" <||*/ bucket-owner-full-control" <||*/
priv.actionStatus = spec.actionStatus || ''; priv.actionStatus = spec.actionStatus || '';
priv.contenTType = spec.contenTType || ''; priv.contenTType = spec.contenTType || '';
/** /**
* Update [doc] the document object and remove [doc] keys * Update [doc] the document object and remove [doc] keys
* which are not in [new_doc]. It only changes [doc] keys not starting * which are not in [new_doc]. It only changes [doc] keys not starting
* with an underscore. * with an underscore.
* ex: doc: {key:value1,_key:value2} with * ex: doc: {key:value1,_key:value2} with
* new_doc: {key:value3,_key:value4} updates * new_doc: {key:value3,_key:value4} updates
* doc: {key:value3,_key:value2}. * doc: {key:value3,_key:value2}.
* @param {object} doc The original document object. * @param {object} doc The original document object.
* @param {object} new_doc The new document object * @param {object} new_doc The new document object
**/ **/
priv.secureDocId = function (string) { priv.secureDocId = function (string) {
var split = string.split('/'), i; var split = string.split('/'), i;
if (split[0] === '') { if (split[0] === '') {
split = split.slice(1); split = split.slice(1);
} }
for (i = 0; i < split.length; i += 1) { for (i = 0; i < split.length; i += 1) {
if (split[i] === '') { if (split[i] === '') {
return ''; return '';
} }
} }
return split.join('%2F'); return split.join('%2F');
}; };
/** /**
* Replace substrings to another strings * Replace substrings to another strings
* @method recursiveReplace * @method recursiveReplace
* @param {string} string The string to do replacement * @param {string} string The string to do replacement
* @param {array} list_of_replacement An array of couple * @param {array} list_of_replacement An array of couple
* ["substring to select", "selected substring replaced by this string"]. * ["substring to select", "selected substring replaced by this string"].
* @return {string} The replaced string * @return {string} The replaced string
*/ */
priv.recursiveReplace = function (string, list_of_replacement) { priv.recursiveReplace = function (string, list_of_replacement) {
var i, split_string = string.split(list_of_replacement[0][0]); var i, split_string = string.split(list_of_replacement[0][0]);
if (list_of_replacement[1]) { if (list_of_replacement[1]) {
for (i = 0; i < split_string.length; i += 1) { for (i = 0; i < split_string.length; i += 1) {
split_string[i] = priv.recursiveReplace( split_string[i] = priv.recursiveReplace(
split_string[i], split_string[i],
list_of_replacement.slice(1) list_of_replacement.slice(1)
); );
} }
} }
return split_string.join(list_of_replacement[0][1]); return split_string.join(list_of_replacement[0][1]);
}; };
/** /**
* Changes / to %2F, % to %25 and . to _. * Changes / to %2F, % to %25 and . to _.
* @method secureName * @method secureName
* @param {string} name The name to secure * @param {string} name The name to secure
* @return {string} The secured name * @return {string} The secured name
*/ */
priv.secureName = function (name) { priv.secureName = function (name) {
return priv.recursiveReplace(name, [["/", "%2F"], ["%", "%25"]]); return priv.recursiveReplace(name, [["/", "%2F"], ["%", "%25"]]);
}; };
/** /**
* Restores the original name from a secured name * Restores the original name from a secured name
* @method restoreName * @method restoreName
* @param {string} secured_name The secured name to restore * @param {string} secured_name The secured name to restore
* @return {string} The original name * @return {string} The original name
*/ */
priv.restoreName = function (secured_name) { priv.restoreName = function (secured_name) {
return priv.recursiveReplace(secured_name, [["%2F", "/"], ["%25", "%"]]); return priv.recursiveReplace(secured_name, [["%2F", "/"], ["%25", "%"]]);
}; };
/** /**
* Convert document id and attachment id to a file name * Convert document id and attachment id to a file name
* @method idsToFileName * @method idsToFileName
* @param {string} doc_id The document id * @param {string} doc_id The document id
* @param {string} attachment_id The attachment id (optional) * @param {string} attachment_id The attachment id (optional)
* @return {string} The file name * @return {string} The file name
*/ */
priv.idsToFileName = function (doc_id, attachment_id) { priv.idsToFileName = function (doc_id, attachment_id) {
doc_id = priv.secureName(doc_id).split(".").join("_."); doc_id = priv.secureName(doc_id).split(".").join("_.");
if (typeof attachment_id === "string") { if (typeof attachment_id === "string") {
attachment_id = priv.secureName(attachment_id).split(".").join("_."); attachment_id = priv.secureName(attachment_id).split(".").join("_.");
return doc_id + "." + attachment_id; return doc_id + "." + attachment_id;
} }
return doc_id; return doc_id;
}; };
/** /**
* Convert a file name to a document id (and attachment id if there) * Convert a file name to a document id (and attachment id if there)
* @method fileNameToIds * @method fileNameToIds
* @param {string} file_name The file name to convert * @param {string} file_name The file name to convert
* @return {array} ["document id", "attachment id"] or ["document id"] * @return {array} ["document id", "attachment id"] or ["document id"]
*/ */
priv.fileNameToIds = function (file_name) { priv.fileNameToIds = function (file_name) {
var separator_index = -1, split = file_name.split("."); var separator_index = -1, split = file_name.split(".");
split.slice(0, -1).forEach(function (file_name_part, index) { split.slice(0, -1).forEach(function (file_name_part, index) {
if (file_name_part.slice(-1) !== "_") { if (file_name_part.slice(-1) !== "_") {
separator_index = index; separator_index = index;
} }
}); });
if (separator_index === -1) { if (separator_index === -1) {
return [priv.restoreName(priv.restoreName( return [priv.restoreName(priv.restoreName(
file_name file_name
).split("_.").join("."))]; ).split("_.").join("."))];
} }
return [ return [
priv.restoreName(priv.restoreName( priv.restoreName(priv.restoreName(
split.slice(0, separator_index + 1).join(".") split.slice(0, separator_index + 1).join(".")
).split("_.").join(".")), ).split("_.").join(".")),
priv.restoreName(priv.restoreName( priv.restoreName(priv.restoreName(
split.slice(separator_index + 1).join(".") split.slice(separator_index + 1).join(".")
).split("_.").join(".")) ).split("_.").join("."))
]; ];
}; };
/** /**
* Removes the last character if it is a "/". "/a/b/c/" become "/a/b/c" * Removes the last character if it is a "/". "/a/b/c/" become "/a/b/c"
* @method removeSlashIfLast * @method removeSlashIfLast
* @param {string} string The string to modify * @param {string} string The string to modify
* @return {string} The modified string * @return {string} The modified string
*/ */
priv.removeSlashIfLast = function (string) { priv.removeSlashIfLast = function (string) {
if (string[string.length - 1] === "/") { if (string[string.length - 1] === "/") {
return string.slice(0, -1); return string.slice(0, -1);
} }
return string; return string;
}; };
that.documentObjectUpdate = function (doc, new_doc) { that.documentObjectUpdate = function (doc, new_doc) {
var k; var k;
for (k in doc) { for (k in doc) {
if (doc.hasOwnProperty(k)) { if (doc.hasOwnProperty(k)) {
if (k[0] !== '_') { if (k[0] !== '_') {
delete doc[k]; delete doc[k];
} }
} }
} }
for (k in new_doc) { for (k in new_doc) {
if (new_doc.hasOwnProperty(k)) { if (new_doc.hasOwnProperty(k)) {
if (k[0] !== '_') { if (k[0] !== '_') {
doc[k] = new_doc[k]; doc[k] = new_doc[k];
} }
} }
} }
}; };
/** /**
* Checks if an object has no enumerable keys * Checks if an object has no enumerable keys
* @method objectIsEmpty * @method objectIsEmpty
* @param {object} obj The object * @param {object} obj The object
* @return {boolean} true if empty, else false * @return {boolean} true if empty, else false
*/ */
that.objectIsEmpty = function (obj) { that.objectIsEmpty = function (obj) {
var k; var k;
for (k in obj) { for (k in obj) {
if (obj.hasOwnProperty(k)) { if (obj.hasOwnProperty(k)) {
return false; return false;
} }
} }
return true; return true;
}; };
// ===================== overrides ====================== // ===================== overrides ======================
that.specToStore = function () { that.specToStore = function () {
return { return {
"username": priv.username, "username": priv.username,
"password": priv.password, "password": priv.password,
"url": priv.url, "url": priv.url,
"server": priv.server, "server": priv.server,
"acl": priv.acl "acl": priv.acl
}; };
}; };
that.validateState = function () { that.validateState = function () {
// xxx complete error message // xxx complete error message
// jjj completion below // jjj completion below
if (typeof priv.AWSIdentifier === "string" && priv.AWSIdentifier === '') { if (typeof priv.AWSIdentifier === "string" && priv.AWSIdentifier === '') {
return 'Need at least one parameter "Aws login".'; return 'Need at least one parameter "Aws login".';
} }
if (typeof priv.password === "string" && priv.password === '') { if (typeof priv.password === "string" && priv.password === '') {
return 'Need at least one parameter "password".'; return 'Need at least one parameter "password".';
} }
if (typeof priv.url === "string" && priv.url === '') { if (typeof priv.url === "string" && priv.url === '') {
return 'Need at least one parameter "url".'; return 'Need at least one parameter "url".';
} }
if (typeof priv.server === "string" && priv.server === '') { if (typeof priv.server === "string" && priv.server === '') {
return 'Need at least one parameter "server".'; return 'Need at least one parameter "server".';
} }
return ''; return '';
}; };
// =================== S3 Specifics ================= // =================== S3 Specifics =================
/** /**
* Encoding the signature using a stringToSign * Encoding the signature using a stringToSign
* Encoding the policy * Encoding the policy
* @method buildStringToSign * @method buildStringToSign
* @param {string} http_verb The HTTP method * @param {string} http_verb The HTTP method
* @param {string} content_md5 The md5 content * @param {string} content_md5 The md5 content
* @param {string} content_type The content type * @param {string} content_type The content type
* @param {number} expires The expires time * @param {number} expires The expires time
* @param {string} x_amz_headers The specific amazon headers * @param {string} x_amz_headers The specific amazon headers
* @param {string} path_key The path of the document * @param {string} path_key The path of the document
* @return {string} The generated signature * @return {string} The generated signature
*/ */
// xxx no need to make it public, use private -> "priv" (not "that") // xxx no need to make it public, use private -> "priv" (not "that")
priv.buildStringToSign = function (http_verb, content_md5, content_type, priv.buildStringToSign = function (http_verb, content_md5, content_type,
expires, x_amz_headers, path_key) { expires, x_amz_headers, path_key) {
//example : //example :
// var StringToSign = S3.buildStringToSign(S3.httpVerb,'','','', // var StringToSign = S3.buildStringToSign(S3.httpVerb,'','','',
// 'x-amz-date:'+S3.requestUTC,'/jio1st/prive.json'); // 'x-amz-date:'+S3.requestUTC,'/jio1st/prive.json');
var StringToSign = http_verb + '\n' var StringToSign = http_verb + '\n'
+ content_md5 + '\n'//content-md5 + content_md5 + '\n'//content-md5
+ content_type + '\n'//content-type + content_type + '\n'//content-type
+ expires + '\n'//expires + expires + '\n'//expires
+ x_amz_headers + '\n'//x-amz headers + x_amz_headers + '\n'//x-amz headers
+ path_key;//path key + path_key;//path key
return StringToSign; return StringToSign;
}; };
that.encodePolicy = function (form) { that.encodePolicy = function (form) {
//generates the policy //generates the policy
//enables the choice for the http response code //enables the choice for the http response code
var http_code, s3_policy, Signature = ''; var http_code, s3_policy, Signature = '';
s3_policy = { s3_policy = {
"expiration": "2020-01-01T00:00:00Z", "expiration": "2020-01-01T00:00:00Z",
"conditions": [ "conditions": [
{"bucket": priv.server }, {"bucket": priv.server },
["starts-with", "$key", ""], ["starts-with", "$key", ""],
{"acl": priv.acl }, {"acl": priv.acl },
{"success_action_redirect": ""}, {"success_action_redirect": ""},
{"success_action_status": http_code }, {"success_action_status": http_code },
["starts-with", "$Content-Type", ""], ["starts-with", "$Content-Type", ""],
["content-length-range", 0, 524288000] ["content-length-range", 0, 524288000]
] ]
}; };
//base64 encoding of the policy (native base64 js >> //base64 encoding of the policy (native base64 js >>
// .btoa() = encode, .atob() = decode) // .btoa() = encode, .atob() = decode)
priv.b64_policy = btoa(JSON.stringify(s3_policy)); priv.b64_policy = btoa(JSON.stringify(s3_policy));
//generates the signature value using the policy and the secret access key //generates the signature value using the policy and the secret access key
//use of sha1.js to generate the signature //use of sha1.js to generate the signature
Signature = that.signature(priv.b64_policy); Signature = that.signature(priv.b64_policy);
}; };
that.signature = function (string) { that.signature = function (string) {
var Signature = b64_hmac_sha1(priv.password, string); var Signature = b64_hmac_sha1(priv.password, string);
return Signature; return Signature;
}; };
function xhr_onreadystatechange(docId, function xhr_onreadystatechange(docId,
command, command,
obj, obj,
http, http,
jio, jio,
isAttachment, isAttachment,
callback) { callback) {
obj.onreadystatechange = function () { obj.onreadystatechange = function () {
var response, err = ''; var response, err = '';
if (obj.readyState === 4) { if (obj.readyState === 4) {
if (this.status === 204 || this.status === 201 || this.status === 200) { if (this.status === 204 || this.status === 201 || this.status === 200) {
switch (http) { switch (http) {
case "POST": case "POST":
that.success({ that.success({
ok: true, ok: true,
id: docId id: docId
}); });
break; break;
case 'PUT': case 'PUT':
if (jio === true) { if (jio === true) {
that.success({ that.success({
ok: true, ok: true,
id: command.getDocId() id: command.getDocId()
}); });
} else { } else {
callback(this.responseText); callback(this.responseText);
} }
break; break;
case 'GET': case 'GET':
if (jio === true) { if (jio === true) {
if (typeof this.responseText !== 'string') { if (typeof this.responseText !== 'string') {
response = JSON.parse(this.responseText); response = JSON.parse(this.responseText);
response._attachments = response._attachments || {}; response._attachments = response._attachments || {};
delete response._attachments; delete response._attachments;
that.success(JSON.stringify(response)); that.success(JSON.stringify(response));
} else { } else {
if (isAttachment === true) { if (isAttachment === true) {
that.success(this.responseText); that.success(this.responseText);
} else { } else {
that.success(JSON.parse(this.responseText)); that.success(JSON.parse(this.responseText));
} }
} }
} else { } else {
callback(this.responseText); callback(this.responseText);
} }
break; break;
case 'DELETE': case 'DELETE':
if (jio === true) { if (jio === true) {
if (isAttachment === false) { if (isAttachment === false) {
that.success({ that.success({
ok: true, ok: true,
id: command.getDocId() id: command.getDocId()
}); });
} else { } else {
that.success({ that.success({
ok: true, ok: true,
id: command.getDocId(), id: command.getDocId(),
attachment: command.getAttachmentId() attachment: command.getAttachmentId()
}); });
} }
} else { } else {
callback(this.responseText); callback(this.responseText);
} }
break; break;
} }
} else { } else {
err = this; err = this;
if (this.status === 405) { if (this.status === 405) {
//status //status
//statustext "Not Found" //statustext "Not Found"
//error //error
//reason "reason" //reason "reason"
//message "did not work" //message "did not work"
err.error = "not_allowed"; err.error = "not_allowed";
that.error(err); that.error(err);
} }
if (this.status === 404) { if (this.status === 404) {
if (http === 'GET') { if (http === 'GET') {
if (jio === true) { if (jio === true) {
//status //status
//statustext "Not Found" //statustext "Not Found"
//error //error
//reason "reason" //reason "reason"
//message "did not work" //message "did not work"
err.statustext = "not_foud"; err.statustext = "not_foud";
err.reason = "file does not exist"; err.reason = "file does not exist";
err.error = "not_found"; err.error = "not_found";
that.error(err); that.error(err);
} else { } else {
callback('404'); callback('404');
} }
} else { } else {
//status //status
//statustext "Not Found" //statustext "Not Found"
//error //error
//reason "reason" //reason "reason"
//message "did not work" //message "did not work"
err.error = "not_found"; err.error = "not_found";
that.error(err); that.error(err);
} }
} }
if (this.status === 409) { if (this.status === 409) {
//status //status
//statustext "Not Found" //statustext "Not Found"
//error //error
//reason "reason" //reason "reason"
//message "did not work" //message "did not work"
err.error = "already_exists"; err.error = "already_exists";
that.error(err); that.error(err);
} }
} }
} }
}; };
} }
priv.updateMeta = function (doc, docid, attachid, action, data) { priv.updateMeta = function (doc, docid, attachid, action, data) {
doc._attachments = doc._attachments || {}; doc._attachments = doc._attachments || {};
switch (action) { switch (action) {
case "add": case "add":
doc._attachments[attachid] = data; doc._attachments[attachid] = data;
//nothing happens //nothing happens
doc = JSON.stringify(doc); doc = JSON.stringify(doc);
break; break;
case "remove": case "remove":
if (doc._attachments !== undefined) { if (doc._attachments !== undefined) {
delete doc._attachments[attachid]; delete doc._attachments[attachid];
} }
doc = JSON.stringify(doc); doc = JSON.stringify(doc);
break; break;
case "update": case "update":
console.log(doc._attachments); console.log(doc._attachments);
doc._attachments[attachid] = data; doc._attachments[attachid] = data;
console.log(doc._attachments); console.log(doc._attachments);
//update happened in the put request //update happened in the put request
doc = JSON.stringify(doc); doc = JSON.stringify(doc);
break; break;
} }
return doc; return doc;
}; };
priv.createError = function (status, message, reason) { priv.createError = function (status, message, reason) {
var error = { var error = {
"status": status, "status": status,
"message": message, "message": message,
"reason": reason "reason": reason
}; };
switch (status) { switch (status) {
case 404: case 404:
error.statusText = "Not found"; error.statusText = "Not found";
break; break;
case 405: case 405:
error.statusText = "Method Not Allowed"; error.statusText = "Method Not Allowed";
break; break;
case 409: case 409:
error.statusText = "Conflicts"; error.statusText = "Conflicts";
break; break;
case 24: case 24:
error.statusText = "Corrupted Document"; error.statusText = "Corrupted Document";
break; break;
} }
error.error = error.statusText.toLowerCase().split(" ").join("_"); error.error = error.statusText.toLowerCase().split(" ").join("_");
return error; return error;
}; };
that.encodeAuthorization = function (key, mime) { that.encodeAuthorization = function (key, mime) {
//GET oriented method //GET oriented method
var requestUTC, httpVerb, StringToSign, Signature; var requestUTC, httpVerb, StringToSign, Signature;
requestUTC = new Date().toUTCString(); requestUTC = new Date().toUTCString();
httpVerb = "GET"; httpVerb = "GET";
StringToSign = priv.buildStringToSign( StringToSign = priv.buildStringToSign(
httpVerb, httpVerb,
'', '',
'application/json', 'application/json',
'', '',
'x-amz-date:' + requestUTC, 'x-amz-date:' + requestUTC,
'/' + priv.server + '/' + key '/' + priv.server + '/' + key
); );
Signature = b64_hmac_sha1(priv.password, StringToSign); Signature = b64_hmac_sha1(priv.password, StringToSign);
return Signature; return Signature;
}; };
that.XHRwrapper = function (command, that.XHRwrapper = function (command,
docId, docId,
attachId, attachId,
http, http,
mime, mime,
data, data,
jio, jio,
is_attachment, is_attachment,
callback) { callback) {
var docFile, requestUTC, StringToSign, url, Signature, xhr; var docFile, requestUTC, StringToSign, url, Signature, xhr;
docFile = priv.secureName(priv.idsToFileName(docId, docFile = priv.secureName(priv.idsToFileName(docId,
attachId || undefined)); attachId || undefined));
requestUTC = new Date().toUTCString(); requestUTC = new Date().toUTCString();
StringToSign = priv.buildStringToSign( StringToSign = priv.buildStringToSign(
http, http,
'', '',
mime, mime,
'', '',
'x-amz-date:' + requestUTC, 'x-amz-date:' + requestUTC,
'/' + priv.server + '/' + docFile '/' + priv.server + '/' + docFile
); );
url = 'http://s3.amazonaws.com/' + priv.server + '/' + docFile; url = 'http://s3.amazonaws.com/' + priv.server + '/' + docFile;
Signature = b64_hmac_sha1(priv.password, StringToSign); Signature = b64_hmac_sha1(priv.password, StringToSign);
xhr = new XMLHttpRequest(); xhr = new XMLHttpRequest();
xhr.open(http, url, true); xhr.open(http, url, true);
xhr.setRequestHeader("HTTP-status-code", "100"); xhr.setRequestHeader("HTTP-status-code", "100");
xhr.setRequestHeader("x-amz-date", requestUTC); xhr.setRequestHeader("x-amz-date", requestUTC);
xhr.setRequestHeader("Authorization", "AWS " xhr.setRequestHeader("Authorization", "AWS "
+ priv.AWSIdentifier + priv.AWSIdentifier
+ ":" + ":"
+ Signature); + Signature);
xhr.setRequestHeader("Content-Type", mime); xhr.setRequestHeader("Content-Type", mime);
xhr.responseType = 'text'; xhr.responseType = 'text';
xhr_onreadystatechange(docId, xhr_onreadystatechange(docId,
command, command,
xhr, xhr,
http, http,
jio, jio,
is_attachment, is_attachment,
callback); callback);
if (http === 'PUT') { if (http === 'PUT') {
xhr.send(data); xhr.send(data);
} else { } else {
xhr.send(null); xhr.send(null);
} }
}; };
// ==================== commands ==================== // ==================== commands ====================
/** /**
* Create a document in local storage. * Create a document in local storage.
* @method post * @method post
* @param {object} command The JIO command * @param {object} command The JIO command
**/ **/
that.post = function (command) { that.post = function (command) {
//as S3 encoding key are directly inserted within the FormData(), //as S3 encoding key are directly inserted within the FormData(),
//use of XHRwrapper function ain't pertinent //use of XHRwrapper function ain't pertinent
var doc, doc_id, mime; var doc, doc_id, mime;
doc = command.cloneDoc(); doc = command.cloneDoc();
doc_id = command.getDocId(); doc_id = command.getDocId();
function postDocument() { function postDocument() {
var http_response, fd, Signature, xhr; var http_response, fd, Signature, xhr;
doc_id = priv.secureName(priv.idsToFileName(doc_id)); doc_id = priv.secureName(priv.idsToFileName(doc_id));
//Meant to deep-serialize in order to avoid //Meant to deep-serialize in order to avoid
//conflicts due to the multipart enctype //conflicts due to the multipart enctype
doc = JSON.stringify(doc); doc = JSON.stringify(doc);
http_response = ''; http_response = '';
fd = new FormData(); fd = new FormData();
//virtually builds the form fields //virtually builds the form fields
//filename //filename
fd.append('key', doc_id); fd.append('key', doc_id);
//file access authorizations //file access authorizations
priv.acl = ""; priv.acl = "";
fd.append('acl', priv.acl); fd.append('acl', priv.acl);
//content-type //content-type
priv.contenTType = "text/plain"; priv.contenTType = "text/plain";
fd.append('Content-Type', priv.contenTType); fd.append('Content-Type', priv.contenTType);
//allows specification of a success url redirection //allows specification of a success url redirection
fd.append('success_action_redirect', ''); fd.append('success_action_redirect', '');
//allows to specify the http code response if the request is successful //allows to specify the http code response if the request is successful
fd.append('success_action_status', http_response); fd.append('success_action_status', http_response);
//login AWS //login AWS
fd.append('AWSAccessKeyId', priv.AWSIdentifier); fd.append('AWSAccessKeyId', priv.AWSIdentifier);
//exchange policy with the amazon s3 service //exchange policy with the amazon s3 service
//can be common to all uploads or specific //can be common to all uploads or specific
that.encodePolicy(fd); that.encodePolicy(fd);
//priv.b64_policy = that.encodePolicy(fd); //priv.b64_policy = that.encodePolicy(fd);
fd.append('policy', priv.b64_policy); fd.append('policy', priv.b64_policy);
//signature through the base64.hmac.sha1(secret key, policy) method //signature through the base64.hmac.sha1(secret key, policy) method
Signature = b64_hmac_sha1(priv.password, priv.b64_policy); Signature = b64_hmac_sha1(priv.password, priv.b64_policy);
fd.append('signature', Signature); fd.append('signature', Signature);
//uploaded content !!may must be a string rather than an object //uploaded content !!may must be a string rather than an object
fd.append('file', doc); fd.append('file', doc);
xhr = new XMLHttpRequest(); xhr = new XMLHttpRequest();
xhr_onreadystatechange(doc_id, command, xhr, 'POST', true, false, ''); xhr_onreadystatechange(doc_id, command, xhr, 'POST', true, false, '');
xhr.open('POST', 'https://' + priv.server + '.s3.amazonaws.com/', true); xhr.open('POST', 'https://' + priv.server + '.s3.amazonaws.com/', true);
xhr.send(fd); xhr.send(fd);
} }
if (doc_id === '' || doc_id === undefined) { if (doc_id === '' || doc_id === undefined) {
doc_id = 'no_document_id_' doc_id = 'no_document_id_'
+ ((Math.random() * 10).toString().split('.'))[1]; + ((Math.random() * 10).toString().split('.'))[1];
doc._id = doc_id; doc._id = doc_id;
} }
mime = 'text/plain; charset=UTF-8'; mime = 'text/plain; charset=UTF-8';
that.XHRwrapper(command, doc_id, '', 'GET', mime, '', false, false, that.XHRwrapper(command, doc_id, '', 'GET', mime, '', false, false,
function (response) { function (response) {
if (response === '404') { if (response === '404') {
postDocument(); postDocument();
} else { } else {
//si ce n'est pas une 404, //si ce n'est pas une 404,
//alors on renvoit une erreur 405 //alors on renvoit une erreur 405
return that.error(priv.createError( return that.error(priv.createError(
409, 409,
"Cannot create document", "Cannot create document",
"Document already exists" "Document already exists"
)); ));
} }
} }
); );
}; };
/** /**
* Get a document or attachment * Get a document or attachment
* @method get * @method get
* @param {object} command The JIO command * @param {object} command The JIO command
**/ **/
that.get = function (command) { that.get = function (command) {
var docId, attachId, isJIO, mime; var docId, attachId, isJIO, mime;
docId = command.getDocId(); docId = command.getDocId();
attachId = command.getAttachmentId() || ''; attachId = command.getAttachmentId() || '';
isJIO = true; isJIO = true;
mime = 'text/plain; charset=UTF-8'; mime = 'text/plain; charset=UTF-8';
that.XHRwrapper(command, docId, attachId, 'GET', mime, '', isJIO, false); that.XHRwrapper(command, docId, attachId, 'GET', mime, '', isJIO, false);
}; };
that.getAttachment = function (command) { that.getAttachment = function (command) {
var docId, attachId, isJIO, mime; var docId, attachId, isJIO, mime;
docId = command.getDocId(); docId = command.getDocId();
attachId = command.getAttachmentId(); attachId = command.getAttachmentId();
isJIO = true; isJIO = true;
mime = 'text/plain; charset=UTF-8'; mime = 'text/plain; charset=UTF-8';
that.XHRwrapper(command, docId, attachId, 'GET', mime, '', isJIO, true); that.XHRwrapper(command, docId, attachId, 'GET', mime, '', isJIO, true);
}; };
/** /**
* Create or update a document in local storage. * Create or update a document in local storage.
* @method put * @method put
* @param {object} command The JIO command * @param {object} command The JIO command
**/ **/
that.put = function (command) { that.put = function (command) {
var doc, docId, mime; var doc, docId, mime;
doc = command.cloneDoc(); doc = command.cloneDoc();
docId = command.getDocId(); docId = command.getDocId();
mime = 'text/plain; charset=UTF-8'; mime = 'text/plain; charset=UTF-8';
//pas d'attachment dans un put simple //pas d'attachment dans un put simple
function putDocument() { function putDocument() {
var attachId, data, isJIO; var attachId, data, isJIO;
attachId = ''; attachId = '';
data = JSON.stringify(doc); data = JSON.stringify(doc);
isJIO = true; isJIO = true;
that.XHRwrapper(command, that.XHRwrapper(command,
docId, docId,
attachId, attachId,
'PUT', 'PUT',
mime, mime,
data, data,
isJIO, isJIO,
false); false);
} }
that.XHRwrapper(command, docId, '', 'GET', mime, '', false, false, that.XHRwrapper(command, docId, '', 'GET', mime, '', false, false,
function (response) { function (response) {
//if (response === '404') {} //if (response === '404') {}
if (response._attachments !== undefined) { if (response._attachments !== undefined) {
doc._attachments = response._attachments; doc._attachments = response._attachments;
} }
putDocument(); putDocument();
} }
); );
}; };
that.putAttachment = function (command) { that.putAttachment = function (command) {
var mon_document, var mon_document,
docId, docId,
attachId, attachId,
mime, mime,
attachment_id, attachment_id,
attachment_data, attachment_data,
attachment_md5, attachment_md5,
attachment_mimetype, attachment_mimetype,
attachment_length; attachment_length;
mon_document = null; mon_document = null;
docId = command.getDocId(); docId = command.getDocId();
attachId = command.getAttachmentId() || ''; attachId = command.getAttachmentId() || '';
mime = 'text/plain; charset=UTF-8'; mime = 'text/plain; charset=UTF-8';
//récupération des variables de l'attachement //récupération des variables de l'attachement
attachment_id = command.getAttachmentId(); attachment_id = command.getAttachmentId();
attachment_data = command.getAttachmentData(); attachment_data = command.getAttachmentData();
attachment_md5 = command.md5SumAttachmentData(); attachment_md5 = command.md5SumAttachmentData();
attachment_mimetype = command.getAttachmentMimeType(); attachment_mimetype = command.getAttachmentMimeType();
attachment_length = command.getAttachmentLength(); attachment_length = command.getAttachmentLength();
function putAttachment() { function putAttachment() {
that.XHRwrapper(command, that.XHRwrapper(command,
docId, docId,
attachId, attachId,
'PUT', 'PUT',
mime, mime,
attachment_data, attachment_data,
false, false,
true, true,
function (reponse) { function (reponse) {
that.success({ that.success({
// response // response
"ok": true, "ok": true,
"id": docId, "id": docId,
"attachment": attachId "attachment": attachId
//"rev": current_revision //"rev": current_revision
}); });
} }
); );
} }
function putDocument() { function putDocument() {
var attachment_obj, data, doc; var attachment_obj, data, doc;
attachment_obj = { attachment_obj = {
//"revpos": 3, // optional //"revpos": 3, // optional
"digest": attachment_md5, "digest": attachment_md5,
"content_type": attachment_mimetype, "content_type": attachment_mimetype,
"length": attachment_length "length": attachment_length
}; };
data = JSON.parse(mon_document); data = JSON.parse(mon_document);
doc = priv.updateMeta(data, docId, attachId, "add", attachment_obj); doc = priv.updateMeta(data, docId, attachId, "add", attachment_obj);
that.XHRwrapper(command, docId, '', 'PUT', mime, doc, false, false, that.XHRwrapper(command, docId, '', 'PUT', mime, doc, false, false,
function (reponse) { function (reponse) {
putAttachment(); putAttachment();
} }
); );
} }
function getDocument() { function getDocument() {
//XHRwrapper(command,'PUT','text/plain; charset=UTF-8',true); //XHRwrapper(command,'PUT','text/plain; charset=UTF-8',true);
that.XHRwrapper(command, docId, '', 'GET', mime, '', false, false, that.XHRwrapper(command, docId, '', 'GET', mime, '', false, false,
function (reponse) { function (reponse) {
if (reponse === '404') { if (reponse === '404') {
return that.error(priv.createError( return that.error(priv.createError(
404, 404,
"Cannot find document", "Cannot find document",
"Document does not exist" "Document does not exist"
)); ));
} }
mon_document = reponse; mon_document = reponse;
putDocument(); putDocument();
} }
); );
} }
getDocument(); getDocument();
}; };
/** /**
* Remove a document or attachment * Remove a document or attachment
* @method remove * @method remove
* @param {object} command The JIO command * @param {object} command The JIO command
*/ */
that.remove = function (command) { that.remove = function (command) {
var docId, mime; var docId, mime;
docId = command.getDocId(); docId = command.getDocId();
mime = 'text/plain; charset=UTF-8'; mime = 'text/plain; charset=UTF-8';
function deleteDocument() { function deleteDocument() {
that.XHRwrapper(command, docId, '', 'DELETE', mime, '', true, false, that.XHRwrapper(command, docId, '', 'DELETE', mime, '', true, false,
function (reponse) { function (reponse) {
that.success({ that.success({
// response // response
"ok": true, "ok": true,
"id": docId "id": docId
//"rev": current_revision //"rev": current_revision
}); });
} }
); );
} }
function myCallback(response) { function myCallback(response) {
} }
that.XHRwrapper(command, docId, '', 'GET', mime, '', false, false, that.XHRwrapper(command, docId, '', 'GET', mime, '', false, false,
function (response) { function (response) {
var attachKeys, keys; var attachKeys, keys;
attachKeys = (JSON.parse(response))._attachments; attachKeys = (JSON.parse(response))._attachments;
for (keys in attachKeys) { for (keys in attachKeys) {
if (attachKeys.hasOwnProperty(keys)) { if (attachKeys.hasOwnProperty(keys)) {
that.XHRwrapper(command, that.XHRwrapper(command,
docId, docId,
keys, keys,
'DELETE', 'DELETE',
mime, mime,
'', '',
false, false,
false, false,
myCallback myCallback
); );
} }
} }
deleteDocument(); deleteDocument();
} }
); );
}; };
that.removeAttachment = function (command) { that.removeAttachment = function (command) {
var mon_document, var mon_document,
docId, docId,
attachId, attachId,
mime, mime,
attachment_id, attachment_id,
attachment_data, attachment_data,
attachment_md5, attachment_md5,
attachment_mimetype, attachment_mimetype,
attachment_length; attachment_length;
mon_document = null; mon_document = null;
docId = command.getDocId(); docId = command.getDocId();
attachId = command.getAttachmentId() || ''; attachId = command.getAttachmentId() || '';
mime = 'text/plain; charset=UTF-8'; mime = 'text/plain; charset=UTF-8';
//récupération des variables de l'attachement //récupération des variables de l'attachement
attachment_id = command.getAttachmentId(); attachment_id = command.getAttachmentId();
attachment_data = command.getAttachmentData(); attachment_data = command.getAttachmentData();
attachment_md5 = command.md5SumAttachmentData(); attachment_md5 = command.md5SumAttachmentData();
attachment_mimetype = command.getAttachmentMimeType(); attachment_mimetype = command.getAttachmentMimeType();
attachment_length = command.getAttachmentLength(); attachment_length = command.getAttachmentLength();
function removeAttachment() { function removeAttachment() {
that.XHRwrapper(command, docId, attachId, 'DELETE', mime, '', true, true, that.XHRwrapper(command, docId, attachId, 'DELETE', mime, '', true, true,
function (reponse) { function (reponse) {
} }
); );
} }
function putDocument() { function putDocument() {
var data, doc; var data, doc;
data = JSON.parse(mon_document); data = JSON.parse(mon_document);
doc = priv.updateMeta(data, docId, attachId, "remove", ''); doc = priv.updateMeta(data, docId, attachId, "remove", '');
that.XHRwrapper(command, docId, '', 'PUT', mime, doc, that.XHRwrapper(command, docId, '', 'PUT', mime, doc,
false, false, function (reponse) { false, false, function (reponse) {
removeAttachment(); removeAttachment();
} }
); );
} }
function getDocument() { function getDocument() {
that.XHRwrapper(command, docId, '', 'GET', mime, '', false, false, that.XHRwrapper(command, docId, '', 'GET', mime, '', false, false,
function (reponse) { function (reponse) {
mon_document = reponse; mon_document = reponse;
putDocument(); putDocument();
} }
); );
} }
getDocument(); getDocument();
}; };
/** /**
* Get all filenames belonging to a user from the document index * Get all filenames belonging to a user from the document index
* @method allDocs * @method allDocs
* @param {object} command The JIO command * @param {object} command The JIO command
**/ **/
that.allDocs = function (command) { that.allDocs = function (command) {
var mon_document, mime; var mon_document, mime;
mon_document = null; mon_document = null;
mime = 'text/plain; charset=UTF-8'; mime = 'text/plain; charset=UTF-8';
function makeJSON() { function makeJSON() {
var keys, var keys,
resultTable, resultTable,
counter, counter,
allDocResponse, allDocResponse,
count, count,
countB, countB,
dealCallback, dealCallback,
errCallback, errCallback,
i, i,
keyId, keyId,
Signature, Signature,
callURL, callURL,
requestUTC, requestUTC,
parse, parse,
checkCounter; checkCounter;
keys = $(mon_document).find('Key'); keys = $(mon_document).find('Key');
resultTable = []; resultTable = [];
counter = 0; counter = 0;
keys.each(function (index) { keys.each(function (index) {
var that, filename, docId; var that, filename, docId;
that = $(this); that = $(this);
filename = that.context.textContent; filename = that.context.textContent;
docId = priv.idsToFileName(priv.fileNameToIds(filename)[0]); docId = priv.idsToFileName(priv.fileNameToIds(filename)[0]);
if (counter === 0) { if (counter === 0) {
counter += 1; counter += 1;
resultTable.push(docId); resultTable.push(docId);
} else if (docId !== resultTable[counter - 1]) { } else if (docId !== resultTable[counter - 1]) {
counter += 1; counter += 1;
resultTable.push(docId); resultTable.push(docId);
} }
}); });
allDocResponse = { allDocResponse = {
// document content will be added to response // document content will be added to response
"total_rows": resultTable.length, "total_rows": resultTable.length,
"offset": 0, "offset": 0,
"rows": [] "rows": []
}; };
//needed to save the index within the $.ajax.success() callback //needed to save the index within the $.ajax.success() callback
count = resultTable.length - 1; count = resultTable.length - 1;
countB = 0; countB = 0;
dealCallback = function (i, countB, allDoc) { dealCallback = function (i, countB, allDoc) {
return function (doc, statustext, response) { return function (doc, statustext, response) {
allDoc.rows[i].doc = response.responseText; allDoc.rows[i].doc = response.responseText;
if (count === 0) { if (count === 0) {
that.success(allDoc); that.success(allDoc);
} else { } else {
count -= 1; count -= 1;
} }
}; };
}; };
errCallback = function (err) { errCallback = function (err) {
if (err.status === 404) { if (err.status === 404) {
//status //status
//statustext "Not Found" //statustext "Not Found"
//error //error
//reason "reason" //reason "reason"
//message "did not work" //message "did not work"
err.error = "not_found"; err.error = "not_found";
that.error(err); that.error(err);
} else { } else {
return that.retry(err); return that.retry(err);
} }
}; };
i = resultTable.length - 1; i = resultTable.length - 1;
if (command.getOption("include_docs") === true) { if (command.getOption("include_docs") === true) {
for (i; i >= 0; i -= 1) { for (i; i >= 0; i -= 1) {
keyId = resultTable[i]; keyId = resultTable[i];
Signature = that.encodeAuthorization(keyId); Signature = that.encodeAuthorization(keyId);
callURL = priv.url + keyId; callURL = priv.url + keyId;
requestUTC = new Date().toUTCString(); requestUTC = new Date().toUTCString();
parse = true; parse = true;
allDocResponse.rows[i] = { allDocResponse.rows[i] = {
"id": priv.fileNameToIds(keyId).join(), "id": priv.fileNameToIds(keyId).join(),
"key": keyId, "key": keyId,
"value": {} "value": {}
}; };
checkCounter = i; checkCounter = i;
$.ajax({ $.ajax({
contentType : '', contentType : '',
crossdomain : true, crossdomain : true,
url : callURL, url : callURL,
type : 'GET', type : 'GET',
headers : { headers : {
'Authorization' : "AWS" 'Authorization' : "AWS"
+ " " + " "
+ priv.AWSIdentifier + priv.AWSIdentifier
+ ":" + ":"
+ Signature, + Signature,
//'Host' : priv.url, //'Host' : priv.url,
'x-amz-date' : requestUTC, 'x-amz-date' : requestUTC,
'Content-Type' : 'application/json' 'Content-Type' : 'application/json'
//'Content-MD5' : '' //'Content-MD5' : ''
//'Content-Length' : , //'Content-Length' : ,
//'Expect' : , //'Expect' : ,
//'x-amz-security-token' : , //'x-amz-security-token' : ,
}, },
success : dealCallback(i, countB, allDocResponse), success : dealCallback(i, countB, allDocResponse),
error : errCallback(this) error : errCallback(this)
}); });
countB += 1; countB += 1;
} }
} else { } else {
for (i; i >= 0; i -= 1) { for (i; i >= 0; i -= 1) {
keyId = resultTable[i]; keyId = resultTable[i];
allDocResponse.rows[i] = { allDocResponse.rows[i] = {
"id": priv.fileNameToIds(keyId).join(), "id": priv.fileNameToIds(keyId).join(),
"key": keyId, "key": keyId,
"value": {} "value": {}
}; };
} }
that.success(allDocResponse); that.success(allDocResponse);
} }
} }
function getXML() { function getXML() {
//XHRwrapper(command,'PUT','text/plain; charset=UTF-8',true); //XHRwrapper(command,'PUT','text/plain; charset=UTF-8',true);
that.XHRwrapper(command, '', '', 'GET', mime, '', false, false, that.XHRwrapper(command, '', '', 'GET', mime, '', false, false,
function (reponse) { function (reponse) {
mon_document = reponse; mon_document = reponse;
makeJSON(); makeJSON();
} }
); );
} }
getXML(); getXML();
//fin alldocs //fin alldocs
}; };
return that; return that;
}); });
/* /*
// It is not possible to attach listeners to xhr level 2 events // It is not possible to attach listeners to xhr level 2 events
// AND validate the Qunit tests through sinon.js // AND validate the Qunit tests through sinon.js
// therefore, below methods are deprecated // therefore, below methods are deprecated
var S3specifics = {}; var S3specifics = {};
S3specifics.uploadProgress = function(evt){ S3specifics.uploadProgress = function(evt){
if (evt.lengthComputable) { if (evt.lengthComputable) {
var percentComplete = Math.round(evt.loaded * 100 / evt.total); var percentComplete = Math.round(evt.loaded * 100 / evt.total);
console.log(percentComplete.toString() + '%'); console.log(percentComplete.toString() + '%');
} else { } else {
console.log('Unable to compute.'); console.log('Unable to compute.');
} }
}; };
S3specifics.uploadComplete = function(evt){ S3specifics.uploadComplete = function(evt){
var evt_txt = evt.target.responseText; var evt_txt = evt.target.responseText;
var parser = new DOMParser(); var parser = new DOMParser();
var xmlDoc = parser.parseFromString(evt_txt, "text/xml"); var xmlDoc = parser.parseFromString(evt_txt, "text/xml");
var responseURL = $(xmlDoc.getElementsByTagName('Location'))[0].text(); var responseURL = $(xmlDoc.getElementsByTagName('Location'))[0].text();
console.log(responseURL); console.log(responseURL);
}; };
S3specifics.uploadFailed = function(evt){ S3specifics.uploadFailed = function(evt){
var evt_txt = evt.target.responseText; var evt_txt = evt.target.responseText;
console.log("Erreur lors de la tentative d'upload : " + evt_txt); console.log("Erreur lors de la tentative d'upload : " + evt_txt);
}; };
S3specifics.uploadCanceled = function(evt){ S3specifics.uploadCanceled = function(evt){
console.log("Upload annulé par l'utilisateur ou le navigateur."); console.log("Upload annulé par l'utilisateur ou le navigateur.");
}; };
S3specifics.onReadyStateChange = function(req, those, that) { S3specifics.onReadyStateChange = function(req, those, that) {
if (req.readyState === 4 && those.status === 200){ if (req.readyState === 4 && those.status === 200){
that.success({ that.success({
ok: true, ok: true,
id: command.getDocId() id: command.getDocId()
}); });
} }
}; };
*/ */
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