forked from blha303/puush-linux
-
Notifications
You must be signed in to change notification settings - Fork 1
/
puush
executable file
·195 lines (181 loc) · 3.19 KB
/
puush
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/bin/sh
# Version: git
if [ -z "$PUUSH_API_KEY" ]
then
PUUSH_API_KEY=""
fi
usage()
{
cat << EOF
Usage: $0 [options] [file]
If [file] is a hyphen, puush will read the filename from STDIN
Options:
-d Delete file
-f No delete prompt
-h Show this message
-l [search] List uploaded files
EOF
}
exclusive()
{
if [ $EXCLUSIVE ]
then
echo "Too many options."
exit 1
fi
EXCLUSIVE=true
}
OPERATION=UP
while getopts ":d:fhl" OPTION
do
case $OPTION in
d)
OPERATION=DEL
FILE=$OPTARG
exclusive
;;
f)
FORCE=true
;;
h)
usage
exit 1
;;
l)
OPERATION=LIST
exclusive
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
if [ -z "$PUUSH_API_KEY" ]
then
echo "Set the variable PUUSH_API_KEY in $0 or with 'export PUUSH_API_KEY=\"apiKeyHere\"'" >&2
exit 1
fi
validate()
{
if [ -z "$2" -o "$2" = -1 ]
then
echo "$3 failed" >&2
exit 1
elif [ "$1" != 0 ]
then
return
else
echo "$3 successful"
if [ $4 ]
then
echo $4
fi
fi
}
printhist()
{
if [ "$1" = 0 ]
then
return
fi
IFS=","
set $1
printf "ID: %s\tFilename: %s\tURL: %s\tDate: %s\n" $1 $4 $3 $2
ID=$1
}
printdel()
{
printf "$1 ~ "
printhist "$2"
ID[$1]=$ID
}
del()
{
RESP=$(curl "https://puush.me/api/del" -s -F "k=$PUUSH_API_KEY" -F "i=$1")
validate "0" "$RESP" "Delete"
}
case $OPERATION in
DEL)
RESP=$(curl "https://puush.me/api/hist" -s -F "k=$PUUSH_API_KEY")
validate "1" "$RESP" "Delete"
HIST=$(echo "$RESP" | grep $FILE)
if [ $(echo "$HIST" | wc -l) = 1 ]
then
ID=$(echo $HIST | cut -d , -f 1)
if [ -z "$ID" -o "$ID" = "0" ]
then
echo "File not found" >&2
exit 1
fi
NAME=$(echo $HIST | cut -d , -f 4)
if ! [ "$FILE" = "$NAME" -o "$FORCE" ]
then
echo "Delete $NAME? ($ID) [Y/n]"
read i
if ! [ "$i" = '' -o "${i^^}" = "Y" ]
then
exit 0
fi
fi
del $ID
else
while read line
do
if [ "$line" = "0" ]
then
continue
fi
i=$[ i + 1 ]
printdel $i "$line"
done <<< "$HIST"
printf "Delete number: "
read i
if [[ $i =~ ^[0-9]+$ ]]
then
del ${ID[$i]}
else
echo "Invalid number" >&2
exit 1
fi
fi
;;
LIST)
RESP=$(curl "https://puush.me/api/hist" -s -F "k=$PUUSH_API_KEY")
validate "1" "$RESP" "List"
if [ $# -gt 1 ]
then
SEARCH=${@: -1}
HIST=$(echo "$RESP" | grep $SEARCH)
else
HIST="$RESP"
fi
while read line
do
if [ "$line" = "0" ]
then
continue
fi
printhist "$line"
done <<< "$HIST"
;;
UP)
FILE=${@: -1}
if [ "$FILE" = "-" ]
then
read FILE
fi
if ! [ -r "$FILE" ]
then
echo "Invalid file specified" >&2
exit 1
fi
RESP=$(curl "https://puush.me/api/up" -# -F "k=$PUUSH_API_KEY" -F "z=poop" -F "f=@$FILE")
validate "0" "$RESP" "Upload" "$(echo "$RESP" | cut -d , -f 2)"
;;
esac
exit 0