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