Commit 52f4a62d authored by Jonathan Rivalan's avatar Jonathan Rivalan

finalisation jslint

parent 15ee9b70
/*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 */ /*global XMLHttpRequest: true, XHRwrapper: true, FormData: true, $: true*/
/*global XHRwrapper: true */
/*global FormData: true */
/*global $: true */
/*jslint vars: 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; var evt, that, priv = {};
var that, priv = {};
spec = spec || {}; spec = spec || {};
that = my.basicStorage(spec, my); that = my.basicStorage(spec, my);
...@@ -48,8 +42,7 @@ jIO.addStorageType("s3", function (spec, my) { ...@@ -48,8 +42,7 @@ jIO.addStorageType("s3", function (spec, my) {
**/ **/
priv.secureDocId = function (string) { priv.secureDocId = function (string) {
var split = string.split('/'), var split = string.split('/'), i;
i;
if (split[0] === '') { if (split[0] === '') {
split = split.slice(1); split = split.slice(1);
} }
...@@ -263,8 +256,8 @@ jIO.addStorageType("s3", function (spec, my) { ...@@ -263,8 +256,8 @@ jIO.addStorageType("s3", function (spec, my) {
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 = ''; var http_code, s3_policy, Signature = '';
var s3_policy = { s3_policy = {
"expiration": "2020-01-01T00:00:00Z", "expiration": "2020-01-01T00:00:00Z",
"conditions": [ "conditions": [
{"bucket": priv.server }, {"bucket": priv.server },
...@@ -282,7 +275,7 @@ jIO.addStorageType("s3", function (spec, my) { ...@@ -282,7 +275,7 @@ jIO.addStorageType("s3", function (spec, my) {
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
var Signature = that.signature(priv.b64_policy); Signature = that.signature(priv.b64_policy);
}; };
...@@ -299,6 +292,7 @@ jIO.addStorageType("s3", function (spec, my) { ...@@ -299,6 +292,7 @@ jIO.addStorageType("s3", function (spec, my) {
isAttachment, isAttachment,
callback) { callback) {
obj.onreadystatechange = function () { obj.onreadystatechange = function () {
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) {
...@@ -321,7 +315,7 @@ jIO.addStorageType("s3", function (spec, my) { ...@@ -321,7 +315,7 @@ jIO.addStorageType("s3", function (spec, my) {
case 'GET': case 'GET':
if (jio === true) { if (jio === true) {
if (typeof this.responseText !== 'string') { if (typeof this.responseText !== 'string') {
var 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));
...@@ -356,7 +350,7 @@ jIO.addStorageType("s3", function (spec, my) { ...@@ -356,7 +350,7 @@ jIO.addStorageType("s3", function (spec, my) {
break; break;
} }
} else { } else {
var err = this; err = this;
if (this.status === 405) { if (this.status === 405) {
//status //status
//statustext "Not Found" //statustext "Not Found"
...@@ -456,10 +450,11 @@ jIO.addStorageType("s3", function (spec, my) { ...@@ -456,10 +450,11 @@ jIO.addStorageType("s3", function (spec, my) {
}; };
that.encodeAuthorization = function (key, mime) { that.encodeAuthorization = function (key, mime) {
//GET oriented method //GET oriented method
var requestUTC = new Date().toUTCString(); var requestUTC, httpVerb, StringToSign, Signature;
var httpVerb = "GET"; requestUTC = new Date().toUTCString();
var StringToSign = priv.buildStringToSign( httpVerb = "GET";
StringToSign = priv.buildStringToSign(
httpVerb, httpVerb,
'', '',
'application/json', 'application/json',
...@@ -467,7 +462,7 @@ jIO.addStorageType("s3", function (spec, my) { ...@@ -467,7 +462,7 @@ jIO.addStorageType("s3", function (spec, my) {
'x-amz-date:' + requestUTC, 'x-amz-date:' + requestUTC,
'/' + priv.server + '/' + key '/' + priv.server + '/' + key
); );
var Signature = b64_hmac_sha1(priv.password, StringToSign); Signature = b64_hmac_sha1(priv.password, StringToSign);
return Signature; return Signature;
}; };
...@@ -481,12 +476,13 @@ jIO.addStorageType("s3", function (spec, my) { ...@@ -481,12 +476,13 @@ jIO.addStorageType("s3", function (spec, my) {
is_attachment, is_attachment,
callback) { callback) {
var docFile = priv.secureName(priv.idsToFileName(docId, var docFile, requestUTC, StringToSign, url, Signature, xhr;
docFile = priv.secureName(priv.idsToFileName(docId,
attachId || undefined)); attachId || undefined));
var requestUTC = new Date().toUTCString(); requestUTC = new Date().toUTCString();
var StringToSign = priv.buildStringToSign( StringToSign = priv.buildStringToSign(
http, http,
'', '',
mime, mime,
...@@ -495,11 +491,9 @@ jIO.addStorageType("s3", function (spec, my) { ...@@ -495,11 +491,9 @@ jIO.addStorageType("s3", function (spec, my) {
'/' + priv.server + '/' + docFile '/' + priv.server + '/' + docFile
); );
var url = 'http://s3.amazonaws.com/' + priv.server + '/' + docFile; url = 'http://s3.amazonaws.com/' + priv.server + '/' + docFile;
var Signature = b64_hmac_sha1(priv.password, StringToSign);
var xhr; Signature = b64_hmac_sha1(priv.password, StringToSign);
xhr = new XMLHttpRequest(); xhr = new XMLHttpRequest();
xhr.open(http, url, true); xhr.open(http, url, true);
...@@ -538,17 +532,18 @@ jIO.addStorageType("s3", function (spec, my) { ...@@ -538,17 +532,18 @@ jIO.addStorageType("s3", function (spec, my) {
//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; 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;
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);
var http_response = ''; http_response = '';
var 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);
...@@ -570,11 +565,11 @@ jIO.addStorageType("s3", function (spec, my) { ...@@ -570,11 +565,11 @@ jIO.addStorageType("s3", function (spec, my) {
//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
var 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);
var 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);
...@@ -585,7 +580,8 @@ jIO.addStorageType("s3", function (spec, my) { ...@@ -585,7 +580,8 @@ jIO.addStorageType("s3", function (spec, my) {
+ ((Math.random() * 10).toString().split('.'))[1]; + ((Math.random() * 10).toString().split('.'))[1];
doc._id = doc_id; doc._id = doc_id;
} }
var 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') {
...@@ -610,18 +606,20 @@ jIO.addStorageType("s3", function (spec, my) { ...@@ -610,18 +606,20 @@ jIO.addStorageType("s3", function (spec, my) {
**/ **/
that.get = function (command) { that.get = function (command) {
var docId = command.getDocId(); var docId, attachId, isJIO, mime;
var attachId = command.getAttachmentId() || ''; docId = command.getDocId();
var isJIO = true; attachId = command.getAttachmentId() || '';
var mime = 'text/plain; charset=UTF-8'; isJIO = true;
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 = command.getDocId(); var docId, attachId, isJIO, mime;
var attachId = command.getAttachmentId(); docId = command.getDocId();
var isJIO = true; attachId = command.getAttachmentId();
var mime = 'text/plain; charset=UTF-8'; isJIO = true;
mime = 'text/plain; charset=UTF-8';
that.XHRwrapper(command, docId, attachId, 'GET', mime, '', isJIO, true); that.XHRwrapper(command, docId, attachId, 'GET', mime, '', isJIO, true);
}; };
...@@ -632,15 +630,16 @@ jIO.addStorageType("s3", function (spec, my) { ...@@ -632,15 +630,16 @@ jIO.addStorageType("s3", function (spec, my) {
**/ **/
that.put = function (command) { that.put = function (command) {
var doc = command.cloneDoc(); var doc, docId, mime;
var docId = command.getDocId(); doc = command.cloneDoc();
var mime = 'text/plain; charset=UTF-8'; docId = command.getDocId();
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 = ''; var attachId, data, isJIO;
var data = JSON.stringify(doc); attachId = '';
var isJIO = true; data = JSON.stringify(doc);
var mime = 'text/plain; charset=UTF-8'; isJIO = true;
that.XHRwrapper(command, that.XHRwrapper(command,
docId, docId,
attachId, attachId,
...@@ -663,16 +662,22 @@ jIO.addStorageType("s3", function (spec, my) { ...@@ -663,16 +662,22 @@ jIO.addStorageType("s3", function (spec, my) {
}; };
that.putAttachment = function (command) { that.putAttachment = function (command) {
var mon_document = null; var mon_document,
var docId = command.getDocId(); docId,
var attachId = command.getAttachmentId() || ''; attachId,
var mime = 'text/plain; charset=UTF-8'; mime,
//récupération des variables de l'attachement attachment_id,
var attachment_id,
attachment_data, attachment_data,
attachment_md5, attachment_md5,
attachment_mimetype, attachment_mimetype,
attachment_length; attachment_length;
mon_document = null;
docId = command.getDocId();
attachId = command.getAttachmentId() || '';
mime = 'text/plain; charset=UTF-8';
//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();
...@@ -701,15 +706,16 @@ jIO.addStorageType("s3", function (spec, my) { ...@@ -701,15 +706,16 @@ jIO.addStorageType("s3", function (spec, my) {
} }
function putDocument() { function putDocument() {
var attachment_obj = { var attachment_obj, data, doc;
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
}; };
var data = JSON.parse(mon_document); data = JSON.parse(mon_document);
var 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) {
...@@ -744,8 +750,9 @@ jIO.addStorageType("s3", function (spec, my) { ...@@ -744,8 +750,9 @@ jIO.addStorageType("s3", function (spec, my) {
*/ */
that.remove = function (command) { that.remove = function (command) {
var docId = command.getDocId(); var docId, mime;
var mime = 'text/plain; charset=UTF-8'; docId = command.getDocId();
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,
...@@ -765,9 +772,8 @@ jIO.addStorageType("s3", function (spec, my) { ...@@ -765,9 +772,8 @@ jIO.addStorageType("s3", function (spec, my) {
that.XHRwrapper(command, docId, '', 'GET', mime, '', false, false, that.XHRwrapper(command, docId, '', 'GET', mime, '', false, false,
function (response) { function (response) {
console.log(response); var attachKeys, keys;
var attachKeys = (JSON.parse(response))._attachments; attachKeys = (JSON.parse(response))._attachments;
var keys;
for (keys in attachKeys) { for (keys in attachKeys) {
if (attachKeys.hasOwnProperty(keys)) { if (attachKeys.hasOwnProperty(keys)) {
that.XHRwrapper(command, that.XHRwrapper(command,
...@@ -788,17 +794,22 @@ jIO.addStorageType("s3", function (spec, my) { ...@@ -788,17 +794,22 @@ jIO.addStorageType("s3", function (spec, my) {
}; };
that.removeAttachment = function (command) { that.removeAttachment = function (command) {
var mon_document = null; var mon_document,
var docId = command.getDocId(); docId,
var attachId = command.getAttachmentId() || ''; attachId,
var mime = 'text/plain; charset=UTF-8'; mime,
//récupération des variables de l'attachement attachment_id,
var attachment_id,
attachment_data, attachment_data,
attachment_md5, attachment_md5,
attachment_mimetype, attachment_mimetype,
attachment_length; attachment_length;
mon_document = null;
docId = command.getDocId();
attachId = command.getAttachmentId() || '';
mime = 'text/plain; charset=UTF-8';
//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();
...@@ -813,8 +824,9 @@ jIO.addStorageType("s3", function (spec, my) { ...@@ -813,8 +824,9 @@ jIO.addStorageType("s3", function (spec, my) {
} }
function putDocument() { function putDocument() {
var data = JSON.parse(mon_document); var data, doc;
var doc = priv.updateMeta(data, docId, attachId, "remove", ''); data = JSON.parse(mon_document);
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();
...@@ -840,17 +852,34 @@ jIO.addStorageType("s3", function (spec, my) { ...@@ -840,17 +852,34 @@ jIO.addStorageType("s3", function (spec, my) {
**/ **/
that.allDocs = function (command) { that.allDocs = function (command) {
var mon_document = null; var mon_document, mime;
var mime = 'text/plain; charset=UTF-8'; mon_document = null;
mime = 'text/plain; charset=UTF-8';
function makeJSON() { function makeJSON() {
var keys = $(mon_document).find('Key'); var keys,
var resultTable = []; resultTable,
var counter = 0; counter,
allDocResponse,
count,
countB,
dealCallback,
errCallback,
i,
keyId,
Signature,
callURL,
requestUTC,
parse,
checkCounter;
keys = $(mon_document).find('Key');
resultTable = [];
counter = 0;
keys.each(function (index) { keys.each(function (index) {
var that = $(this); var that, filename, docId;
var filename = that.context.textContent; that = $(this);
var docId = priv.idsToFileName(priv.fileNameToIds(filename)[0]); filename = that.context.textContent;
docId = priv.idsToFileName(priv.fileNameToIds(filename)[0]);
if (counter === 0) { if (counter === 0) {
counter += 1; counter += 1;
resultTable.push(docId); resultTable.push(docId);
...@@ -860,7 +889,7 @@ jIO.addStorageType("s3", function (spec, my) { ...@@ -860,7 +889,7 @@ jIO.addStorageType("s3", function (spec, my) {
} }
}); });
var 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,
...@@ -868,9 +897,9 @@ jIO.addStorageType("s3", function (spec, my) { ...@@ -868,9 +897,9 @@ jIO.addStorageType("s3", function (spec, my) {
}; };
//needed to save the index within the $.ajax.success() callback //needed to save the index within the $.ajax.success() callback
var count = resultTable.length - 1; count = resultTable.length - 1;
var countB = 0; countB = 0;
var 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) {
...@@ -881,35 +910,34 @@ jIO.addStorageType("s3", function (spec, my) { ...@@ -881,35 +910,34 @@ jIO.addStorageType("s3", function (spec, my) {
}; };
}; };
var 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);
} }
}; };
var i = resultTable.length - 1; i = resultTable.length - 1;
var keyId;
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];
var Signature = that.encodeAuthorization(keyId); Signature = that.encodeAuthorization(keyId);
var callURL = priv.url + keyId; callURL = priv.url + keyId;
var requestUTC = new Date().toUTCString(); requestUTC = new Date().toUTCString();
var 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": {}
}; };
var checkCounter = i; checkCounter = i;
$.ajax({ $.ajax({
contentType : '', contentType : '',
......
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