-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathipfs-paste
executable file
·110 lines (94 loc) · 2.06 KB
/
ipfs-paste
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
#!/bin/sh
USAGE="$0 [-v] [--paste] [<name>]"
usage() {
echo "$USAGE"
echo "publish stdin (or pasteboard) to ipfs"
exit 0
}
die() {
echo >&2 "error: $@"
exit 1
}
log() {
if [ $verbose ]; then
printf >&2 "$@"
fi
}
# todo: make this cross-platform
case $(uname) in
Darwin)
clicopy="pbcopy"
clipaste="pbpaste"
;;
Linux)
if type xsel >/dev/null;
then
clicopy="xsel -i"
clipaste="xsel -o"
else
if type xclip >/dev/null;
then
clicopy="xclip -i"
clipaste="xclip -o"
else
die "xsel or xclip is needed on Linux"
fi
fi
;;
*) ;;
esac
# get user options
while [ $# -gt 0 ]; do
# get options
arg="$1"
shift
case "$arg" in
-h|--help) usage ;;
-v|--verbose) verbose=1 ;;
--paste) paste=1 ;;
--*)
die "unrecognised option: '$arg'\n$USAGE" ;;
*)
if [ "$name" = "" ]; then
name="$arg"
else
die "too many arguments\n$USAGE"
fi
;;
esac
done
if [ "$name" = "" ]; then
name=paste
fi
if [ $paste ]; then
test -n "$clipaste" || die "clipboard paste unkown for $(uname)"
log "paste to ipfs..."
date=$(date +"%Y-%m-%dT%H:%M:%SZ")
fpath=$(mktemp "/tmp/paste.$date.XXXXXX.txt") ||
die "could not 'mktemp /tmp/paste.$date.XXXXXX.txt'"
$clipaste >"$fpath" ||
die "could not paste using '$clipaste'"
fhash=$(ipfs add -q <"$fpath") ||
die "could not 'ipfs add -q' content from '$fpath'"
else
log "stdin to ipfs..."
fhash=$(cat | ipfs add -q) ||
die "could not 'ipfs add -q'"
fi
log " $fhash\n"
log "constructing dir..."
dir=$(ipfs object new unixfs-dir) ||
die "could not 'ipfs object new unixfs-dir'"
dir=$(ipfs object patch "$dir" add-link "$name" "$fhash") ||
die "could not 'ipfs object patch $dir add-link $name $fhash'"
pin=$(ipfs pin add -r "$dir") ||
die "could not 'ipfs pin add -r $dir'"
log " $dir\n"
log "copying url to clipboard..."
out="https://ipfs.io/ipfs/$dir/$name"
echo "$out" | $clicopy ||
die "could not copy using '$clicopy'"
log " copied\n"
echo "$out"
log "preloading on the gateways..."
(curl "$out" >/dev/null 2>&1 && log " ok\n") || log " n/a\n"