@@ -0,0 +1,87 @@
1+source $stdenv/setup
2+
3+# Curl flags to handle redirects, not use EPSV, handle cookies for
4+# servers to need them during redirects, and work on SSL without a
5+# certificate (this isn't a security problem because we check the
6+# cryptographic hash of the output anyway).
7+
8+set -o noglob
9+
10+curl="curl \
11+ --location \
12+ --max-redirs 20 \
13+ --retry 2 \
14+ --disable-epsv \
15+ --cookie-jar cookies \
16+ --insecure \
17+ --speed-time 5 \
18+ -# \
19+ --fail \
20+ $curlOpts \
21+ $NIX_CURL_FLAGS"
22+
23+finish() {
24+ runHook postFetch
25+ set +o noglob
26+ exit 0
27+}
28+
29+ipfs_add() {
30+ if curl --retry 0 --head --silent "localhost:5001" > /dev/null; then
31+ echo "[0m[01;36m=IPFS=[0m add $ipfs"
32+ tar --owner=root --group=root -cWf "source.tar" $(echo *)
33+ res=$(curl -# -F "file=@source.tar" "localhost:5001/api/v0/tar/add" | sed 's/.*"Hash":"\(.*\)".*/\1/')
34+ if [ $ipfs != $res ]; then
35+ echo "\`ipfs tar add' results in $res when $ipfs is expected"
36+ exit 1
37+ fi
38+ rm "source.tar"
39+ fi
40+}
41+
42+echo
43+
44+mkdir download
45+cd download
46+
47+if curl --retry 0 --head --silent "localhost:5001" > /dev/null; then
48+ curlexit=18;
49+ echo "[0m[01;36m=IPFS=[0m get $ipfs"
50+ # if we get error code 18, resume partial download
51+ while [ $curlexit -eq 18 ]; do
52+ # keep this inside an if statement, since on failure it doesn't abort the script
53+ if $curl -C - "http://localhost:5001/api/v0/tar/cat?arg=$ipfs" --output "$ipfs.tar"; then
54+ unpackFile "$ipfs.tar"
55+ rm "$ipfs.tar"
56+ set +o noglob
57+ mv $(echo *) "$out"
58+ finish
59+ else
60+ curlexit=$?;
61+ fi
62+ done
63+fi
64+
65+if test -n "$url"; then
66+ curlexit=18;
67+ echo "Downloading $url"
68+ while [ $curlexit -eq 18 ]; do
69+ # keep this inside an if statement, since on failure it doesn't abort the script
70+ if $curl "$url" -O; then
71+ set +o noglob
72+ tmpfile=$(echo *)
73+ unpackFile $tmpfile
74+ rm $tmpfile
75+ ipfs_add
76+ mv $(echo *) "$out"
77+ finish
78+ else
79+ curlexit=$?;
80+ fi
81+ done
82+fi
83+
84+echo "[01;31merror:[0m cannot download $ipfs from ipfs or the given url"
85+echo
86+set +o noglob
87+exit 1