Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
John McEleney
/
casperjs
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
Commit
b84771ef
...
b84771ef9e18c062cf5d7905c0f114a84d89bec9
authored
2011-12-22 12:52:46 +0100
by
Nicolas Perriault
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
better implementation of Casper.download()
1 parent
1932b913
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
59 additions
and
44 deletions
lib/casper.js
lib/clientutils.js
tests/suites/casper/clientutils.js
tests/suites/casper/encode.js
lib/casper.js
View file @
b84771e
...
...
@@ -293,10 +293,20 @@
return
this
.
exit
(
Number
(
status
)
>
0
?
Number
(
status
)
:
1
);
},
/**
* Downloads a resource and saves it on the filesystem.
*
* @param String url The url of the resource to download
* @param String targetPath The destination file path
* @return Casper
*/
download
:
function
(
url
,
targetPath
)
{
var
fs
=
require
(
'fs'
);
var
cu
=
new
phantom
.
Casper
.
ClientUtils
();
fs
.
write
(
targetPath
,
cu
.
decode
(
this
.
base64encode
(
url
)),
'w'
);
try
{
require
(
'fs'
).
write
(
targetPath
,
cu
.
decode
(
this
.
base64encode
(
url
)),
'w'
);
}
catch
(
e
)
{
this
.
log
(
"Error while downloading "
+
url
+
" to "
+
targetPath
+
": "
+
e
,
"error"
);
}
return
this
;
},
...
...
lib/clientutils.js
View file @
b84771e
...
...
@@ -74,44 +74,44 @@
* @return string
*/
this
.
decode
=
function
(
str
)
{
//
var c1, c2, c3, c4, i = 0, len = str.length, out = "";
//
while (i < len) {
//
do {
//
c1 = BASE64_DECODE_CHARS[str.charCodeAt(i++) & 0xff];
//
} while (i < len && c1 == -1);
//
if (c1 == -1) {
//
break;
//
}
//
do {
//
c2 = BASE64_DECODE_CHARS[str.charCodeAt(i++) & 0xff];
//
} while (i < len && c2 == -1);
//
if (c2 == -1) {
//
break;
//
}
//
out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));
//
do {
//
c3 = str.charCodeAt(i++) & 0xff;
//
if (c3 == 61)
//
return out;
//
c3 = BASE64_DECODE_CHARS[c3];
//
} while (i < len && c3 == -1);
//
if (c3 == -1) {
//
break;
//
}
//
out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));
//
do {
//
c4 = str.charCodeAt(i++) & 0xff;
//
if (c4 == 61) {
//
return out;
//
}
//
c4 = BASE64_DECODE_CHARS[c4];
//
} while (i < len && c4 == -1);
//
if (c4 == -1) {
//
break;
//
}
//
out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
//
}
//
return out;
var
c1
,
c2
,
c3
,
c4
,
i
=
0
,
len
=
str
.
length
,
out
=
""
;
while
(
i
<
len
)
{
do
{
c1
=
BASE64_DECODE_CHARS
[
str
.
charCodeAt
(
i
++
)
&
0xff
];
}
while
(
i
<
len
&&
c1
==
-
1
);
if
(
c1
==
-
1
)
{
break
;
}
do
{
c2
=
BASE64_DECODE_CHARS
[
str
.
charCodeAt
(
i
++
)
&
0xff
];
}
while
(
i
<
len
&&
c2
==
-
1
);
if
(
c2
==
-
1
)
{
break
;
}
out
+=
String
.
fromCharCode
((
c1
<<
2
)
|
((
c2
&
0x30
)
>>
4
));
do
{
c3
=
str
.
charCodeAt
(
i
++
)
&
0xff
;
if
(
c3
==
61
)
return
out
;
c3
=
BASE64_DECODE_CHARS
[
c3
];
}
while
(
i
<
len
&&
c3
==
-
1
);
if
(
c3
==
-
1
)
{
break
;
}
out
+=
String
.
fromCharCode
(((
c2
&
0
XF
)
<<
4
)
|
((
c3
&
0x3C
)
>>
2
));
do
{
c4
=
str
.
charCodeAt
(
i
++
)
&
0xff
;
if
(
c4
==
61
)
{
return
out
;
}
c4
=
BASE64_DECODE_CHARS
[
c4
];
}
while
(
i
<
len
&&
c4
==
-
1
);
if
(
c4
==
-
1
)
{
break
;
}
out
+=
String
.
fromCharCode
(((
c3
&
0x03
)
<<
6
)
|
c4
);
}
return
out
;
};
/**
...
...
tests/suites/casper/clientutils.js
View file @
b84771e
...
...
@@ -11,7 +11,7 @@
"Les nuages filent et le ciel s'éclaircit\n"
+
"Et dans ma tête qui bourdonnent?\n"
+
"Les abeilles!"
),
'a file contents'
:
fs
.
read
(
phantom
.
casperPath
+
'/tests/site/
images/phantom.png
'
)
'a file contents'
:
fs
.
read
(
phantom
.
casperPath
+
'/tests/site/
alert.html
'
)
};
for
(
var
what
in
testCases
)
{
...
...
tests/suites/casper/encode.js
View file @
b84771e
(
function
(
t
)
{
t
.
comment
(
'Casper.base64encode()'
);
casper
.
start
(
'tests/site/index.html'
,
function
(
self
)
{
var
imageUrl
=
'file://'
+
phantom
.
casperPath
+
'/tests/site/images/phantom.png'
;
var
image
=
self
.
base64encode
(
imageUrl
);
var
fs
=
require
(
'fs'
);
t
.
comment
(
'Casper.base64encode()'
);
t
.
assertEquals
(
image
.
length
,
6160
,
'Casper.base64encode() can retrieve base64 contents'
);
t
.
comment
(
'Casper.download()'
);
self
.
download
(
imageUrl
,
'logo.png'
);
var
fs
=
require
(
'fs'
);
t
.
assert
(
fs
.
exists
(
'logo.png'
),
'Casper.download() downloads a file'
);
if
(
fs
.
exists
(
'logo.png'
))
{
fs
.
remove
(
'logo.png'
);
}
});
casper
.
run
(
function
(
self
)
{
...
...
Please
register
or
sign in
to post a comment