Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
jio
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Alexandra Rogova
jio
Commits
00ad6db5
Commit
00ad6db5
authored
Aug 07, 2013
by
Tristan Cavelier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
s3storage.js amd compatible now
parent
3865c36d
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
914 additions
and
901 deletions
+914
-901
src/jio.storage/s3storage.js
src/jio.storage/s3storage.js
+914
-901
No files found.
src/jio.storage/s3storage.js
View file @
00ad6db5
/*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
define, jIO, btoa, b64_hmac_sha1, jQuery, XMLHttpRequest, XHRwrapper,
/*global XMLHttpRequest: true, XHRwrapper: true, FormData: true, $: true
*/
FormData
*/
/**
/**
* JIO S3 Storage. Type = "s3".
* JIO S3 Storage. Type = "s3".
* Amazon S3 "database" storage.
* Amazon S3 "database" storage.
*/
*/
jIO
.
addStorageType
(
"
s3
"
,
function
(
spec
,
my
)
{
// define([module_name], [dependencies], module);
var
evt
,
that
,
priv
=
{};
(
function
(
dependencies
,
module
)
{
spec
=
spec
||
{};
"
use strict
"
;
that
=
my
.
basicStorage
(
spec
,
my
);
if
(
typeof
define
===
'
function
'
&&
define
.
amd
)
{
return
define
(
dependencies
,
module
);
// attributes
}
priv
.
username
=
spec
.
username
||
''
;
module
(
jIO
,
jQuery
,
{
b64_hmac_sha1
:
b64_hmac_sha1
});
priv
.
AWSIdentifier
=
spec
.
AWSIdentifier
||
''
;
}([
'
jio
'
,
'
jquery
'
,
'
sha1
'
],
function
(
jIO
,
$
,
sha1
)
{
priv
.
password
=
spec
.
password
||
''
;
var
b64_hmac_sha1
=
sha1
.
b64_hmac_sha1
;
priv
.
server
=
spec
.
server
||
''
;
/*|| jiobucket ||*/
priv
.
acl
=
spec
.
acl
||
''
;
jIO
.
addStorageType
(
"
s3
"
,
function
(
spec
,
my
)
{
var
evt
,
that
,
priv
=
{};
/*||> "private,
spec
=
spec
||
{};
public-read,
that
=
my
.
basicStorage
(
spec
,
my
);
public-read-write,
authenticated-read,
// attributes
bucket-owner-read,
priv
.
username
=
spec
.
username
||
''
;
bucket-owner-full-control" <||*/
priv
.
AWSIdentifier
=
spec
.
AWSIdentifier
||
''
;
priv
.
password
=
spec
.
password
||
''
;
priv
.
actionStatus
=
spec
.
actionStatus
||
''
;
priv
.
server
=
spec
.
server
||
''
;
/*|| jiobucket ||*/
priv
.
acl
=
spec
.
acl
||
''
;
priv
.
contenTType
=
spec
.
contenTType
||
''
;
/*||> "private,
/**
public-read,
* Update [doc] the document object and remove [doc] keys
public-read-write,
* which are not in [new_doc]. It only changes [doc] keys not starting
authenticated-read,
* with an underscore.
bucket-owner-read,
* ex: doc: {key:value1,_key:value2} with
bucket-owner-full-control" <||*/
* new_doc: {key:value3,_key:value4} updates
* doc: {key:value3,_key:value2}.
priv
.
actionStatus
=
spec
.
actionStatus
||
''
;
* @param {object} doc The original document object.
* @param {object} new_doc The new document object
priv
.
contenTType
=
spec
.
contenTType
||
''
;
**/
/**
priv
.
secureDocId
=
function
(
string
)
{
* Update [doc] the document object and remove [doc] keys
var
split
=
string
.
split
(
'
/
'
),
i
;
* which are not in [new_doc]. It only changes [doc] keys not starting
if
(
split
[
0
]
===
''
)
{
* with an underscore.
split
=
split
.
slice
(
1
);
* ex: doc: {key:value1,_key:value2} with
}
* new_doc: {key:value3,_key:value4} updates
for
(
i
=
0
;
i
<
split
.
length
;
i
+=
1
)
{
* doc: {key:value3,_key:value2}.
if
(
split
[
i
]
===
''
)
{
* @param {object} doc The original document object.
return
''
;
* @param {object} new_doc The new document object
**/
priv
.
secureDocId
=
function
(
string
)
{
var
split
=
string
.
split
(
'
/
'
),
i
;
if
(
split
[
0
]
===
''
)
{
split
=
split
.
slice
(
1
);
}
}
}
for
(
i
=
0
;
i
<
split
.
length
;
i
+=
1
)
{
return
split
.
join
(
'
%2F
'
);
if
(
split
[
i
]
===
''
)
{
};
return
''
;
}
}
return
split
.
join
(
'
%2F
'
);
};
/**
* Replace substrings to another strings
* @method recursiveReplace
* @param {string} string The string to do replacement
* @param {array} list_of_replacement An array of couple
* ["substring to select", "selected substring replaced by this string"].
* @return {string} The replaced string
*/
priv
.
recursiveReplace
=
function
(
string
,
list_of_replacement
)
{
var
i
,
split_string
=
string
.
split
(
list_of_replacement
[
0
][
0
]);
if
(
list_of_replacement
[
1
])
{
for
(
i
=
0
;
i
<
split_string
.
length
;
i
+=
1
)
{
split_string
[
i
]
=
priv
.
recursiveReplace
(
split_string
[
i
],
list_of_replacement
.
slice
(
1
)
);
}
}
return
split_string
.
join
(
list_of_replacement
[
0
][
1
]);
};
/**
/**
* Replace substrings to another strings
* Changes / to %2F, % to %25 and . to _.
* @method recursiveReplace
* @method secureName
* @param {string} string The string to do replacement
* @param {string} name The name to secure
* @param {array} list_of_replacement An array of couple
* @return {string} The secured name
* ["substring to select", "selected substring replaced by this string"].
*/
* @return {string} The replaced string
priv
.
secureName
=
function
(
name
)
{
*/
return
priv
.
recursiveReplace
(
name
,
[[
"
/
"
,
"
%2F
"
],
[
"
%
"
,
"
%25
"
]]);
priv
.
recursiveReplace
=
function
(
string
,
list_of_replacement
)
{
};
var
i
,
split_string
=
string
.
split
(
list_of_replacement
[
0
][
0
]);
if
(
list_of_replacement
[
1
])
{
/**
for
(
i
=
0
;
i
<
split_string
.
length
;
i
+=
1
)
{
* Restores the original name from a secured name
split_string
[
i
]
=
priv
.
recursiveReplace
(
* @method restoreName
split_string
[
i
],
* @param {string} secured_name The secured name to restore
list_of_replacement
.
slice
(
1
)
* @return {string} The original name
);
*/
priv
.
restoreName
=
function
(
secured_name
)
{
return
priv
.
recursiveReplace
(
secured_name
,
[[
"
%2F
"
,
"
/
"
],
[
"
%25
"
,
"
%
"
]]);
};
/**
* Convert document id and attachment id to a file name
* @method idsToFileName
* @param {string} doc_id The document id
* @param {string} attachment_id The attachment id (optional)
* @return {string} The file name
*/
priv
.
idsToFileName
=
function
(
doc_id
,
attachment_id
)
{
doc_id
=
priv
.
secureName
(
doc_id
).
split
(
"
.
"
).
join
(
"
_.
"
);
if
(
typeof
attachment_id
===
"
string
"
)
{
attachment_id
=
priv
.
secureName
(
attachment_id
).
split
(
"
.
"
).
join
(
"
_.
"
);
return
doc_id
+
"
.
"
+
attachment_id
;
}
}
}
return
doc_id
;
return
split_string
.
join
(
list_of_replacement
[
0
][
1
]);
};
};
/**
/**
* Convert a file name to a document id (and attachment id if there)
* Changes / to %2F, % to %25 and . to _.
* @method fileNameToIds
* @method secureName
* @param {string} file_name The file name to convert
* @param {string} name The name to secure
* @return {array} ["document id", "attachment id"] or ["document id"]
* @return {string} The secured name
*/
*/
priv
.
fileNameToIds
=
function
(
file_name
)
{
priv
.
secureName
=
function
(
name
)
{
var
separator_index
=
-
1
,
split
=
file_name
.
split
(
"
.
"
);
return
priv
.
recursiveReplace
(
name
,
[[
"
/
"
,
"
%2F
"
],
[
"
%
"
,
"
%25
"
]]);
split
.
slice
(
0
,
-
1
).
forEach
(
function
(
file_name_part
,
index
)
{
};
if
(
file_name_part
.
slice
(
-
1
)
!==
"
_
"
)
{
separator_index
=
index
;
/**
}
* Restores the original name from a secured name
});
* @method restoreName
if
(
separator_index
===
-
1
)
{
* @param {string} secured_name The secured name to restore
return
[
priv
.
restoreName
(
priv
.
restoreName
(
* @return {string} The original name
file_name
*/
).
split
(
"
_.
"
).
join
(
"
.
"
))];
priv
.
restoreName
=
function
(
secured_name
)
{
return
priv
.
recursiveReplace
(
secured_name
,
[[
"
%2F
"
,
"
/
"
],
[
"
%25
"
,
"
%
"
]]);
};
/**
* Convert document id and attachment id to a file name
* @method idsToFileName
* @param {string} doc_id The document id
* @param {string} attachment_id The attachment id (optional)
* @return {string} The file name
*/
priv
.
idsToFileName
=
function
(
doc_id
,
attachment_id
)
{
doc_id
=
priv
.
secureName
(
doc_id
).
split
(
"
.
"
).
join
(
"
_.
"
);
if
(
typeof
attachment_id
===
"
string
"
)
{
attachment_id
=
priv
.
secureName
(
attachment_id
).
split
(
"
.
"
).
join
(
"
_.
"
);
return
doc_id
+
"
.
"
+
attachment_id
;
}
return
doc_id
;
};
/**
* Convert a file name to a document id (and attachment id if there)
* @method fileNameToIds
* @param {string} file_name The file name to convert
* @return {array} ["document id", "attachment id"] or ["document id"]
*/
priv
.
fileNameToIds
=
function
(
file_name
)
{
var
separator_index
=
-
1
,
split
=
file_name
.
split
(
"
.
"
);
split
.
slice
(
0
,
-
1
).
forEach
(
function
(
file_name_part
,
index
)
{
if
(
file_name_part
.
slice
(
-
1
)
!==
"
_
"
)
{
separator_index
=
index
;
}
}
});
return
[
if
(
separator_index
===
-
1
)
{
priv
.
restoreName
(
priv
.
restoreName
(
return
[
priv
.
restoreName
(
priv
.
restoreName
(
split
.
slice
(
0
,
separator_index
+
1
).
join
(
"
.
"
)
file_name
).
split
(
"
_.
"
).
join
(
"
.
"
)),
).
split
(
"
_.
"
).
join
(
"
.
"
))];
priv
.
restoreName
(
priv
.
restoreName
(
}
split
.
slice
(
separator_index
+
1
).
join
(
"
.
"
)
return
[
).
split
(
"
_.
"
).
join
(
"
.
"
))
priv
.
restoreName
(
priv
.
restoreName
(
];
split
.
slice
(
0
,
separator_index
+
1
).
join
(
"
.
"
)
};
).
split
(
"
_.
"
).
join
(
"
.
"
)),
priv
.
restoreName
(
priv
.
restoreName
(
/**
split
.
slice
(
separator_index
+
1
).
join
(
"
.
"
)
* Removes the last character if it is a "/". "/a/b/c/" become "/a/b/c"
).
split
(
"
_.
"
).
join
(
"
.
"
))
* @method removeSlashIfLast
];
* @param {string} string The string to modify
};
* @return {string} The modified string
*/
/**
priv
.
removeSlashIfLast
=
function
(
string
)
{
* Removes the last character if it is a "/". "/a/b/c/" become "/a/b/c"
if
(
string
[
string
.
length
-
1
]
===
"
/
"
)
{
* @method removeSlashIfLast
return
string
.
slice
(
0
,
-
1
);
* @param {string} string The string to modify
}
* @return {string} The modified string
return
string
;
*/
};
priv
.
removeSlashIfLast
=
function
(
string
)
{
if
(
string
[
string
.
length
-
1
]
===
"
/
"
)
{
return
string
.
slice
(
0
,
-
1
);
}
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 ======================
that
.
specToStore
=
function
()
{
return
{
"
username
"
:
priv
.
username
,
"
password
"
:
priv
.
password
,
"
server
"
:
priv
.
server
,
"
acl
"
:
priv
.
acl
};
};
};
that
.
validateState
=
function
()
{
// ===================== overrides ======================
// xxx complete error message
that
.
specToStore
=
function
()
{
// jjj completion below
return
{
"
username
"
:
priv
.
username
,
"
password
"
:
priv
.
password
,
"
server
"
:
priv
.
server
,
"
acl
"
:
priv
.
acl
};
};
if
(
typeof
priv
.
AWSIdentifier
===
"
string
"
&&
priv
.
AWSIdentifier
===
''
)
{
that
.
validateState
=
function
()
{
return
'
Need at least one parameter "Aws login".
'
;
// xxx complete error message
}
// jjj completion below
if
(
typeof
priv
.
password
===
"
string
"
&&
priv
.
password
===
''
)
{
return
'
Need at least one parameter "password".
'
;
if
(
typeof
priv
.
AWSIdentifier
===
"
string
"
&&
priv
.
AWSIdentifier
===
''
)
{
}
return
'
Need at least one parameter "Aws login".
'
;
if
(
typeof
priv
.
server
===
"
string
"
&&
priv
.
server
===
''
)
{
}
return
'
Need at least one parameter "server".
'
;
if
(
typeof
priv
.
password
===
"
string
"
&&
priv
.
password
===
''
)
{
}
return
'
Need at least one parameter "password".
'
;
return
''
;
}
};
if
(
typeof
priv
.
server
===
"
string
"
&&
priv
.
server
===
''
)
{
return
'
Need at least one parameter "server".
'
;
// =================== S3 Specifics =================
}
/**
return
''
;
* Encoding the signature using a stringToSign
* Encoding the policy
* @method buildStringToSign
* @param {string} http_verb The HTTP method
* @param {string} content_md5 The md5 content
* @param {string} content_type The content type
* @param {number} expires The expires time
* @param {string} x_amz_headers The specific amazon headers
* @param {string} path_key The path of the document
* @return {string} The generated signature
*/
// xxx no need to make it public, use private -> "priv" (not "that")
priv
.
buildStringToSign
=
function
(
http_verb
,
content_md5
,
content_type
,
expires
,
x_amz_headers
,
path_key
)
{
//example :
// var StringToSign = S3.buildStringToSign(S3.httpVerb,'','','',
// 'x-amz-date:'+S3.requestUTC,'/jio1st/prive.json');
var
StringToSign
=
http_verb
+
'
\n
'
+
content_md5
+
'
\n
'
//content-md5
+
content_type
+
'
\n
'
//content-type
+
expires
+
'
\n
'
//expires
+
x_amz_headers
+
'
\n
'
//x-amz headers
+
path_key
;
//path key
return
StringToSign
;
};
that
.
encodePolicy
=
function
(
form
)
{
//generates the policy
//enables the choice for the http response code
var
http_code
,
s3_policy
,
Signature
=
''
;
s3_policy
=
{
"
expiration
"
:
"
2020-01-01T00:00:00Z
"
,
"
conditions
"
:
[
{
"
bucket
"
:
priv
.
server
},
[
"
starts-with
"
,
"
$key
"
,
""
],
{
"
acl
"
:
priv
.
acl
},
{
"
success_action_redirect
"
:
""
},
{
"
success_action_status
"
:
http_code
},
[
"
starts-with
"
,
"
$Content-Type
"
,
""
],
[
"
content-length-range
"
,
0
,
524288000
]
]
};
};
//base64 encoding of the policy (native base64 js >>
// =================== S3 Specifics =================
// .btoa() = encode, .atob() = decode)
/**
priv
.
b64_policy
=
btoa
(
JSON
.
stringify
(
s3_policy
));
* Encoding the signature using a stringToSign
//generates the signature value using the policy and the secret access key
* Encoding the policy
//use of sha1.js to generate the signature
* @method buildStringToSign
Signature
=
that
.
signature
(
priv
.
b64_policy
);
* @param {string} http_verb The HTTP method
* @param {string} content_md5 The md5 content
};
* @param {string} content_type The content type
* @param {number} expires The expires time
that
.
signature
=
function
(
string
)
{
* @param {string} x_amz_headers The specific amazon headers
var
Signature
=
b64_hmac_sha1
(
priv
.
password
,
string
);
* @param {string} path_key The path of the document
return
Signature
;
* @return {string} The generated signature
};
*/
function
xhr_onreadystatechange
(
docId
,
// xxx no need to make it public, use private -> "priv" (not "that")
command
,
priv
.
buildStringToSign
=
function
(
http_verb
,
content_md5
,
content_type
,
obj
,
expires
,
x_amz_headers
,
path_key
)
{
http
,
//example :
jio
,
// var StringToSign = S3.buildStringToSign(S3.httpVerb,'','','',
isAttachment
,
// 'x-amz-date:'+S3.requestUTC,'/jio1st/prive.json');
callback
)
{
obj
.
onreadystatechange
=
function
()
{
var
StringToSign
=
http_verb
+
'
\n
'
var
response
,
err
=
''
;
+
content_md5
+
'
\n
'
//content-md5
if
(
obj
.
readyState
===
4
)
{
+
content_type
+
'
\n
'
//content-type
if
(
this
.
status
===
204
||
this
.
status
===
201
||
this
.
status
===
200
)
{
+
expires
+
'
\n
'
//expires
switch
(
http
)
{
+
x_amz_headers
+
'
\n
'
//x-amz headers
case
"
POST
"
:
+
path_key
;
//path key
that
.
success
({
ok
:
true
,
return
StringToSign
;
id
:
docId
};
});
break
;
case
'
PUT
'
:
if
(
jio
===
true
)
{
that
.
encodePolicy
=
function
(
form
)
{
//generates the policy
//enables the choice for the http response code
var
http_code
,
s3_policy
,
Signature
=
''
;
s3_policy
=
{
"
expiration
"
:
"
2020-01-01T00:00:00Z
"
,
"
conditions
"
:
[
{
"
bucket
"
:
priv
.
server
},
[
"
starts-with
"
,
"
$key
"
,
""
],
{
"
acl
"
:
priv
.
acl
},
{
"
success_action_redirect
"
:
""
},
{
"
success_action_status
"
:
http_code
},
[
"
starts-with
"
,
"
$Content-Type
"
,
""
],
[
"
content-length-range
"
,
0
,
524288000
]
]
};
//base64 encoding of the policy (native base64 js >>
// .btoa() = encode, .atob() = decode)
priv
.
b64_policy
=
btoa
(
JSON
.
stringify
(
s3_policy
));
//generates the signature value using the policy and the secret access key
//use of sha1.js to generate the signature
Signature
=
that
.
signature
(
priv
.
b64_policy
);
};
that
.
signature
=
function
(
string
)
{
var
Signature
=
b64_hmac_sha1
(
priv
.
password
,
string
);
return
Signature
;
};
function
xhr_onreadystatechange
(
docId
,
command
,
obj
,
http
,
jio
,
isAttachment
,
callback
)
{
obj
.
onreadystatechange
=
function
()
{
var
response
,
err
=
''
;
if
(
obj
.
readyState
===
4
)
{
if
(
this
.
status
===
204
||
this
.
status
===
201
||
this
.
status
===
200
)
{
switch
(
http
)
{
case
"
POST
"
:
that
.
success
({
that
.
success
({
ok
:
true
,
ok
:
true
,
id
:
command
.
getDocId
()
id
:
docId
});
});
}
else
{
break
;
callback
(
this
.
responseText
);
case
'
PUT
'
:
}
if
(
jio
===
true
)
{
break
;
case
'
GET
'
:
if
(
jio
===
true
)
{
if
(
typeof
this
.
responseText
!==
'
string
'
)
{
response
=
JSON
.
parse
(
this
.
responseText
);
response
.
_attachments
=
response
.
_attachments
||
{};
delete
response
.
_attachments
;
that
.
success
(
JSON
.
stringify
(
response
));
}
else
{
if
(
isAttachment
===
true
)
{
that
.
success
(
this
.
responseText
);
}
else
{
that
.
success
(
JSON
.
parse
(
this
.
responseText
));
}
}
}
else
{
callback
(
this
.
responseText
);
}
break
;
case
'
DELETE
'
:
if
(
jio
===
true
)
{
if
(
isAttachment
===
false
)
{
that
.
success
({
that
.
success
({
ok
:
true
,
ok
:
true
,
id
:
command
.
getDocId
()
id
:
command
.
getDocId
()
});
});
}
else
{
}
else
{
that
.
success
({
callback
(
this
.
responseText
);
ok
:
true
,
id
:
command
.
getDocId
(),
attachment
:
command
.
getAttachmentId
()
});
}
}
}
else
{
break
;
callback
(
this
.
responseText
);
case
'
GET
'
:
}
break
;
}
}
else
{
err
=
this
;
if
(
this
.
status
===
405
)
{
//status
//statustext "Not Found"
//error
//reason "reason"
//message "did not work"
err
.
error
=
"
not_allowed
"
;
that
.
error
(
err
);
}
if
(
this
.
status
===
404
)
{
if
(
http
===
'
GET
'
)
{
if
(
jio
===
true
)
{
if
(
jio
===
true
)
{
if
(
typeof
this
.
responseText
!==
'
string
'
)
{
response
=
JSON
.
parse
(
this
.
responseText
);
response
.
_attachments
=
response
.
_attachments
||
{};
delete
response
.
_attachments
;
that
.
success
(
JSON
.
stringify
(
response
));
}
else
{
if
(
isAttachment
===
true
)
{
that
.
success
(
this
.
responseText
);
}
else
{
that
.
success
(
JSON
.
parse
(
this
.
responseText
));
}
}
}
else
{
callback
(
this
.
responseText
);
}
break
;
case
'
DELETE
'
:
if
(
jio
===
true
)
{
if
(
isAttachment
===
false
)
{
that
.
success
({
ok
:
true
,
id
:
command
.
getDocId
()
});
}
else
{
that
.
success
({
ok
:
true
,
id
:
command
.
getDocId
(),
attachment
:
command
.
getAttachmentId
()
});
}
}
else
{
callback
(
this
.
responseText
);
}
break
;
}
}
else
{
err
=
this
;
if
(
this
.
status
===
405
)
{
//status
//statustext "Not Found"
//error
//reason "reason"
//message "did not work"
err
.
error
=
"
not_allowed
"
;
that
.
error
(
err
);
}
if
(
this
.
status
===
404
)
{
if
(
http
===
'
GET
'
)
{
if
(
jio
===
true
)
{
//status
//statustext "Not Found"
//error
//reason "reason"
//message "did not work"
err
.
statustext
=
"
not_foud
"
;
err
.
reason
=
"
file does not exist
"
;
err
.
error
=
"
not_found
"
;
that
.
error
(
err
);
}
else
{
callback
(
'
404
'
);
}
}
else
{
//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
.
reason
=
"
file does not exist
"
;
err
.
error
=
"
not_found
"
;
err
.
error
=
"
not_found
"
;
that
.
error
(
err
);
that
.
error
(
err
);
}
else
{
callback
(
'
404
'
);
}
}
}
else
{
}
//status
if
(
this
.
status
===
409
)
{
//statustext "Not Found"
//status
//error
//statustext "Not Found"
//reason "reason"
//error
//message "did not work"
//reason "reason"
err
.
error
=
"
not_found
"
;
//message "did not work"
err
.
error
=
"
already_exists
"
;
that
.
error
(
err
);
that
.
error
(
err
);
}
}
}
}
if
(
this
.
status
===
409
)
{
//status
//statustext "Not Found"
//error
//reason "reason"
//message "did not work"
err
.
error
=
"
already_exists
"
;
that
.
error
(
err
);
}
}
}
};
}
priv
.
updateMeta
=
function
(
doc
,
docid
,
attachid
,
action
,
data
)
{
doc
.
_attachments
=
doc
.
_attachments
||
{};
switch
(
action
)
{
case
"
add
"
:
doc
.
_attachments
[
attachid
]
=
data
;
//nothing happens
doc
=
JSON
.
stringify
(
doc
);
break
;
case
"
remove
"
:
if
(
doc
.
_attachments
!==
undefined
)
{
delete
doc
.
_attachments
[
attachid
];
}
doc
=
JSON
.
stringify
(
doc
);
break
;
case
"
update
"
:
doc
.
_attachments
[
attachid
]
=
data
;
//update happened in the put request
doc
=
JSON
.
stringify
(
doc
);
break
;
}
}
return
doc
;
};
};
}
priv
.
updateMeta
=
function
(
doc
,
docid
,
attachid
,
action
,
data
)
{
priv
.
createError
=
function
(
status
,
message
,
reason
)
{
doc
.
_attachments
=
doc
.
_attachments
||
{};
var
error
=
{
switch
(
action
)
{
"
status
"
:
status
,
case
"
add
"
:
"
message
"
:
message
,
doc
.
_attachments
[
attachid
]
=
data
;
"
reason
"
:
reason
//nothing happens
};
doc
=
JSON
.
stringify
(
doc
);
switch
(
status
)
{
break
;
case
404
:
case
"
remove
"
:
error
.
statusText
=
"
Not found
"
;
if
(
doc
.
_attachments
!==
undefined
)
{
break
;
delete
doc
.
_attachments
[
attachid
];
case
405
:
error
.
statusText
=
"
Method Not Allowed
"
;
break
;
case
409
:
error
.
statusText
=
"
Conflicts
"
;
break
;
case
24
:
error
.
statusText
=
"
Corrupted Document
"
;
break
;
}
}
doc
=
JSON
.
stringify
(
doc
);
error
.
error
=
error
.
statusText
.
toLowerCase
().
split
(
"
"
).
join
(
"
_
"
);
break
;
return
error
;
case
"
update
"
:
doc
.
_attachments
[
attachid
]
=
data
;
//update happened in the put request
doc
=
JSON
.
stringify
(
doc
);
break
;
}
return
doc
;
};
priv
.
createError
=
function
(
status
,
message
,
reason
)
{
var
error
=
{
"
status
"
:
status
,
"
message
"
:
message
,
"
reason
"
:
reason
};
};
switch
(
status
)
{
case
404
:
error
.
statusText
=
"
Not found
"
;
break
;
case
405
:
error
.
statusText
=
"
Method Not Allowed
"
;
break
;
case
409
:
error
.
statusText
=
"
Conflicts
"
;
break
;
case
24
:
error
.
statusText
=
"
Corrupted Document
"
;
break
;
}
error
.
error
=
error
.
statusText
.
toLowerCase
().
split
(
"
"
).
join
(
"
_
"
);
return
error
;
};
that
.
encodeAuthorization
=
function
(
key
,
mime
)
{
//GET oriented method
var
requestUTC
,
httpVerb
,
StringToSign
,
Signature
;
requestUTC
=
new
Date
().
toUTCString
();
httpVerb
=
"
GET
"
;
StringToSign
=
priv
.
buildStringToSign
(
httpVerb
,
''
,
'
application/json
'
,
''
,
'
x-amz-date:
'
+
requestUTC
,
'
/
'
+
priv
.
server
+
'
/
'
+
key
);
Signature
=
b64_hmac_sha1
(
priv
.
password
,
StringToSign
);
return
Signature
;
};
that
.
XHRwrapper
=
function
(
command
,
docId
,
attachId
,
http
,
mime
,
data
,
jio
,
is_attachment
,
callback
)
{
var
docFile
,
requestUTC
,
StringToSign
,
url
,
Signature
,
xhr
;
docFile
=
priv
.
secureName
(
priv
.
idsToFileName
(
docId
,
attachId
||
undefined
));
requestUTC
=
new
Date
().
toUTCString
();
StringToSign
=
priv
.
buildStringToSign
(
http
,
''
,
mime
,
''
,
'
x-amz-date:
'
+
requestUTC
,
'
/
'
+
priv
.
server
+
'
/
'
+
docFile
);
url
=
'
http://s3.amazonaws.com/
'
+
priv
.
server
+
'
/
'
+
docFile
;
Signature
=
b64_hmac_sha1
(
priv
.
password
,
StringToSign
);
xhr
=
new
XMLHttpRequest
();
xhr
.
open
(
http
,
url
,
true
);
xhr
.
setRequestHeader
(
"
HTTP-status-code
"
,
"
100
"
);
xhr
.
setRequestHeader
(
"
x-amz-date
"
,
requestUTC
);
xhr
.
setRequestHeader
(
"
Authorization
"
,
"
AWS
"
+
priv
.
AWSIdentifier
+
"
:
"
+
Signature
);
xhr
.
setRequestHeader
(
"
Content-Type
"
,
mime
);
xhr
.
responseType
=
'
text
'
;
xhr_onreadystatechange
(
docId
,
command
,
xhr
,
http
,
jio
,
is_attachment
,
callback
);
if
(
http
===
'
PUT
'
)
{
that
.
encodeAuthorization
=
function
(
key
,
mime
)
{
xhr
.
send
(
data
);
//GET oriented method
}
else
{
var
requestUTC
,
httpVerb
,
StringToSign
,
Signature
;
xhr
.
send
(
null
);
requestUTC
=
new
Date
().
toUTCString
();
}
httpVerb
=
"
GET
"
;
};
StringToSign
=
priv
.
buildStringToSign
(
httpVerb
,
// ==================== commands ====================
''
,
/**
'
application/json
'
,
* Create a document in local storage.
''
,
* @method post
'
x-amz-date:
'
+
requestUTC
,
* @param {object} command The JIO command
'
/
'
+
priv
.
server
+
'
/
'
+
key
**/
);
Signature
=
b64_hmac_sha1
(
priv
.
password
,
StringToSign
);
that
.
post
=
function
(
command
)
{
return
Signature
;
//as S3 encoding key are directly inserted within the FormData(),
};
//use of XHRwrapper function ain't pertinent
that
.
XHRwrapper
=
function
(
command
,
var
doc
,
doc_id
,
mime
;
docId
,
doc
=
command
.
cloneDoc
();
attachId
,
doc_id
=
command
.
getDocId
();
http
,
mime
,
function
postDocument
()
{
data
,
var
http_response
,
fd
,
Signature
,
xhr
;
jio
,
doc_id
=
priv
.
secureName
(
priv
.
idsToFileName
(
doc_id
));
is_attachment
,
//Meant to deep-serialize in order to avoid
callback
)
{
//conflicts due to the multipart enctype
doc
=
JSON
.
stringify
(
doc
);
var
docFile
,
requestUTC
,
StringToSign
,
url
,
Signature
,
xhr
;
http_response
=
''
;
docFile
=
priv
.
secureName
(
priv
.
idsToFileName
(
docId
,
fd
=
new
FormData
();
attachId
||
undefined
));
//virtually builds the form fields
//filename
requestUTC
=
new
Date
().
toUTCString
();
fd
.
append
(
'
key
'
,
doc_id
);
//file access authorizations
StringToSign
=
priv
.
buildStringToSign
(
priv
.
acl
=
""
;
http
,
fd
.
append
(
'
acl
'
,
priv
.
acl
);
''
,
//content-type
mime
,
priv
.
contenTType
=
"
text/plain
"
;
''
,
fd
.
append
(
'
Content-Type
'
,
priv
.
contenTType
);
'
x-amz-date:
'
+
requestUTC
,
//allows specification of a success url redirection
'
/
'
+
priv
.
server
+
'
/
'
+
docFile
fd
.
append
(
'
success_action_redirect
'
,
''
);
);
//allows to specify the http code response if the request is successful
fd
.
append
(
'
success_action_status
'
,
http_response
);
url
=
'
http://s3.amazonaws.com/
'
+
priv
.
server
+
'
/
'
+
docFile
;
//login AWS
fd
.
append
(
'
AWSAccessKeyId
'
,
priv
.
AWSIdentifier
);
Signature
=
b64_hmac_sha1
(
priv
.
password
,
StringToSign
);
//exchange policy with the amazon s3 service
//can be common to all uploads or specific
that
.
encodePolicy
(
fd
);
//priv.b64_policy = that.encodePolicy(fd);
fd
.
append
(
'
policy
'
,
priv
.
b64_policy
);
//signature through the base64.hmac.sha1(secret key, policy) method
Signature
=
b64_hmac_sha1
(
priv
.
password
,
priv
.
b64_policy
);
fd
.
append
(
'
signature
'
,
Signature
);
//uploaded content !!may must be a string rather than an object
fd
.
append
(
'
file
'
,
doc
);
xhr
=
new
XMLHttpRequest
();
xhr
=
new
XMLHttpRequest
();
xhr_onreadystatechange
(
doc_id
,
command
,
xhr
,
'
POST
'
,
true
,
false
,
''
);
xhr
.
open
(
'
POST
'
,
'
https://
'
+
priv
.
server
+
'
.s3.amazonaws.com/
'
,
true
);
xhr
.
send
(
fd
);
}
if
(
doc_id
===
''
||
doc_id
===
undefined
)
{
xhr
.
open
(
http
,
url
,
true
);
doc_id
=
'
no_document_id_
'
xhr
.
setRequestHeader
(
"
HTTP-status-code
"
,
"
100
"
);
+
((
Math
.
random
()
*
10
).
toString
().
split
(
'
.
'
))[
1
];
xhr
.
setRequestHeader
(
"
x-amz-date
"
,
requestUTC
);
doc
.
_id
=
doc_id
;
xhr
.
setRequestHeader
(
"
Authorization
"
,
"
AWS
"
}
+
priv
.
AWSIdentifier
+
"
:
"
+
Signature
);
xhr
.
setRequestHeader
(
"
Content-Type
"
,
mime
);
xhr
.
responseType
=
'
text
'
;
xhr_onreadystatechange
(
docId
,
command
,
xhr
,
http
,
jio
,
is_attachment
,
callback
);
if
(
http
===
'
PUT
'
)
{
xhr
.
send
(
data
);
}
else
{
xhr
.
send
(
null
);
}
};
mime
=
'
text/plain; charset=UTF-8
'
;
// ==================== commands ====================
that
.
XHRwrapper
(
command
,
doc_id
,
''
,
'
GET
'
,
mime
,
''
,
false
,
false
,
/**
function
(
response
)
{
* Create a document in local storage.
if
(
response
===
'
404
'
)
{
* @method post
postDocument
();
* @param {object} command The JIO command
}
else
{
**/
//si ce n'est pas une 404,
//alors on renvoit une erreur 405
that
.
post
=
function
(
command
)
{
return
that
.
error
(
priv
.
createError
(
//as S3 encoding key are directly inserted within the FormData(),
409
,
//use of XHRwrapper function ain't pertinent
"
Cannot create document
"
,
"
Document already exists
"
var
doc
,
doc_id
,
mime
;
));
doc
=
command
.
cloneDoc
();
}
doc_id
=
command
.
getDocId
();
function
postDocument
()
{
var
http_response
,
fd
,
Signature
,
xhr
;
doc_id
=
priv
.
secureName
(
priv
.
idsToFileName
(
doc_id
));
//Meant to deep-serialize in order to avoid
//conflicts due to the multipart enctype
doc
=
JSON
.
stringify
(
doc
);
http_response
=
''
;
fd
=
new
FormData
();
//virtually builds the form fields
//filename
fd
.
append
(
'
key
'
,
doc_id
);
//file access authorizations
priv
.
acl
=
""
;
fd
.
append
(
'
acl
'
,
priv
.
acl
);
//content-type
priv
.
contenTType
=
"
text/plain
"
;
fd
.
append
(
'
Content-Type
'
,
priv
.
contenTType
);
//allows specification of a success url redirection
fd
.
append
(
'
success_action_redirect
'
,
''
);
//allows to specify the http code response if the request is successful
fd
.
append
(
'
success_action_status
'
,
http_response
);
//login AWS
fd
.
append
(
'
AWSAccessKeyId
'
,
priv
.
AWSIdentifier
);
//exchange policy with the amazon s3 service
//can be common to all uploads or specific
that
.
encodePolicy
(
fd
);
//priv.b64_policy = that.encodePolicy(fd);
fd
.
append
(
'
policy
'
,
priv
.
b64_policy
);
//signature through the base64.hmac.sha1(secret key, policy) method
Signature
=
b64_hmac_sha1
(
priv
.
password
,
priv
.
b64_policy
);
fd
.
append
(
'
signature
'
,
Signature
);
//uploaded content !!may must be a string rather than an object
fd
.
append
(
'
file
'
,
doc
);
xhr
=
new
XMLHttpRequest
();
xhr_onreadystatechange
(
doc_id
,
command
,
xhr
,
'
POST
'
,
true
,
false
,
''
);
xhr
.
open
(
'
POST
'
,
'
https://
'
+
priv
.
server
+
'
.s3.amazonaws.com/
'
,
true
);
xhr
.
send
(
fd
);
}
}
);
};
/**
* Get a document or attachment
* @method get
* @param {object} command The JIO command
**/
that
.
get
=
function
(
command
)
{
var
docId
,
attachId
,
isJIO
,
mime
;
docId
=
command
.
getDocId
();
attachId
=
command
.
getAttachmentId
()
||
''
;
isJIO
=
true
;
mime
=
'
text/plain; charset=UTF-8
'
;
that
.
XHRwrapper
(
command
,
docId
,
attachId
,
'
GET
'
,
mime
,
''
,
isJIO
,
false
);
};
that
.
getAttachment
=
function
(
command
)
{
var
docId
,
attachId
,
isJIO
,
mime
;
docId
=
command
.
getDocId
();
attachId
=
command
.
getAttachmentId
();
isJIO
=
true
;
mime
=
'
text/plain; charset=UTF-8
'
;
that
.
XHRwrapper
(
command
,
docId
,
attachId
,
'
GET
'
,
mime
,
''
,
isJIO
,
true
);
};
/**
* Create or update a document in local storage.
* @method put
* @param {object} command The JIO command
**/
that
.
put
=
function
(
command
)
{
var
doc
,
docId
,
mime
;
doc
=
command
.
cloneDoc
();
docId
=
command
.
getDocId
();
mime
=
'
text/plain; charset=UTF-8
'
;
//pas d'attachment dans un put simple
function
putDocument
()
{
var
attachId
,
data
,
isJIO
;
attachId
=
''
;
data
=
JSON
.
stringify
(
doc
);
isJIO
=
true
;
that
.
XHRwrapper
(
command
,
docId
,
attachId
,
'
PUT
'
,
mime
,
data
,
isJIO
,
false
);
}
that
.
XHRwrapper
(
command
,
docId
,
''
,
'
GET
'
,
mime
,
''
,
false
,
false
,
if
(
doc_id
===
''
||
doc_id
===
undefined
)
{
function
(
response
)
{
doc_id
=
'
no_document_id_
'
//if (response === '404') {}
+
((
Math
.
random
()
*
10
).
toString
().
split
(
'
.
'
))[
1
];
if
(
response
.
_attachments
!==
undefined
)
{
doc
.
_id
=
doc_id
;
doc
.
_attachments
=
response
.
_attachments
;
}
putDocument
();
}
}
);
};
mime
=
'
text/plain; charset=UTF-8
'
;
that
.
XHRwrapper
(
command
,
doc_id
,
''
,
'
GET
'
,
mime
,
''
,
false
,
false
,
that
.
putAttachment
=
function
(
command
)
{
function
(
response
)
{
var
mon_document
,
if
(
response
===
'
404
'
)
{
docId
,
postDocument
();
attachId
,
}
else
{
mime
,
//si ce n'est pas une 404,
attachment_id
,
//alors on renvoit une erreur 405
attachment_data
,
return
that
.
error
(
priv
.
createError
(
attachment_md5
,
409
,
attachment_mimetype
,
"
Cannot create document
"
,
attachment_length
;
"
Document already exists
"
));
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_data
=
command
.
getAttachmentData
();
attachment_md5
=
command
.
md5SumAttachmentData
();
attachment_mimetype
=
command
.
getAttachmentMimeType
();
attachment_length
=
command
.
getAttachmentLength
();
function
putAttachment
()
{
that
.
XHRwrapper
(
command
,
docId
,
attachId
,
'
PUT
'
,
mime
,
attachment_data
,
false
,
true
,
function
(
reponse
)
{
that
.
success
({
// response
"
ok
"
:
true
,
"
id
"
:
docId
,
"
attachment
"
:
attachId
//"rev": current_revision
});
}
}
);
);
}
}
;
function
putDocument
()
{
/**
var
attachment_obj
,
data
,
doc
;
* Get a document or attachment
attachment_obj
=
{
* @method get
//"revpos": 3, // optional
* @param {object} command The JIO command
"
digest
"
:
attachment_md5
,
**/
"
content_type
"
:
attachment_mimetype
,
"
length
"
:
attachment_length
that
.
get
=
function
(
command
)
{
};
var
docId
,
attachId
,
isJIO
,
mime
;
data
=
JSON
.
parse
(
mon_document
);
docId
=
command
.
getDocId
();
attachId
=
command
.
getAttachmentId
()
||
''
;
isJIO
=
true
;
mime
=
'
text/plain; charset=UTF-8
'
;
that
.
XHRwrapper
(
command
,
docId
,
attachId
,
'
GET
'
,
mime
,
''
,
isJIO
,
false
);
};
doc
=
priv
.
updateMeta
(
data
,
docId
,
attachId
,
"
add
"
,
attachment_obj
);
that
.
getAttachment
=
function
(
command
)
{
var
docId
,
attachId
,
isJIO
,
mime
;
docId
=
command
.
getDocId
();
attachId
=
command
.
getAttachmentId
();
isJIO
=
true
;
mime
=
'
text/plain; charset=UTF-8
'
;
that
.
XHRwrapper
(
command
,
docId
,
attachId
,
'
GET
'
,
mime
,
''
,
isJIO
,
true
);
};
that
.
XHRwrapper
(
command
,
docId
,
''
,
'
PUT
'
,
mime
,
doc
,
false
,
false
,
/**
function
(
reponse
)
{
* Create or update a document in local storage.
putAttachment
();
* @method put
}
* @param {object} command The JIO command
);
**/
}
that
.
put
=
function
(
command
)
{
var
doc
,
docId
,
mime
;
doc
=
command
.
cloneDoc
();
docId
=
command
.
getDocId
();
mime
=
'
text/plain; charset=UTF-8
'
;
//pas d'attachment dans un put simple
function
putDocument
()
{
var
attachId
,
data
,
isJIO
;
attachId
=
''
;
data
=
JSON
.
stringify
(
doc
);
isJIO
=
true
;
that
.
XHRwrapper
(
command
,
docId
,
attachId
,
'
PUT
'
,
mime
,
data
,
isJIO
,
false
);
}
function
getDocument
()
{
//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
(
response
)
{
if
(
reponse
===
'
404
'
)
{
//if (response === '404') {}
return
that
.
error
(
priv
.
createError
(
if
(
response
.
_attachments
!==
undefined
)
{
404
,
doc
.
_attachments
=
response
.
_attachments
;
"
Cannot find document
"
,
"
Document does not exist
"
));
}
}
mon_document
=
reponse
;
putDocument
();
putDocument
();
}
}
);
);
}
};
getDocument
();
};
/**
* Remove a document or attachment
* @method remove
* @param {object} command The JIO command
*/
that
.
remove
=
function
(
command
)
{
var
docId
,
mime
;
docId
=
command
.
getDocId
();
mime
=
'
text/plain; charset=UTF-8
'
;
function
deleteDocument
()
{
that
.
XHRwrapper
(
command
,
docId
,
''
,
'
DELETE
'
,
mime
,
''
,
true
,
false
,
function
(
reponse
)
{
that
.
success
({
// response
"
ok
"
:
true
,
"
id
"
:
docId
//"rev": current_revision
});
}
);
}
function
myCallback
(
response
)
{
that
.
putAttachment
=
function
(
command
)
{
}
var
mon_document
,
docId
,
attachId
,
mime
,
attachment_id
,
attachment_data
,
attachment_md5
,
attachment_mimetype
,
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_data
=
command
.
getAttachmentData
();
attachment_md5
=
command
.
md5SumAttachmentData
();
attachment_mimetype
=
command
.
getAttachmentMimeType
();
attachment_length
=
command
.
getAttachmentLength
();
function
putAttachment
()
{
that
.
XHRwrapper
(
command
,
docId
,
attachId
,
'
PUT
'
,
mime
,
attachment_data
,
false
,
true
,
function
(
reponse
)
{
that
.
success
({
// response
"
ok
"
:
true
,
"
id
"
:
docId
,
"
attachment
"
:
attachId
//"rev": current_revision
});
}
);
}
function
putDocument
()
{
var
attachment_obj
,
data
,
doc
;
attachment_obj
=
{
//"revpos": 3, // optional
"
digest
"
:
attachment_md5
,
"
content_type
"
:
attachment_mimetype
,
"
length
"
:
attachment_length
};
data
=
JSON
.
parse
(
mon_document
);
doc
=
priv
.
updateMeta
(
data
,
docId
,
attachId
,
"
add
"
,
attachment_obj
);
that
.
XHRwrapper
(
command
,
docId
,
''
,
'
GET
'
,
mime
,
''
,
false
,
false
,
that
.
XHRwrapper
(
command
,
docId
,
''
,
'
PUT
'
,
mime
,
doc
,
false
,
false
,
function
(
response
)
{
function
(
reponse
)
{
var
attachKeys
,
keys
;
putAttachment
();
attachKeys
=
(
JSON
.
parse
(
response
)).
_attachments
;
for
(
keys
in
attachKeys
)
{
if
(
attachKeys
.
hasOwnProperty
(
keys
))
{
that
.
XHRwrapper
(
command
,
docId
,
keys
,
'
DELETE
'
,
mime
,
''
,
false
,
false
,
myCallback
);
}
}
}
);
deleteDocument
();
}
}
);
};
that
.
removeAttachment
=
function
(
command
)
{
var
mon_document
,
docId
,
attachId
,
mime
,
attachment_id
,
attachment_data
,
attachment_md5
,
attachment_mimetype
,
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_data
=
command
.
getAttachmentData
();
attachment_md5
=
command
.
md5SumAttachmentData
();
attachment_mimetype
=
command
.
getAttachmentMimeType
();
attachment_length
=
command
.
getAttachmentLength
();
function
removeAttachment
()
{
that
.
XHRwrapper
(
command
,
docId
,
attachId
,
'
DELETE
'
,
mime
,
''
,
true
,
true
,
function
(
reponse
)
{
}
);
}
function
putDocument
()
{
function
getDocument
()
{
var
data
,
doc
;
//XHRwrapper(command,'PUT','text/plain; charset=UTF-8',true);
data
=
JSON
.
parse
(
mon_document
);
that
.
XHRwrapper
(
command
,
docId
,
''
,
'
GET
'
,
mime
,
''
,
false
,
false
,
doc
=
priv
.
updateMeta
(
data
,
docId
,
attachId
,
"
remove
"
,
''
);
function
(
reponse
)
{
that
.
XHRwrapper
(
command
,
docId
,
''
,
'
PUT
'
,
mime
,
doc
,
if
(
reponse
===
'
404
'
)
{
false
,
false
,
function
(
reponse
)
{
return
that
.
error
(
priv
.
createError
(
removeAttachment
();
404
,
}
"
Cannot find document
"
,
);
"
Document does not exist
"
}
));
}
mon_document
=
reponse
;
putDocument
();
}
);
}
getDocument
();
};
/**
* Remove a document or attachment
* @method remove
* @param {object} command The JIO command
*/
that
.
remove
=
function
(
command
)
{
var
docId
,
mime
;
docId
=
command
.
getDocId
();
mime
=
'
text/plain; charset=UTF-8
'
;
function
deleteDocument
()
{
that
.
XHRwrapper
(
command
,
docId
,
''
,
'
DELETE
'
,
mime
,
''
,
true
,
false
,
function
(
reponse
)
{
that
.
success
({
// response
"
ok
"
:
true
,
"
id
"
:
docId
//"rev": current_revision
});
}
);
}
function
myCallback
(
response
)
{
}
function
getDocument
()
{
that
.
XHRwrapper
(
command
,
docId
,
''
,
'
GET
'
,
mime
,
''
,
false
,
false
,
that
.
XHRwrapper
(
command
,
docId
,
''
,
'
GET
'
,
mime
,
''
,
false
,
false
,
function
(
reponse
)
{
function
(
response
)
{
mon_document
=
reponse
;
var
attachKeys
,
keys
;
putDocument
();
attachKeys
=
(
JSON
.
parse
(
response
)).
_attachments
;
for
(
keys
in
attachKeys
)
{
if
(
attachKeys
.
hasOwnProperty
(
keys
))
{
that
.
XHRwrapper
(
command
,
docId
,
keys
,
'
DELETE
'
,
mime
,
''
,
false
,
false
,
myCallback
);
}
}
deleteDocument
();
}
}
);
);
}
};
getDocument
();
};
/**
* Get all filenames belonging to a user from the document index
* @method allDocs
* @param {object} command The JIO command
**/
that
.
allDocs
=
function
(
command
)
{
var
mon_document
,
mime
;
mon_document
=
null
;
mime
=
'
text/plain; charset=UTF-8
'
;
function
makeJSON
()
{
var
keys
,
resultTable
,
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
)
{
var
that
,
filename
,
docId
;
that
=
$
(
this
);
filename
=
that
.
context
.
textContent
;
docId
=
priv
.
idsToFileName
(
priv
.
fileNameToIds
(
filename
)[
0
]);
if
(
counter
===
0
)
{
counter
+=
1
;
resultTable
.
push
(
docId
);
}
else
if
(
docId
!==
resultTable
[
counter
-
1
])
{
counter
+=
1
;
resultTable
.
push
(
docId
);
}
});
allDocResponse
=
{
// document content will be added to response
"
total_rows
"
:
resultTable
.
length
,
"
offset
"
:
0
,
"
rows
"
:
[]
};
//needed to save the index within the $.ajax.success() callback
that
.
removeAttachment
=
function
(
command
)
{
count
=
resultTable
.
length
-
1
;
var
mon_document
,
countB
=
0
;
docId
,
attachId
,
mime
,
attachment_id
,
attachment_data
,
attachment_md5
,
attachment_mimetype
,
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_data
=
command
.
getAttachmentData
();
attachment_md5
=
command
.
md5SumAttachmentData
();
attachment_mimetype
=
command
.
getAttachmentMimeType
();
attachment_length
=
command
.
getAttachmentLength
();
function
removeAttachment
()
{
that
.
XHRwrapper
(
command
,
docId
,
attachId
,
'
DELETE
'
,
mime
,
''
,
true
,
true
,
function
(
reponse
)
{
}
);
}
dealCallback
=
function
(
i
,
countB
,
allDoc
)
{
function
putDocument
(
)
{
return
function
(
doc
,
statustext
,
response
)
{
var
data
,
doc
;
allDoc
.
rows
[
i
].
doc
=
response
.
responseText
;
data
=
JSON
.
parse
(
mon_document
)
;
if
(
count
===
0
)
{
doc
=
priv
.
updateMeta
(
data
,
docId
,
attachId
,
"
remove
"
,
''
);
that
.
success
(
allDoc
);
that
.
XHRwrapper
(
command
,
docId
,
''
,
'
PUT
'
,
mime
,
doc
,
}
else
{
false
,
false
,
function
(
reponse
)
{
count
-=
1
;
removeAttachment
()
;
}
}
}
;
)
;
}
;
}
errCallback
=
function
(
err
)
{
function
getDocument
()
{
if
(
err
.
status
===
404
)
{
that
.
XHRwrapper
(
command
,
docId
,
''
,
'
GET
'
,
mime
,
''
,
false
,
false
,
//status
function
(
reponse
)
{
//statustext "Not Found"
mon_document
=
reponse
;
//error
putDocument
();
//reason "reason"
}
//message "did not work"
);
err
.
error
=
"
not_found
"
;
}
that
.
error
(
err
);
getDocument
();
}
else
{
};
return
that
.
retry
(
err
);
}
};
i
=
resultTable
.
length
-
1
;
/**
* Get all filenames belonging to a user from the document index
* @method allDocs
* @param {object} command The JIO command
**/
that
.
allDocs
=
function
(
command
)
{
var
mon_document
,
mime
;
mon_document
=
null
;
mime
=
'
text/plain; charset=UTF-8
'
;
function
makeJSON
()
{
var
keys
,
resultTable
,
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
)
{
var
that
,
filename
,
docId
;
that
=
$
(
this
);
filename
=
that
.
context
.
textContent
;
docId
=
priv
.
idsToFileName
(
priv
.
fileNameToIds
(
filename
)[
0
]);
if
(
counter
===
0
)
{
counter
+=
1
;
resultTable
.
push
(
docId
);
}
else
if
(
docId
!==
resultTable
[
counter
-
1
])
{
counter
+=
1
;
resultTable
.
push
(
docId
);
}
});
if
(
command
.
getOption
(
"
include_docs
"
)
===
true
)
{
allDocResponse
=
{
// document content will be added to response
"
total_rows
"
:
resultTable
.
length
,
"
offset
"
:
0
,
"
rows
"
:
[]
};
for
(
i
;
i
>=
0
;
i
-=
1
)
{
//needed to save the index within the $.ajax.success() callback
keyId
=
resultTable
[
i
];
count
=
resultTable
.
length
-
1
;
Signature
=
that
.
encodeAuthorization
(
keyId
);
countB
=
0
;
callURL
=
'
http://
'
+
priv
.
server
+
'
.s3.amazonaws.com/
'
+
keyId
;
requestUTC
=
new
Date
().
toUTCString
();
parse
=
true
;
allDocResponse
.
rows
[
i
]
=
{
dealCallback
=
function
(
i
,
countB
,
allDoc
)
{
"
id
"
:
priv
.
fileNameToIds
(
keyId
).
join
(),
return
function
(
doc
,
statustext
,
response
)
{
"
key
"
:
keyId
,
allDoc
.
rows
[
i
].
doc
=
response
.
responseText
;
"
value
"
:
{}
if
(
count
===
0
)
{
};
that
.
success
(
allDoc
);
checkCounter
=
i
;
}
else
{
count
-=
1
;
$
.
ajax
({
}
contentType
:
''
,
crossdomain
:
true
,
url
:
callURL
,
type
:
'
GET
'
,
headers
:
{
'
Authorization
'
:
"
AWS
"
+
"
"
+
priv
.
AWSIdentifier
+
"
:
"
+
Signature
,
'
x-amz-date
'
:
requestUTC
,
'
Content-Type
'
:
'
application/json
'
//'Content-MD5' : ''
//'Content-Length' : ,
//'Expect' : ,
//'x-amz-security-token' : ,
},
success
:
dealCallback
(
i
,
countB
,
allDocResponse
),
error
:
errCallback
(
this
)
});
countB
+=
1
;
}
}
else
{
for
(
i
;
i
>=
0
;
i
-=
1
)
{
keyId
=
resultTable
[
i
];
allDocResponse
.
rows
[
i
]
=
{
"
id
"
:
priv
.
fileNameToIds
(
keyId
).
join
(),
"
key
"
:
keyId
,
"
value
"
:
{}
};
};
};
errCallback
=
function
(
err
)
{
if
(
err
.
status
===
404
)
{
//status
//statustext "Not Found"
//error
//reason "reason"
//message "did not work"
err
.
error
=
"
not_found
"
;
that
.
error
(
err
);
}
else
{
return
that
.
retry
(
err
);
}
};
i
=
resultTable
.
length
-
1
;
if
(
command
.
getOption
(
"
include_docs
"
)
===
true
)
{
for
(
i
;
i
>=
0
;
i
-=
1
)
{
keyId
=
resultTable
[
i
];
Signature
=
that
.
encodeAuthorization
(
keyId
);
callURL
=
'
http://
'
+
priv
.
server
+
'
.s3.amazonaws.com/
'
+
keyId
;
requestUTC
=
new
Date
().
toUTCString
();
parse
=
true
;
allDocResponse
.
rows
[
i
]
=
{
"
id
"
:
priv
.
fileNameToIds
(
keyId
).
join
(),
"
key
"
:
keyId
,
"
value
"
:
{}
};
checkCounter
=
i
;
$
.
ajax
({
contentType
:
''
,
crossdomain
:
true
,
url
:
callURL
,
type
:
'
GET
'
,
headers
:
{
'
Authorization
'
:
"
AWS
"
+
"
"
+
priv
.
AWSIdentifier
+
"
:
"
+
Signature
,
'
x-amz-date
'
:
requestUTC
,
'
Content-Type
'
:
'
application/json
'
//'Content-MD5' : ''
//'Content-Length' : ,
//'Expect' : ,
//'x-amz-security-token' : ,
},
success
:
dealCallback
(
i
,
countB
,
allDocResponse
),
error
:
errCallback
(
this
)
});
countB
+=
1
;
}
}
else
{
for
(
i
;
i
>=
0
;
i
-=
1
)
{
keyId
=
resultTable
[
i
];
allDocResponse
.
rows
[
i
]
=
{
"
id
"
:
priv
.
fileNameToIds
(
keyId
).
join
(),
"
key
"
:
keyId
,
"
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
();
//fin alldocs
};
return
that
;
});
getXML
();
}));
//fin alldocs
};
return
that
;
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment