-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgit-annex-remote-ipfs
executable file
·148 lines (139 loc) · 3.28 KB
/
git-annex-remote-ipfs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/bin/sh
# This is a git-annex external special remote program,
# which adds experimental ipfs support to git-annex.
#
# Install in PATH as git-annex-remote-ipfs
#
# Copyright 2015 Joey Hess; licenced under the GNU GPL version 3 or higher.
set -e
# use ipfs: as a prefix to indicate when an "url" is really stored in ipfs
isipfsurl () {
echo "${1}" | egrep -q "^ipfs:"
# done
return
}
# convert an ipfs: url to an address that the ipfs client understands
urltoaddress () {
echo "${1}" | sed -e 's/^ipfs://'
# done
return
}
# convert address to to ipfs: url
addresstourl () {
echo "ipfs:${1}"
# done
return
}
# Gets a VALUE response and stores it in $RET
getvalue () {
read resp
# Tricky POSIX shell code to split first word of the resp,
# preserving all other whitespace
case "${resp%% *}" in
VALUE)
#OLD CODE:
#RET="$(echo "$resp" | sed 's/^VALUE \?//')"
#NEW CODE:
if [[ ${resp} = "VALUE" ]]; then
RET=""
else
RET="$(echo "${resp}" | sed 's/^VALUE \?//')"
echo "RESP: ${resp}, RESP SED: ${RET}" >&2
fi
;;
*)
RET=""
;;
esac
# done
return
}
# Get a list of all known ipfs addresses for a key,
# storing it in a temp file.
getaddrs () {
key="${1}"
tmp="${2}"
echo GETURLS "${key}"
getvalue
while [ -n "${RET}" ] ; do
echo "================================="
echo BUG: RET IS $RET AND ISIPFSURL RETURNS `isipfsurl $RET` >&2
echo "================================="
PARSED_RET=`echo $RET | sed 's/VALUE //'`;
if isipfsurl $PARSED_RET; then
echo "$PARSED_RET" >> "$tmp"
getvalue
fi
done
}
# This has to come first, to get the protocol started.
echo VERSION 1
while read line; do
set -- $line
case "$1" in
INITREMOTE)
echo INITREMOTE-SUCCESS
;;
PREPARE)
echo PREPARE-SUCCESS
;;
CLAIMURL)
url="$2"
if isipfsurl "$url"; then
echo CLAIMURL-SUCCESS
else
echo CLAIMURL-FAILURE
fi
;;
CHECKURL)
url="$2"
# TODO if size of file can be quickly determined
# (without downloading it) return the size
# instead of UNKNOWN
echo CHECKURL-CONTENTS UNKNOWN "$(urltoaddress "$url")"
;;
TRANSFER)
op="$2"
key="$3"
shift 3
file="$@"
case "$op" in
STORE)
addr=$(ipfs add -q "$file" </dev/null) || true
if [ -z "$addr" ]; then
echo TRANSFER-FAILURE STORE "$key" "ipfs add failed"
else
echo "SETURIPRESENT" "$key" "$(addresstourl "$addr")"
echo TRANSFER-SUCCESS STORE "$key"
fi
;;
RETRIEVE)
addrtmp=$(mktemp)
getaddrs "$key" "$addrtmp"
addr="$(urltoaddress "$(head "$addrtmp")")" || true
rm -f "$addrtmp"
if [ -z "$addr" ]; then
echo TRANSFER-FAILURE RETRIEVE "$key" "no known ipfs address for this key"
else
if ! ipfs get --output="$file" "$addr" >&2 </dev/null; then
echo TRANSFER-FAILURE RETRIEVE "$key" "failed downloading ipfs $addr"
else
echo TRANSFER-SUCCESS RETRIEVE "$key"
fi
fi
;;
esac
;;
CHECKPRESENT)
key="$2"
echo CHECKPRESENT-FAILURE "$key"
;;
REMOVE)
key="$2"
echo REMOVE-FAILURE "$key" "cannot remove content from ipfs (instead, run ipfs gc to clear your local ipfs cache)"
;;
*)
echo UNSUPPORTED-REQUEST
;;
esac
done