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