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
Labels
Merge Requests
18
Merge Requests
18
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
nexedi
jio
Commits
441320d8
Commit
441320d8
authored
Aug 06, 2013
by
Tristan Cavelier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
davstorage.js amd compatible now
parent
f2df0cdd
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
828 additions
and
812 deletions
+828
-812
src/jio.storage/davstorage.js
src/jio.storage/davstorage.js
+828
-812
No files found.
src/jio.storage/davstorage.js
View file @
441320d8
/*
/*
* Copyright 2013, Nexedi SA
* Copyright 2013, Nexedi SA
* Released under the LGPL license.
* Released under the LGPL license.
* http://www.gnu.org/licenses/lgpl.html
* http://www.gnu.org/licenses/lgpl.html
*/
*/
/*jslint indent: 2, maxlen: 80, nomen: true */
/*jslint indent: 2, maxlen: 80, nomen: true */
/*global
jIO: true, $: true, btoa: true
*/
/*global
define, jIO, jQuery, btoa
*/
// JIO Dav Storage Description :
// JIO Dav Storage Description :
// {
// {
...
@@ -57,871 +57,887 @@
...
@@ -57,871 +57,887 @@
// ('%', '%25')
// ('%', '%25')
// The file will be saved as http:%2F%2F100%25_.json
// The file will be saved as http:%2F%2F100%25_.json
jIO
.
addStorageType
(
"
dav
"
,
function
(
spec
,
my
)
{
// define([module_name], [dependencies], module);
(
function
(
dependencies
,
module
)
{
"
use strict
"
;
"
use strict
"
;
var
priv
=
{},
that
=
my
.
basicStorage
(
spec
,
my
),
dav
=
{};
if
(
typeof
define
===
'
function
'
&&
define
.
amd
)
{
return
define
(
dependencies
,
module
);
// ATTRIBUTES //
}
priv
.
url
=
null
;
module
(
jIO
,
jQuery
);
priv
.
username
=
null
;
}([
'
jio
'
,
'
jquery
'
],
function
(
jIO
,
$
)
{
priv
.
password
=
null
;
"
use strict
"
;
priv
.
encoded_login
=
null
;
jIO
.
addStorageType
(
"
dav
"
,
function
(
spec
,
my
)
{
var
priv
=
{},
that
=
my
.
basicStorage
(
spec
,
my
),
dav
=
{};
// CONSTRUCTOR //
/**
// ATTRIBUTES //
* Init the dav storage connector thanks to the description
priv
.
url
=
null
;
* @method __init__
priv
.
username
=
null
;
* @param {object} description The description object
priv
.
password
=
null
;
*/
priv
.
encoded_login
=
null
;
priv
.
__init__
=
function
(
description
)
{
priv
.
url
=
description
.
url
||
""
;
// CONSTRUCTOR //
priv
.
url
=
priv
.
removeSlashIfLast
(
priv
.
url
);
/**
// if (description.secured_login) {
* Init the dav storage connector thanks to the description
// not implemented
* @method __init__
// } else
* @param {object} description The description object
if
(
description
.
encoded_login
)
{
*/
priv
.
encoded_login
=
description
.
encoded_login
;
priv
.
__init__
=
function
(
description
)
{
}
else
if
(
description
.
auth_type
)
{
priv
.
url
=
description
.
url
||
""
;
if
(
description
.
auth_type
===
"
basic
"
)
{
priv
.
url
=
priv
.
removeSlashIfLast
(
priv
.
url
);
priv
.
encoded_login
=
"
Basic
"
+
// if (description.secured_login) {
btoa
((
description
.
username
||
""
)
+
"
:
"
+
// not implemented
(
description
.
password
||
""
));
// } else
if
(
description
.
encoded_login
)
{
priv
.
encoded_login
=
description
.
encoded_login
;
}
else
if
(
description
.
auth_type
)
{
if
(
description
.
auth_type
===
"
basic
"
)
{
priv
.
encoded_login
=
"
Basic
"
+
btoa
((
description
.
username
||
""
)
+
"
:
"
+
(
description
.
password
||
""
));
}
}
else
{
priv
.
encoded_login
=
""
;
}
}
}
else
{
priv
.
encoded_login
=
""
;
}
};
// OVERRIDES //
that
.
specToStore
=
function
()
{
// TODO: secured password
// The encoded_login can be seen by anyone, we must find a way to secure it!
// secured_login = encrypt(encoded_login)
// encoded_login = decrypt(secured_login)
return
{
"
url
"
:
priv
.
url
,
"
encoded_login
"
:
priv
.
encoded_login
};
};
};
// OVERRIDES //
that
.
validateState
=
function
()
{
that
.
specToStore
=
function
()
{
if
(
typeof
priv
.
url
!==
"
string
"
||
priv
.
url
===
""
)
{
// TODO: secured password
return
"
The webDav server URL is not provided
"
;
// The encoded_login can be seen by anyone,
}
// we must find a way to secure it!
if
(
priv
.
encoded_login
===
null
)
{
// secured_login = encrypt(encoded_login)
return
"
Impossible to create the authorization
"
;
// encoded_login = decrypt(secured_login)
}
return
{
return
""
;
"
url
"
:
priv
.
url
,
};
"
encoded_login
"
:
priv
.
encoded_login
};
// TOOLS //
/**
* Generate a new uuid
* @method generateUuid
* @return {string} The new uuid
*/
priv
.
generateUuid
=
function
()
{
var
S4
=
function
()
{
/* 65536 */
var
i
,
string
=
Math
.
floor
(
Math
.
random
()
*
0x10000
).
toString
(
16
);
for
(
i
=
string
.
length
;
i
<
4
;
i
+=
1
)
{
string
=
"
0
"
+
string
;
}
return
string
;
};
};
return
S4
()
+
S4
()
+
"
-
"
+
S4
()
+
"
-
"
+
S4
()
+
"
-
"
+
S4
()
+
"
-
"
+
S4
()
+
S4
()
+
S4
();
that
.
validateState
=
function
()
{
};
if
(
typeof
priv
.
url
!==
"
string
"
||
priv
.
url
===
""
)
{
return
"
The webDav server URL is not provided
"
;
// /**
// * Clones an object in deep
// * @method clone
// * @param {object} object The object to clone
// * @return {object} The cloned object
// */
// priv.clone = function (object) {
// var tmp = JSON.stringify(object);
// if (tmp === undefined) {
// return undefined;
// }
// return JSON.parse(tmp);
// };
/**
* 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
)
);
}
}
}
if
(
priv
.
encoded_login
===
null
)
{
return
split_string
.
join
(
list_of_replacement
[
0
][
1
]);
return
"
Impossible to create the authorization
"
;
};
/**
* Changes spaces to %20, / to %2f, % to %25 and ? to %3f
* @method secureName
* @param {string} name The name to secure
* @return {string} The secured name
*/
priv
.
secureName
=
function
(
name
)
{
return
priv
.
recursiveReplace
(
name
,
[
[
"
"
,
"
%20
"
],
[
"
/
"
,
"
%2F
"
],
[
"
%
"
,
"
%25
"
],
[
"
?
"
,
"
%3F
"
]
]);
};
/**
* Restores the original name from a secured name
* @method restoreName
* @param {string} secured_name The secured name to restore
* @return {string} The original name
*/
priv
.
restoreName
=
function
(
secured_name
)
{
return
priv
.
recursiveReplace
(
secured_name
,
[
[
"
%20
"
,
"
"
],
[
"
%2F
"
,
"
/
"
],
[
"
%25
"
,
"
%
"
],
[
"
%3F
"
,
"
?
"
]
]);
};
/**
* 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
)
!==
"
_
"
)
{
if
(
separator_index
!==
-
1
)
{
separator_index
=
new
TypeError
(
"
Corrupted file name
"
);
separator_index
.
status
=
24
;
throw
separator_index
;
}
separator_index
=
index
;
}
}
});
return
""
;
if
(
separator_index
===
-
1
)
{
return
[
priv
.
restoreName
(
priv
.
restoreName
(
file_name
).
split
(
"
_.
"
).
join
(
"
.
"
))];
}
return
[
priv
.
restoreName
(
priv
.
restoreName
(
split
.
slice
(
0
,
separator_index
+
1
).
join
(
"
.
"
)
).
split
(
"
_.
"
).
join
(
"
.
"
)),
priv
.
restoreName
(
priv
.
restoreName
(
split
.
slice
(
separator_index
+
1
).
join
(
"
.
"
)
).
split
(
"
_.
"
).
join
(
"
.
"
))
];
};
/**
* Removes the last character if it is a "/". "/a/b/c/" become "/a/b/c"
* @method removeSlashIfLast
* @param {string} string The string to modify
* @return {string} The modified string
*/
priv
.
removeSlashIfLast
=
function
(
string
)
{
if
(
string
[
string
.
length
-
1
]
===
"
/
"
)
{
return
string
.
slice
(
0
,
-
1
);
}
return
string
;
};
/**
* Modify an ajax object to add default values
* @method makeAjaxObject
* @param {string} file_name The file name to add to the url
* @param {object} ajax_object The ajax object to override
* @return {object} A new ajax object with default values
*/
priv
.
makeAjaxObject
=
function
(
file_name
,
method
,
ajax_object
)
{
ajax_object
.
type
=
method
||
ajax_object
.
type
||
"
GET
"
;
ajax_object
.
url
=
priv
.
url
+
"
/
"
+
priv
.
secureName
(
file_name
)
+
"
?_=
"
+
Date
.
now
();
ajax_object
.
async
=
ajax_object
.
async
===
false
?
false
:
true
;
ajax_object
.
crossdomain
=
ajax_object
.
crossdomain
===
false
?
false
:
true
;
ajax_object
.
headers
=
ajax_object
.
headers
||
{};
ajax_object
.
headers
.
Authorization
=
ajax_object
.
headers
.
Authorization
||
priv
.
encoded_login
;
return
ajax_object
;
};
/**
* Runs all ajax requests for davStorage
* @method ajax
* @param {string} doc_id The document id
* @param {string} attachment_id The attachment id, can be undefined
* @param {string} method The request method
* @param {object} ajax_object The request parameters (optional)
*/
priv
.
ajax
=
function
(
doc_id
,
attachment_id
,
method
,
ajax_object
)
{
var
new_ajax_object
=
JSON
.
parse
(
JSON
.
stringify
(
ajax_object
)
||
"
{}
"
);
return
$
.
ajax
(
priv
.
makeAjaxObject
(
priv
.
idsToFileName
(
doc_id
||
''
,
attachment_id
),
method
,
new_ajax_object
));
//.always(then || function () {});
};
/**
* Creates error objects for this storage
* @method createError
* @param {string} url url to clean up
* @return {object} error The error object
*/
priv
.
createError
=
function
(
status
,
message
,
reason
)
{
var
error
=
{
"
status
"
:
status
,
"
message
"
:
message
,
"
reason
"
:
reason
};
};
switch
(
status
)
{
case
404
:
// TOOLS //
error
.
statusText
=
"
Not found
"
;
/**
break
;
* Generate a new uuid
case
405
:
* @method generateUuid
error
.
statusText
=
"
Method Not Allowed
"
;
* @return {string} The new uuid
break
;
*/
case
409
:
priv
.
generateUuid
=
function
()
{
error
.
statusText
=
"
Conflicts
"
;
var
S4
=
function
()
{
break
;
/* 65536 */
case
24
:
var
i
,
string
=
Math
.
floor
(
error
.
statusText
=
"
Corrupted Document
"
;
Math
.
random
()
*
0x10000
break
;
).
toString
(
16
);
}
for
(
i
=
string
.
length
;
i
<
4
;
i
+=
1
)
{
error
.
error
=
error
.
statusText
.
toLowerCase
().
split
(
"
"
).
join
(
"
_
"
);
string
=
"
0
"
+
string
;
return
error
;
};
/**
* Converts ajax error object to a JIO error object
* @method ajaxErrorToJioError
* @param {object} ajax_error_object The ajax error object
* @param {string} message The error message
* @param {string} reason The error reason
* @return {object} The JIO error object
*/
priv
.
ajaxErrorToJioError
=
function
(
ajax_error_object
,
message
,
reason
)
{
var
jio_error_object
=
{};
jio_error_object
.
status
=
ajax_error_object
.
status
;
jio_error_object
.
statusText
=
ajax_error_object
.
statusText
;
jio_error_object
.
error
=
ajax_error_object
.
statusText
.
toLowerCase
().
split
(
"
"
).
join
(
"
_
"
);
jio_error_object
.
message
=
message
;
jio_error_object
.
reason
=
reason
;
return
jio_error_object
;
};
/**
* Function that create an object containing jQuery like callbacks
* @method makeJQLikeCallback
* @return {object} jQuery like callback methods
*/
priv
.
makeJQLikeCallback
=
function
()
{
var
result
=
null
,
emptyFun
=
function
()
{},
jql
=
{
"
respond
"
:
function
()
{
result
=
arguments
;
},
"
to_return
"
:
{
"
always
"
:
function
(
func
)
{
if
(
result
)
{
func
.
apply
(
func
,
result
);
jql
.
to_return
.
always
=
emptyFun
;
}
else
{
jql
.
respond
=
func
;
}
return
jql
.
to_return
;
},
"
then
"
:
function
(
func
)
{
if
(
result
)
{
func
(
result
[
1
]);
jql
.
to_return
.
then
=
emptyFun
;
}
else
{
jql
.
respond
=
function
(
err
,
response
)
{
func
(
response
);
};
}
return
jql
.
to_return
;
}
}
}
return
string
;
};
return
S4
()
+
S4
()
+
"
-
"
+
S4
()
+
"
-
"
+
S4
()
+
"
-
"
+
S4
()
+
"
-
"
+
S4
()
+
S4
()
+
S4
();
};
};
return
jql
;
};
// /**
// * Clones an object in deep
// DAV REQUESTS //
// * @method clone
/**
// * @param {object} object The object to clone
* Retrieve a document file
// * @return {object} The cloned object
* @method dav.getDocument
// */
* @param {string} doc_id The document id
// priv.clone = function (object) {
*/
// var tmp = JSON.stringify(object);
dav
.
getDocument
=
function
(
doc_id
)
{
// if (tmp === undefined) {
var
doc
,
jql
=
priv
.
makeJQLikeCallback
(),
error
=
null
;
// return undefined;
priv
.
ajax
(
doc_id
,
undefined
,
"
GET
"
).
always
(
function
(
one
,
state
,
three
)
{
// }
if
(
state
!==
"
success
"
)
{
// return JSON.parse(tmp);
error
=
priv
.
ajaxErrorToJioError
(
// };
one
,
"
Cannot retrieve document
"
,
/**
"
Unknown
"
* Replace substrings to another strings
);
* @method recursiveReplace
if
(
one
.
status
===
404
)
{
* @param {string} string The string to do replacement
error
.
reason
=
"
Not Found
"
;
* @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
jql
.
respond
(
error
,
undefined
);
}
}
try
{
return
split_string
.
join
(
list_of_replacement
[
0
][
1
]);
doc
=
JSON
.
parse
(
one
);
};
}
catch
(
e
)
{
return
jql
.
respond
(
priv
.
createError
(
/**
24
,
* Changes spaces to %20, / to %2f, % to %25 and ? to %3f
"
Cannot parse document
"
,
* @method secureName
"
Document is corrupted
"
* @param {string} name The name to secure
),
undefined
);
* @return {string} The secured name
*/
priv
.
secureName
=
function
(
name
)
{
return
priv
.
recursiveReplace
(
name
,
[
[
"
"
,
"
%20
"
],
[
"
/
"
,
"
%2F
"
],
[
"
%
"
,
"
%25
"
],
[
"
?
"
,
"
%3F
"
]
]);
};
/**
* Restores the original name from a secured name
* @method restoreName
* @param {string} secured_name The secured name to restore
* @return {string} The original name
*/
priv
.
restoreName
=
function
(
secured_name
)
{
return
priv
.
recursiveReplace
(
secured_name
,
[
[
"
%20
"
,
"
"
],
[
"
%2F
"
,
"
/
"
],
[
"
%25
"
,
"
%
"
],
[
"
%3F
"
,
"
?
"
]
]);
};
/**
* 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
;
}
}
// document health is good
return
doc_id
;
return
jql
.
respond
(
undefined
,
doc
);
};
});
return
jql
.
to_return
;
/**
};
* Convert a file name to a document id (and attachment id if there)
* @method fileNameToIds
/**
* @param {string} file_name The file name to convert
* Retrieve an attachment file
* @return {array} ["document id", "attachment id"] or ["document id"]
* @method dav.getAttachment
*/
* @param {string} doc_id The document id
priv
.
fileNameToIds
=
function
(
file_name
)
{
* @param {string} attachment_id The attachment id
var
separator_index
=
-
1
,
split
=
file_name
.
split
(
"
.
"
);
*/
split
.
slice
(
0
,
-
1
).
forEach
(
function
(
file_name_part
,
index
)
{
dav
.
getAttachment
=
function
(
doc_id
,
attachment_id
)
{
if
(
file_name_part
.
slice
(
-
1
)
!==
"
_
"
)
{
var
jql
=
priv
.
makeJQLikeCallback
(),
error
=
null
;
if
(
separator_index
!==
-
1
)
{
priv
.
ajax
(
separator_index
=
new
TypeError
(
"
Corrupted file name
"
);
doc_id
,
separator_index
.
status
=
24
;
attachment_id
,
throw
separator_index
;
"
GET
"
}
).
always
(
function
(
one
,
state
,
three
)
{
separator_index
=
index
;
if
(
state
!==
"
success
"
)
{
error
=
priv
.
ajaxErrorToJioError
(
one
,
"
Cannot retrieve attachment
"
,
"
Unknown
"
);
if
(
one
.
status
===
404
)
{
error
.
reason
=
"
Not Found
"
;
}
}
return
jql
.
respond
(
error
,
undefined
);
}
return
jql
.
respond
(
undefined
,
one
);
});
return
jql
.
to_return
;
};
/**
* Uploads a document file
* @method dav.putDocument
* @param {object} doc The document object
*/
dav
.
putDocument
=
function
(
doc
)
{
var
jql
=
priv
.
makeJQLikeCallback
();
priv
.
ajax
(
doc
.
_id
,
undefined
,
"
PUT
"
,
{
"
dataType
"
:
"
text
"
,
"
data
"
:
JSON
.
stringify
(
doc
)
}).
always
(
function
(
one
,
state
,
three
)
{
if
(
state
!==
"
success
"
)
{
return
jql
.
respond
(
priv
.
ajaxErrorToJioError
(
one
,
"
Cannot upload document
"
,
"
Unknown
"
),
undefined
);
}
jql
.
respond
(
undefined
,
{
"
ok
"
:
true
,
"
id
"
:
doc
.
_id
});
});
return
jql
.
to_return
;
};
/**
* Uploads an attachment file
* @method dav.putAttachment
* @param {string} doc_id The document id
* @param {string} attachment_id The attachment id
* @param {string} data The attachment data
*/
dav
.
putAttachment
=
function
(
doc_id
,
attachment_id
,
data
)
{
var
jql
=
priv
.
makeJQLikeCallback
();
priv
.
ajax
(
doc_id
,
attachment_id
,
"
PUT
"
,
{
"
dataType
"
:
"
text
"
,
"
data
"
:
data
}).
always
(
function
(
one
,
state
,
three
)
{
if
(
state
!==
"
success
"
)
{
return
jql
.
respond
(
priv
.
ajaxErrorToJioError
(
one
,
"
Cannot upload attachment
"
,
"
Unknown
"
),
undefined
);
}
return
jql
.
respond
(
undefined
,
{
"
ok
"
:
true
,
"
id
"
:
doc_id
,
"
attachment
"
:
attachment_id
});
});
});
if
(
separator_index
===
-
1
)
{
return
jql
.
to_return
;
return
[
priv
.
restoreName
(
priv
.
restoreName
(
};
file_name
).
split
(
"
_.
"
).
join
(
"
.
"
))];
/**
* Deletes a document file
* @method dav.removeDocument
* @param {string} doc_id The document id
*/
dav
.
removeDocument
=
function
(
doc_id
)
{
var
jql
=
priv
.
makeJQLikeCallback
(),
error
=
null
;
priv
.
ajax
(
doc_id
,
undefined
,
"
DELETE
"
).
always
(
function
(
one
,
state
,
three
)
{
if
(
state
!==
"
success
"
)
{
error
=
priv
.
ajaxErrorToJioError
(
one
,
"
Cannot delete document
"
,
"
Unknown
"
);
if
(
one
.
status
===
404
)
{
error
.
reason
=
"
Not Found
"
;
}
return
jql
.
respond
(
error
,
undefined
);
}
}
jql
.
respond
(
undefined
,
{
"
ok
"
:
true
,
"
id
"
:
doc_id
});
return
[
});
priv
.
restoreName
(
priv
.
restoreName
(
return
jql
.
to_return
;
split
.
slice
(
0
,
separator_index
+
1
).
join
(
"
.
"
)
};
).
split
(
"
_.
"
).
join
(
"
.
"
)),
priv
.
restoreName
(
priv
.
restoreName
(
/**
split
.
slice
(
separator_index
+
1
).
join
(
"
.
"
)
* Deletes an attachment file
).
split
(
"
_.
"
).
join
(
"
.
"
))
* @method dav.removeAttachment
];
* @param {string} doc_id The document id
};
* @param {string} attachment_id The attachment id
*/
/**
dav
.
removeAttachment
=
function
(
doc_id
,
attachment_id
)
{
* Removes the last character if it is a "/". "/a/b/c/" become "/a/b/c"
var
jql
=
priv
.
makeJQLikeCallback
(),
error
=
null
;
* @method removeSlashIfLast
priv
.
ajax
(
* @param {string} string The string to modify
doc_id
,
* @return {string} The modified string
attachment_id
,
*/
"
DELETE
"
priv
.
removeSlashIfLast
=
function
(
string
)
{
).
always
(
function
(
one
,
state
,
three
)
{
if
(
string
[
string
.
length
-
1
]
===
"
/
"
)
{
if
(
state
!==
"
success
"
)
{
return
string
.
slice
(
0
,
-
1
);
error
=
priv
.
ajaxErrorToJioError
(
one
,
"
Cannot delete attachment
"
,
"
Unknown
"
);
if
(
one
.
status
===
404
)
{
error
.
reason
=
"
Not Found
"
;
}
return
jql
.
respond
(
error
,
undefined
);
}
jql
.
respond
(
undefined
,
{
"
ok
"
:
true
,
"
id
"
:
doc_id
});
});
return
jql
.
to_return
;
};
/**
* Get a list of document file
* @method dav.allDocs
*/
dav
.
allDocs
=
function
()
{
var
jql
=
priv
.
makeJQLikeCallback
(),
rows
=
[];
priv
.
ajax
(
undefined
,
undefined
,
"
PROPFIND
"
,
{
"
dataType
"
:
"
xml
"
,
"
headers
"
:
{
"
Depth
"
:
1
}
}).
always
(
function
(
one
,
state
,
three
)
{
var
response
,
len
;
if
(
state
!==
"
success
"
)
{
return
jql
.
respond
(
priv
.
ajaxErrorToJioError
(
one
,
"
Cannot get the document list
"
,
"
Unknown
"
),
undefined
);
}
}
response
=
$
(
one
).
find
(
"
D
\\
:response, response
"
);
return
string
;
len
=
response
.
length
;
};
if
(
len
===
1
)
{
return
jql
.
respond
({
"
total_rows
"
:
0
,
"
rows
"
:
[]});
/**
* Modify an ajax object to add default values
* @method makeAjaxObject
* @param {string} file_name The file name to add to the url
* @param {object} ajax_object The ajax object to override
* @return {object} A new ajax object with default values
*/
priv
.
makeAjaxObject
=
function
(
file_name
,
method
,
ajax_object
)
{
ajax_object
.
type
=
method
||
ajax_object
.
type
||
"
GET
"
;
ajax_object
.
url
=
priv
.
url
+
"
/
"
+
priv
.
secureName
(
file_name
)
+
"
?_=
"
+
Date
.
now
();
ajax_object
.
async
=
ajax_object
.
async
===
false
?
false
:
true
;
ajax_object
.
crossdomain
=
ajax_object
.
crossdomain
===
false
?
false
:
true
;
ajax_object
.
headers
=
ajax_object
.
headers
||
{};
ajax_object
.
headers
.
Authorization
=
ajax_object
.
headers
.
Authorization
||
priv
.
encoded_login
;
return
ajax_object
;
};
/**
* Runs all ajax requests for davStorage
* @method ajax
* @param {string} doc_id The document id
* @param {string} attachment_id The attachment id, can be undefined
* @param {string} method The request method
* @param {object} ajax_object The request parameters (optional)
*/
priv
.
ajax
=
function
(
doc_id
,
attachment_id
,
method
,
ajax_object
)
{
var
new_ajax_object
=
JSON
.
parse
(
JSON
.
stringify
(
ajax_object
)
||
"
{}
"
);
return
$
.
ajax
(
priv
.
makeAjaxObject
(
priv
.
idsToFileName
(
doc_id
||
''
,
attachment_id
),
method
,
new_ajax_object
));
//.always(then || function () {});
};
/**
* Creates error objects for this storage
* @method createError
* @param {string} url url to clean up
* @return {object} error The error object
*/
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
;
}
}
response
.
each
(
function
(
i
,
data
)
{
error
.
error
=
error
.
statusText
.
toLowerCase
().
split
(
"
"
).
join
(
"
_
"
);
var
row
;
return
error
;
if
(
i
>
0
)
{
// exclude parent folder
};
row
=
{
"
id
"
:
""
,
/**
"
key
"
:
""
,
* Converts ajax error object to a JIO error object
"
value
"
:
{}
* @method ajaxErrorToJioError
};
* @param {object} ajax_error_object The ajax error object
$
(
data
).
find
(
"
D
\\
:href, href
"
).
each
(
function
()
{
* @param {string} message The error message
row
.
id
=
$
(
this
).
text
().
split
(
'
/
'
).
slice
(
-
1
)[
0
];
* @param {string} reason The error reason
try
{
* @return {object} The JIO error object
row
.
id
=
priv
.
fileNameToIds
(
row
.
id
);
*/
}
catch
(
e
)
{
priv
.
ajaxErrorToJioError
=
function
(
ajax_error_object
,
message
,
reason
)
{
if
(
e
.
name
===
"
TypeError
"
&&
e
.
status
===
24
)
{
var
jio_error_object
=
{};
return
;
jio_error_object
.
status
=
ajax_error_object
.
status
;
}
jio_error_object
.
statusText
=
ajax_error_object
.
statusText
;
throw
e
;
jio_error_object
.
error
=
ajax_error_object
.
statusText
.
toLowerCase
().
split
(
"
"
).
join
(
"
_
"
);
jio_error_object
.
message
=
message
;
jio_error_object
.
reason
=
reason
;
return
jio_error_object
;
};
/**
* Function that create an object containing jQuery like callbacks
* @method makeJQLikeCallback
* @return {object} jQuery like callback methods
*/
priv
.
makeJQLikeCallback
=
function
()
{
var
result
=
null
,
emptyFun
=
function
()
{},
jql
=
{
"
respond
"
:
function
()
{
result
=
arguments
;
},
"
to_return
"
:
{
"
always
"
:
function
(
func
)
{
if
(
result
)
{
func
.
apply
(
func
,
result
);
jql
.
to_return
.
always
=
emptyFun
;
}
else
{
jql
.
respond
=
func
;
}
}
if
(
row
.
id
.
length
!==
1
)
{
return
jql
.
to_return
;
row
=
undefined
;
},
"
then
"
:
function
(
func
)
{
if
(
result
)
{
func
(
result
[
1
]);
jql
.
to_return
.
then
=
emptyFun
;
}
else
{
}
else
{
row
.
id
=
row
.
id
[
0
];
jql
.
respond
=
function
(
err
,
response
)
{
row
.
key
=
row
.
id
;
func
(
response
);
};
}
}
});
return
jql
.
to_return
;
if
(
row
!==
undefined
)
{
rows
.
push
(
row
);
}
}
}
}
};
return
jql
;
};
// DAV REQUESTS //
/**
* Retrieve a document file
* @method dav.getDocument
* @param {string} doc_id The document id
*/
dav
.
getDocument
=
function
(
doc_id
)
{
var
doc
,
jql
=
priv
.
makeJQLikeCallback
(),
error
=
null
;
priv
.
ajax
(
doc_id
,
undefined
,
"
GET
"
).
always
(
function
(
one
,
state
,
three
)
{
if
(
state
!==
"
success
"
)
{
error
=
priv
.
ajaxErrorToJioError
(
one
,
"
Cannot retrieve document
"
,
"
Unknown
"
);
if
(
one
.
status
===
404
)
{
error
.
reason
=
"
Not Found
"
;
}
return
jql
.
respond
(
error
,
undefined
);
}
try
{
doc
=
JSON
.
parse
(
one
);
}
catch
(
e
)
{
return
jql
.
respond
(
priv
.
createError
(
24
,
"
Cannot parse document
"
,
"
Document is corrupted
"
),
undefined
);
}
// document health is good
return
jql
.
respond
(
undefined
,
doc
);
});
});
jql
.
respond
(
undefined
,
{
return
jql
.
to_return
;
"
total_rows
"
:
rows
.
length
,
};
"
rows
"
:
rows
/**
* Retrieve an attachment file
* @method dav.getAttachment
* @param {string} doc_id The document id
* @param {string} attachment_id The attachment id
*/
dav
.
getAttachment
=
function
(
doc_id
,
attachment_id
)
{
var
jql
=
priv
.
makeJQLikeCallback
(),
error
=
null
;
priv
.
ajax
(
doc_id
,
attachment_id
,
"
GET
"
).
always
(
function
(
one
,
state
,
three
)
{
if
(
state
!==
"
success
"
)
{
error
=
priv
.
ajaxErrorToJioError
(
one
,
"
Cannot retrieve attachment
"
,
"
Unknown
"
);
if
(
one
.
status
===
404
)
{
error
.
reason
=
"
Not Found
"
;
}
return
jql
.
respond
(
error
,
undefined
);
}
return
jql
.
respond
(
undefined
,
one
);
});
});
});
return
jql
.
to_return
;
return
jql
.
to_return
;
};
};
/**
// JIO COMMANDS //
* Uploads a document file
* @method dav.putDocument
// wedDav methods rfc4918 (short summary)
* @param {object} doc The document object
// COPY Reproduces single resources (files) and collections (directory
*/
// trees). Will overwrite files (if specified by request) but will
dav
.
putDocument
=
function
(
doc
)
{
// respond 209 (Conflict) if it would overwrite a tree
var
jql
=
priv
.
makeJQLikeCallback
();
// DELETE deletes files and directory trees
priv
.
ajax
(
doc
.
_id
,
undefined
,
"
PUT
"
,
{
// GET just the vanilla HTTP/1.1 behaviour
"
dataType
"
:
"
text
"
,
// HEAD ditto
"
data
"
:
JSON
.
stringify
(
doc
)
// LOCK locks a resources
}).
always
(
function
(
one
,
state
,
three
)
{
// MKCOL creates a directory
if
(
state
!==
"
success
"
)
{
// MOVE Moves (rename or copy) a file or a directory tree. Will
return
jql
.
respond
(
priv
.
ajaxErrorToJioError
(
// 'overwrite' files (if specified by the request) but will respond
one
,
// 209 (Conflict) if it would overwrite a tree.
"
Cannot upload document
"
,
// OPTIONS If WebDAV is enabled and available for the path this reports the
"
Unknown
"
// WebDAV extension methods
),
undefined
);
// PROPFIND Retrieves the requested file characteristics, DAV lock status
// and 'dead' properties for individual files, a directory and its
// child files, or a directory tree
// PROPPATCHset and remove 'dead' meta-data properties
// PUT Update or create resource or collections
// UNLOCK unlocks a resource
// Notes: all Ajax requests should be CORS (cross-domain)
// adding custom headers triggers preflight OPTIONS request!
// http://remysharp.com/2011/04/21/getting-cors-working/
/**
* Creates a new document
* @method post
* @param {object} command The JIO command
*/
that
.
post
=
function
(
command
)
{
var
doc_id
=
command
.
getDocId
()
||
priv
.
generateUuid
();
dav
.
getDocument
(
doc_id
).
always
(
function
(
err
,
response
)
{
if
(
err
)
{
if
(
err
.
status
===
404
)
{
// the document does not already exist
// updating document
var
doc
=
command
.
cloneDoc
();
doc
.
_id
=
doc_id
;
return
dav
.
putDocument
(
doc
).
always
(
function
(
err
,
response
)
{
if
(
err
)
{
return
that
.
retry
(
err
);
}
return
that
.
success
(
response
);
});
}
}
if
(
err
.
status
===
24
)
{
jql
.
respond
(
undefined
,
{
"
ok
"
:
true
,
"
id
"
:
doc
.
_id
});
return
that
.
error
(
err
);
});
return
jql
.
to_return
;
};
/**
* Uploads an attachment file
* @method dav.putAttachment
* @param {string} doc_id The document id
* @param {string} attachment_id The attachment id
* @param {string} data The attachment data
*/
dav
.
putAttachment
=
function
(
doc_id
,
attachment_id
,
data
)
{
var
jql
=
priv
.
makeJQLikeCallback
();
priv
.
ajax
(
doc_id
,
attachment_id
,
"
PUT
"
,
{
"
dataType
"
:
"
text
"
,
"
data
"
:
data
}).
always
(
function
(
one
,
state
,
three
)
{
if
(
state
!==
"
success
"
)
{
return
jql
.
respond
(
priv
.
ajaxErrorToJioError
(
one
,
"
Cannot upload attachment
"
,
"
Unknown
"
),
undefined
);
}
}
// an error occured
return
jql
.
respond
(
undefined
,
{
return
that
.
retry
(
err
);
"
ok
"
:
true
,
}
"
id
"
:
doc_id
,
// the document already exists
"
attachment
"
:
attachment_id
return
that
.
error
(
priv
.
createError
(
});
405
,
});
"
Cannot create document
"
,
return
jql
.
to_return
;
"
Document already exists
"
};
));
});
/**
};
* Deletes a document file
* @method dav.removeDocument
/**
* @param {string} doc_id The document id
* Creates or updates a document
*/
* @method put
dav
.
removeDocument
=
function
(
doc_id
)
{
* @param {object} command The JIO command
var
jql
=
priv
.
makeJQLikeCallback
(),
error
=
null
;
*/
priv
.
ajax
(
that
.
put
=
function
(
command
)
{
doc_id
,
dav
.
putDocument
(
command
.
cloneDoc
()).
always
(
function
(
err
,
response
)
{
undefined
,
if
(
err
)
{
"
DELETE
"
// an error occured
).
always
(
function
(
one
,
state
,
three
)
{
return
that
.
retry
(
err
);
if
(
state
!==
"
success
"
)
{
}
error
=
priv
.
ajaxErrorToJioError
(
// document updated
one
,
return
that
.
success
(
response
);
"
Cannot delete document
"
,
});
"
Unknown
"
};
);
if
(
one
.
status
===
404
)
{
/**
error
.
reason
=
"
Not Found
"
;
* Add an attachment to a document
}
* @method putAttachment
return
jql
.
respond
(
error
,
undefined
);
* @param {object} command The JIO command
*/
that
.
putAttachment
=
function
(
command
)
{
var
doc
=
null
,
doc_id
=
command
.
getDocId
(),
attachment_id
,
tmp
;
attachment_id
=
command
.
getAttachmentId
();
dav
.
getDocument
(
doc_id
).
always
(
function
(
err
,
response
)
{
if
(
err
)
{
// document not found or error
tmp
=
that
.
retry
;
if
(
err
.
status
===
404
||
err
.
status
===
24
)
{
tmp
=
that
.
error
;
}
}
return
tmp
(
err
);
jql
.
respond
(
undefined
,
{
"
ok
"
:
true
,
"
id
"
:
doc_id
});
}
});
doc
=
response
;
return
jql
.
to_return
;
doc
.
_attachments
=
doc
.
_attachments
||
{};
};
doc
.
_attachments
[
attachment_id
]
=
{
"
length
"
:
command
.
getAttachmentLength
(),
/**
"
digest
"
:
"
md5-
"
+
command
.
md5SumAttachmentData
(),
* Deletes an attachment file
"
content_type
"
:
command
.
getAttachmentMimeType
()
* @method dav.removeAttachment
};
* @param {string} doc_id The document id
// put the attachment
* @param {string} attachment_id The attachment id
dav
.
putAttachment
(
*/
dav
.
removeAttachment
=
function
(
doc_id
,
attachment_id
)
{
var
jql
=
priv
.
makeJQLikeCallback
(),
error
=
null
;
priv
.
ajax
(
doc_id
,
doc_id
,
attachment_id
,
attachment_id
,
command
.
getAttachmentData
()
"
DELETE
"
).
always
(
function
(
err
,
response
)
{
).
always
(
function
(
one
,
state
,
three
)
{
if
(
state
!==
"
success
"
)
{
error
=
priv
.
ajaxErrorToJioError
(
one
,
"
Cannot delete attachment
"
,
"
Unknown
"
);
if
(
one
.
status
===
404
)
{
error
.
reason
=
"
Not Found
"
;
}
return
jql
.
respond
(
error
,
undefined
);
}
jql
.
respond
(
undefined
,
{
"
ok
"
:
true
,
"
id
"
:
doc_id
});
});
return
jql
.
to_return
;
};
/**
* Get a list of document file
* @method dav.allDocs
*/
dav
.
allDocs
=
function
()
{
var
jql
=
priv
.
makeJQLikeCallback
(),
rows
=
[];
priv
.
ajax
(
undefined
,
undefined
,
"
PROPFIND
"
,
{
"
dataType
"
:
"
xml
"
,
"
headers
"
:
{
"
Depth
"
:
1
}
}).
always
(
function
(
one
,
state
,
three
)
{
var
response
,
len
;
if
(
state
!==
"
success
"
)
{
return
jql
.
respond
(
priv
.
ajaxErrorToJioError
(
one
,
"
Cannot get the document list
"
,
"
Unknown
"
),
undefined
);
}
response
=
$
(
one
).
find
(
"
D
\\
:response, response
"
);
len
=
response
.
length
;
if
(
len
===
1
)
{
return
jql
.
respond
({
"
total_rows
"
:
0
,
"
rows
"
:
[]});
}
response
.
each
(
function
(
i
,
data
)
{
var
row
;
if
(
i
>
0
)
{
// exclude parent folder
row
=
{
"
id
"
:
""
,
"
key
"
:
""
,
"
value
"
:
{}
};
$
(
data
).
find
(
"
D
\\
:href, href
"
).
each
(
function
()
{
row
.
id
=
$
(
this
).
text
().
split
(
'
/
'
).
slice
(
-
1
)[
0
];
try
{
row
.
id
=
priv
.
fileNameToIds
(
row
.
id
);
}
catch
(
e
)
{
if
(
e
.
name
===
"
TypeError
"
&&
e
.
status
===
24
)
{
return
;
}
throw
e
;
}
if
(
row
.
id
.
length
!==
1
)
{
row
=
undefined
;
}
else
{
row
.
id
=
row
.
id
[
0
];
row
.
key
=
row
.
id
;
}
});
if
(
row
!==
undefined
)
{
rows
.
push
(
row
);
}
}
});
jql
.
respond
(
undefined
,
{
"
total_rows
"
:
rows
.
length
,
"
rows
"
:
rows
});
});
return
jql
.
to_return
;
};
// JIO COMMANDS //
// wedDav methods rfc4918 (short summary)
// COPY Reproduces single resources (files) and collections (directory
// trees). Will overwrite files (if specified by request) but will
// respond 209 (Conflict) if it would overwrite a tree
// DELETE deletes files and directory trees
// GET just the vanilla HTTP/1.1 behaviour
// HEAD ditto
// LOCK locks a resources
// MKCOL creates a directory
// MOVE Moves (rename or copy) a file or a directory tree. Will
// 'overwrite' files (if specified by the request) but will respond
// 209 (Conflict) if it would overwrite a tree.
// OPTIONS If WebDAV is enabled and available for the path this reports the
// WebDAV extension methods
// PROPFIND Retrieves the requested file characteristics, DAV lock status
// and 'dead' properties for individual files, a directory and its
// child files, or a directory tree
// PROPPATCHset and remove 'dead' meta-data properties
// PUT Update or create resource or collections
// UNLOCK unlocks a resource
// Notes: all Ajax requests should be CORS (cross-domain)
// adding custom headers triggers preflight OPTIONS request!
// http://remysharp.com/2011/04/21/getting-cors-working/
/**
* Creates a new document
* @method post
* @param {object} command The JIO command
*/
that
.
post
=
function
(
command
)
{
var
doc_id
=
command
.
getDocId
()
||
priv
.
generateUuid
();
dav
.
getDocument
(
doc_id
).
always
(
function
(
err
,
response
)
{
if
(
err
)
{
if
(
err
)
{
if
(
err
.
status
===
404
)
{
// the document does not already exist
// updating document
var
doc
=
command
.
cloneDoc
();
doc
.
_id
=
doc_id
;
return
dav
.
putDocument
(
doc
).
always
(
function
(
err
,
response
)
{
if
(
err
)
{
return
that
.
retry
(
err
);
}
return
that
.
success
(
response
);
});
}
if
(
err
.
status
===
24
)
{
return
that
.
error
(
err
);
}
// an error occured
// an error occured
return
that
.
retry
(
err
);
return
that
.
retry
(
err
);
}
}
// update the document
// the document already exists
dav
.
putDocument
(
doc
).
always
(
function
(
err
,
response
)
{
return
that
.
error
(
priv
.
createError
(
405
,
"
Cannot create document
"
,
"
Document already exists
"
));
});
};
/**
* Creates or updates a document
* @method put
* @param {object} command The JIO command
*/
that
.
put
=
function
(
command
)
{
dav
.
putDocument
(
command
.
cloneDoc
()).
always
(
function
(
err
,
response
)
{
if
(
err
)
{
// an error occured
return
that
.
retry
(
err
);
}
// document updated
return
that
.
success
(
response
);
});
};
/**
* Add an attachment to a document
* @method putAttachment
* @param {object} command The JIO command
*/
that
.
putAttachment
=
function
(
command
)
{
var
doc
=
null
,
doc_id
=
command
.
getDocId
(),
attachment_id
,
tmp
;
attachment_id
=
command
.
getAttachmentId
();
dav
.
getDocument
(
doc_id
).
always
(
function
(
err
,
response
)
{
if
(
err
)
{
// document not found or error
tmp
=
that
.
retry
;
if
(
err
.
status
===
404
||
err
.
status
===
24
)
{
tmp
=
that
.
error
;
}
return
tmp
(
err
);
}
doc
=
response
;
doc
.
_attachments
=
doc
.
_attachments
||
{};
doc
.
_attachments
[
attachment_id
]
=
{
"
length
"
:
command
.
getAttachmentLength
(),
"
digest
"
:
"
md5-
"
+
command
.
md5SumAttachmentData
(),
"
content_type
"
:
command
.
getAttachmentMimeType
()
};
// put the attachment
dav
.
putAttachment
(
doc_id
,
attachment_id
,
command
.
getAttachmentData
()
).
always
(
function
(
err
,
response
)
{
if
(
err
)
{
if
(
err
)
{
// an error occured
return
that
.
retry
(
err
);
return
that
.
retry
(
err
);
}
}
response
.
attachment
=
attachment_id
;
// update the document
return
that
.
success
(
response
);
dav
.
putDocument
(
doc
).
always
(
function
(
err
,
response
)
{
if
(
err
)
{
return
that
.
retry
(
err
);
}
response
.
attachment
=
attachment_id
;
return
that
.
success
(
response
);
});
});
});
});
});
});
};
/**
* Get a document
* @method get
* @param {object} command The JIO command
*/
that
.
get
=
function
(
command
)
{
dav
.
getDocument
(
command
.
getDocId
()).
always
(
function
(
err
,
response
)
{
if
(
err
)
{
if
(
err
.
status
===
404
||
err
.
status
===
24
)
{
return
that
.
error
(
err
);
}
return
that
.
retry
(
err
);
}
return
that
.
success
(
response
);
});
};
/**
* Get an attachment
* @method getAttachment
* @param {object} command The JIO command
*/
that
.
getAttachment
=
function
(
command
)
{
dav
.
getAttachment
(
command
.
getDocId
(),
command
.
getAttachmentId
()
).
always
(
function
(
err
,
response
)
{
if
(
err
)
{
if
(
err
.
status
===
404
)
{
return
that
.
error
(
err
);
}
return
that
.
retry
(
err
);
}
return
that
.
success
(
response
);
});
};
/**
* Remove a document
* @method remove
* @param {object} command The JIO command
*/
that
.
remove
=
function
(
command
)
{
var
doc_id
=
command
.
getDocId
(),
count
=
0
,
end
;
end
=
function
()
{
count
-=
1
;
if
(
count
===
0
)
{
that
.
success
({
"
ok
"
:
true
,
"
id
"
:
doc_id
});
}
};
};
dav
.
getDocument
(
doc_id
).
always
(
function
(
err
,
response
)
{
var
attachment_id
=
null
;
/**
if
(
err
)
{
* Get a document
if
(
err
.
status
===
404
)
{
* @method get
return
that
.
error
(
err
);
* @param {object} command The JIO command
}
*/
if
(
err
.
status
!==
24
)
{
// 24 -> corrupted document
that
.
get
=
function
(
command
)
{
dav
.
getDocument
(
command
.
getDocId
()).
always
(
function
(
err
,
response
)
{
if
(
err
)
{
if
(
err
.
status
===
404
||
err
.
status
===
24
)
{
return
that
.
error
(
err
);
}
return
that
.
retry
(
err
);
return
that
.
retry
(
err
);
}
}
response
=
{};
return
that
.
success
(
response
);
}
});
count
+=
2
;
};
dav
.
removeDocument
(
doc_id
).
always
(
function
(
err
,
response
)
{
/**
* Get an attachment
* @method getAttachment
* @param {object} command The JIO command
*/
that
.
getAttachment
=
function
(
command
)
{
dav
.
getAttachment
(
command
.
getDocId
(),
command
.
getAttachmentId
()
).
always
(
function
(
err
,
response
)
{
if
(
err
)
{
if
(
err
)
{
if
(
err
.
status
===
404
)
{
if
(
err
.
status
===
404
)
{
return
that
.
error
(
err
);
return
that
.
error
(
err
);
}
}
return
that
.
retry
(
err
);
return
that
.
retry
(
err
);
}
}
return
end
(
);
return
that
.
success
(
response
);
});
});
for
(
attachment_id
in
response
.
_attachments
)
{
};
if
(
response
.
_attachments
.
hasOwnProperty
(
attachment_id
))
{
count
+=
1
;
/**
dav
.
removeAttachment
(
* Remove a document
doc_id
,
* @method remove
attachment_id
* @param {object} command The JIO command
).
always
(
end
);
*/
that
.
remove
=
function
(
command
)
{
var
doc_id
=
command
.
getDocId
(),
count
=
0
,
end
;
end
=
function
()
{
count
-=
1
;
if
(
count
===
0
)
{
that
.
success
({
"
ok
"
:
true
,
"
id
"
:
doc_id
});
}
}
}
};
end
();
dav
.
getDocument
(
doc_id
).
always
(
function
(
err
,
response
)
{
});
var
attachment_id
=
null
;
};
if
(
err
)
{
if
(
err
.
status
===
404
)
{
/**
return
that
.
error
(
err
);
* Remove an attachment
}
* @method removeAttachment
if
(
err
.
status
!==
24
)
{
// 24 -> corrupted document
* @param {object} command The JIO command
return
that
.
retry
(
err
);
*/
}
that
.
removeAttachment
=
function
(
command
)
{
response
=
{};
var
doc_id
=
command
.
getDocId
(),
doc
,
attachment_id
;
attachment_id
=
command
.
getAttachmentId
();
dav
.
getDocument
(
doc_id
).
always
(
function
(
err
,
response
)
{
var
still_has_attachments
;
if
(
err
)
{
if
(
err
.
status
===
404
||
err
.
status
===
24
)
{
return
that
.
error
(
err
);
}
}
return
that
.
retry
(
err
);
count
+=
2
;
}
dav
.
removeDocument
(
doc_id
).
always
(
function
(
err
,
response
)
{
doc
=
response
;
if
(
err
)
{
if
(
typeof
(
doc
.
_attachments
||
{})[
attachment_id
]
!==
"
object
"
)
{
if
(
err
.
status
===
404
)
{
return
that
.
error
(
priv
.
createError
(
return
that
.
error
(
err
);
404
,
}
"
Cannot remove attachment
"
,
return
that
.
retry
(
err
);
"
Not Found
"
}
));
return
end
();
}
});
delete
doc
.
_attachments
[
attachment_id
];
for
(
attachment_id
in
response
.
_attachments
)
{
// check if there is still attachments
if
(
response
.
_attachments
.
hasOwnProperty
(
attachment_id
))
{
for
(
still_has_attachments
in
doc
.
_attachments
)
{
count
+=
1
;
if
(
doc
.
_attachments
.
hasOwnProperty
(
still_has_attachments
))
{
dav
.
removeAttachment
(
break
;
doc_id
,
attachment_id
).
always
(
end
);
}
}
}
}
end
();
if
(
still_has_attachments
===
undefined
)
{
});
delete
doc
.
_attachments
;
};
}
doc
.
_id
=
doc_id
;
/**
dav
.
putDocument
(
doc
).
always
(
function
(
err
,
response
)
{
* Remove an attachment
* @method removeAttachment
* @param {object} command The JIO command
*/
that
.
removeAttachment
=
function
(
command
)
{
var
doc_id
=
command
.
getDocId
(),
doc
,
attachment_id
;
attachment_id
=
command
.
getAttachmentId
();
dav
.
getDocument
(
doc_id
).
always
(
function
(
err
,
response
)
{
var
still_has_attachments
;
if
(
err
)
{
if
(
err
)
{
if
(
err
.
status
===
404
||
err
.
status
===
24
)
{
return
that
.
error
(
err
);
}
return
that
.
retry
(
err
);
return
that
.
retry
(
err
);
}
}
dav
.
removeAttachment
(
doc
=
response
;
doc_id
,
if
(
typeof
(
doc
.
_attachments
||
{})[
attachment_id
]
!==
"
object
"
)
{
attachment_id
return
that
.
error
(
priv
.
createError
(
).
always
(
function
(
err
,
response
)
{
404
,
that
.
success
({
"
ok
"
:
true
,
"
id
"
:
doc_id
,
"
attachment
"
:
attachment_id
});
"
Cannot remove attachment
"
,
"
Not Found
"
));
}
delete
doc
.
_attachments
[
attachment_id
];
// check if there is still attachments
for
(
still_has_attachments
in
doc
.
_attachments
)
{
if
(
doc
.
_attachments
.
hasOwnProperty
(
still_has_attachments
))
{
break
;
}
}
if
(
still_has_attachments
===
undefined
)
{
delete
doc
.
_attachments
;
}
doc
.
_id
=
doc_id
;
dav
.
putDocument
(
doc
).
always
(
function
(
err
,
response
)
{
if
(
err
)
{
return
that
.
retry
(
err
);
}
dav
.
removeAttachment
(
doc_id
,
attachment_id
).
always
(
function
(
err
,
response
)
{
that
.
success
({
"
ok
"
:
true
,
"
id
"
:
doc_id
,
"
attachment
"
:
attachment_id
});
});
});
});
});
});
});
};
/**
* Gets a document list from a distant dav storage
* Options:
* - {boolean} include_docs Also retrieve the actual document content.
* @method allDocs
* @param {object} command The JIO command
*/
that
.
allDocs
=
function
(
command
)
{
var
count
=
0
,
end
,
rows
;
end
=
function
()
{
count
-=
1
;
if
(
count
===
0
)
{
that
.
success
(
rows
);
}
};
};
dav
.
allDocs
().
always
(
function
(
err
,
response
)
{
if
(
err
)
{
/**
return
that
.
retry
(
err
);
* Gets a document list from a distant dav storage
}
* Options:
if
(
command
.
getOption
(
"
include_docs
"
)
===
true
)
{
* - {boolean} include_docs Also retrieve the actual document content.
count
+=
1
;
* @method allDocs
rows
=
response
;
* @param {object} command The JIO command
rows
.
rows
.
forEach
(
function
(
row
)
{
*/
that
.
allDocs
=
function
(
command
)
{
var
count
=
0
,
end
,
rows
;
end
=
function
()
{
count
-=
1
;
if
(
count
===
0
)
{
that
.
success
(
rows
);
}
};
dav
.
allDocs
().
always
(
function
(
err
,
response
)
{
if
(
err
)
{
return
that
.
retry
(
err
);
}
if
(
command
.
getOption
(
"
include_docs
"
)
===
true
)
{
count
+=
1
;
count
+=
1
;
dav
.
getDocument
(
rows
=
response
;
row
.
id
rows
.
rows
.
forEach
(
function
(
row
)
{
).
always
(
function
(
err
,
response
)
{
count
+=
1
;
if
(
err
)
{
dav
.
getDocument
(
if
(
err
.
status
===
404
||
err
.
status
===
24
)
{
row
.
id
return
that
.
error
(
err
);
).
always
(
function
(
err
,
response
)
{
if
(
err
)
{
if
(
err
.
status
===
404
||
err
.
status
===
24
)
{
return
that
.
error
(
err
);
}
return
that
.
retry
(
err
);
}
}
return
that
.
retry
(
err
);
row
.
doc
=
response
;
}
end
();
row
.
doc
=
response
;
});
end
();
});
});
});
end
();
end
();
}
else
{
}
else
{
that
.
success
(
response
);
that
.
success
(
response
);
}
}
});
});
};
};
priv
.
__init__
(
spec
);
return
that
;
});
priv
.
__init__
(
spec
);
}));
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