-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilebin-cli.sh
More file actions
334 lines (272 loc) · 13.2 KB
/
Copy pathfilebin-cli.sh
File metadata and controls
334 lines (272 loc) · 13.2 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#!/usr/bin/env sh
### /// filebin-cli.sh // ConzZah // 2026-06-15 23:53 ///
## dependency checks
deps="sha256sum mktemp paste stty curl grep sed cut rev tr"
missing_deps=""
for dep in $deps; do
! command -v "$dep" >/dev/null && \
missing_deps="$dep $missing_deps"
done
[ -n "$missing_deps" ] && \
printf '\n%s\n%s\n' "--> MISSING DEPENDENCIES:" "$missing_deps" && exit 1
init () {
## init variables
t="$(printf %"$(stty size| cut -d ' ' -f 2)"s| tr ' ' '~')" # <-- prints spaces (takes number from stty size) and uses tr to replace those with tildes
fb="https://filebin.net"; bin_id=""; exitcode=""
local_file=""; local_filename=""; remote_filename=""
bin_sha256sums=""; bin_total_size_readable=""; bin_filecount=""
bin_filenames=""; bin_creationdate_rel=""; bin_expirydate_rel=""
}
get_bin_info () {
## arguably the most important function.
## its job is organizing and reformatting json output so we can parse it easier with tools like grep, sed, cut, etc..
## (i refuse to use jq)
cleanup
## create tmpdir
## if we have access to /run/user/1000, use this as tmpdir, since it's basically RAM
## meaning: no disk reads/writes for our operations
## caveat: is not avilable on every system (example: termux)
[ -d "/run/user/1000" ] && {
tmp="/run/user/1000/filebin-cli-wd"
mkdir -p "$tmp"
}
## should /run/user/1000 not be available, fall back to mktemp
[ ! -d "/run/user/1000" ] && tmp="$(mktemp -d)"
## fetch the core json file for the bin
curl -fsSL -X "GET" "$fb/$bin_id" -H "accept: application/json" -o "${tmp}/${bin_id}.json"
check4error "$?"
## organize relevant data into variables using grep, sed, cut & tr
## globals:
creationdates_rel="$(grep -s "created_at_relative" "${tmp}/${bin_id}.json"| cut -d '"' -f 4)"
updated_at_rel="$(grep -s "updated_at_relative" "${tmp}/${bin_id}.json"| cut -d '"' -f 4)"
filesizes_readable="$(grep -s "bytes_readable" "${tmp}/${bin_id}.json"| cut -d '"' -f 4)"
filesizes_bytes="$(grep "bytes" "${tmp}/${bin_id}.json"|sed -e "/bytes_readable/d" -e "s#,##g"|tr -s " "|cut -b 11-)"
## bin metadata:
bin_id="$(grep -s "id" "${tmp}/${bin_id}.json"| cut -d '"' -f 4)"
bin_expirydate_rel="$(grep -s "expired_at_relative" "${tmp}/${bin_id}.json"| cut -d '"' -f 4)"
bin_readonly="$(grep -s "readonly" "${tmp}/${bin_id}.json"| tr -s " "| cut -b 14-| sed 's#,##g')"
bin_filecount="$(grep -s -m1 "files" "${tmp}/${bin_id}.json"| tr -s " "| cut -b 11-| sed 's#,##g')"
bin_total_size_readable="$(printf '%s\n' "$filesizes_readable"| sed -n '1p')"
bin_creationdate_rel="$(printf '%s\n' "$creationdates_rel"| sed -n '1p')"
bin_updatedate_rel="$(printf '%s\n' "$updated_at_rel"| sed -n '1p')"
## create tmpfile vars
## (we need those for paste)
bin_sha256sums="${tmp}/bin_sha256sums"
bin_filenames="${tmp}/bin_filenames"
bin_filesize_readable="${tmp}/bin_filesize_readable"
bin_filesize_bytes="${tmp}/bin_filesize_bytes"
bin_file_creationdate_rel="${tmp}/bin_file_creationdate_rel"
bin_file_updatedate_rel="${tmp}/bin_file_updatedate_rel"
bin_file_content_type="${tmp}/bin_file_content_type"
## file metadata:
## (which we save to the vars (tmpfiles) we created in the previous step)
grep -s "sha256" "${tmp}/${bin_id}.json"| cut -d '"' -f 4 > "$bin_sha256sums"
grep -s "filename" "${tmp}/${bin_id}.json"| cut -d '"' -f 4 > "$bin_filenames"
printf '%s\n' "$filesizes_readable"| sed '1d' > "$bin_filesize_readable"
printf '%s\n' "$filesizes_bytes"| sed '1d' > "$bin_filesize_bytes"
printf '%s\n' "$creationdates_rel"| sed '1d' > "$bin_file_creationdate_rel"
printf '%s\n' "$updated_at_rel"| sed '1d' > "$bin_file_updatedate_rel"
grep -s "content-type" "${tmp}/${bin_id}.json"| cut -d '"' -f 4 > "$bin_file_content_type"
## 🛸️ compile data 🛸️
## ( so ":" becomes our universal delimiter. )
fdex="${tmp}/$bin_id-index.info"
bdex="${tmp}/$bin_id-bin.info"
# on $fdex, we use paste to format file-specific values
# $fdex = :bin_sha256sums:bin_filenames:bin_filesize_readable:bin_filesize_bytes:bin_file_creationdate_rel:bin_file_updatedate_rel:bin_file_content_type:
# fields: 2 3 4 5 6 7 8
paste -d ':' "$bin_sha256sums" "$bin_filenames" "$bin_filesize_readable" "$bin_filesize_bytes" "$bin_file_creationdate_rel" "$bin_file_updatedate_rel" "$bin_file_content_type" > "$fdex"
## we add our delimiter to every beginning and end of line with sed
sed -i -e 's/^/:/g' -e 's/$/:/g' "$fdex"
# on $bdex, we can just use printf, because it's a single line.
# $bdex = :bin_id:bin_total_size_readable:bin_filecount:bin_readonly:bin_expirydate_rel:bin_creationdate_rel:bin_updatedate_rel:
# fields: 2 3 4 5 6 7 8
printf '%s\n' ":$bin_id:$bin_total_size_readable:$bin_filecount:$bin_readonly:$bin_expirydate_rel:$bin_creationdate_rel:$bin_updatedate_rel:" > "$bdex"
}
upload_to_bin () {
get_bin_info
x="upload"
## before we go any further, verify that the file we are trying to upload exists.
## if user enters nothing, print error and exit
[ -z "$local_file" ] && printf '%s\n' "ERROR: SPECIFY THE PATH TO A FILE AND TRY AGAIN." && quit
## if the file doesn't exist we print error and exit.
[ ! -f "$local_file" ] && printf '%s\n' "ERROR: '$local_file' DOESN'T EXIST OR IS A DIRECTORY." && quit
## get the basename of the file we want to upload
## (this emulates the basename command)
local_filename="$(printf '%s' "$local_file"| rev| cut -d '/' -f 1| rev)"
urlencode_filename
## start the upload
printf '\n%s\n' "UPLOADING: $local_filename TO '$fb/$bin_id"
curl -fsSL -X "POST" "$fb/$bin_id/$urlencoded_filename" -H "accept: application/json" -H "Content-Type: application/octet-stream" --data-binary @"$local_file"; check4error "$?"
verify_sha256sum
}
download_from_bin () {
get_bin_info
x="download"
archive=""
# if "$remote_filename is empty, assume the user wants to download the whole bin in zip format:
[ -z "$remote_filename" ] && remote_filename="zip"
# if the user specified nothing (or zip / tar) after dl, prepare to download the whole bin in chosen format:
[ "$remote_filename" = "zip" ] || [ "$remote_filename" = "tar" ] && {
archive="1"
fb="$fb/archive"
dlmsg="DOWNLOADING: ${bin_id}.${remote_filename}"
printf "tar\nzip" > "$fdex"
}
verify_remote_filename_existence
[ -z "$archive" ] && dlmsg="DOWNLOADING: $remote_filename"
urlencode_filename
printf '%s\n' "$dlmsg"
curl -fsSL -o "$remote_filename" "${fb}/${bin_id}/${urlencoded_filename}"
check4error "$?"
# if user downloaded the bin in archive format, we can't compare checksums, so we exit early here.
case "$fb" in
*"/archive") mv "$remote_filename" "${bin_id}.${remote_filename}"; quit ;;
esac
## otherwise verify checksums
verify_sha256sum
}
verify_remote_filename_existence () {
# if "$remote_filename" is set to anything, try to grep the definitive result:
# (this allows you to only type out part of a filename and have it still be matched, if it should exist.)
# (we basically achieved semi-autocompletion for $remote_filename with this one)
[ -n "$remote_filename" ] && remote_filename="$(grep -m1 "$remote_filename" "$fdex"| cut -d ":" -f 3)"
# if "$remote_filename" is empty, print error and quit
[ -z "$remote_filename" ] && printf '%s\n' "ERROR: FILENAMES CAN'T BE EMPTY" && quit
# else we check if $remote_filename actually exists in $fdex.
[ "$remote_filename" != "$(grep -o -m1 "$remote_filename" "$fdex")" ] && \
printf '\n%s\n' "ERROR: THE REQUESTED FILE DOESN'T EXIST @ $fb/$bin_id" && quit
}
urlencode_filename () {
# mainly there so curl doesn't break on filenames that contain " ", "(", ")" etc..
[ "$x" = "upload" ] && urlencoded_filename="$local_filename"
[ "$x" = "download" ] || [ "$x" = "delete" ] || [ "$x" = "ls" ] && \
urlencoded_filename="$(printf '%s\n' "$remote_filename"| sed -e 's# #%20#g' -e 's#(#%28#g' -e 's#)#%29#g')"
}
verify_sha256sum () {
### tries to verify that uploads or downloads aren't fragmented by comparing two sha256sums
## we define _x twice because sha256sum must have the full filepath to create a checksum. ( _x = filename, _X = filepath )
[ "$x" = "upload" ] && { get_bin_info; _x="$local_filename"; _X="$local_file" ;} # <-- if we upload, $_x = local
[ "$x" = "download" ] && { _x="$remote_filename"; _X="$_x" ;} # <-- if we download, $_x = remote
printf "\n\nCOMPARING HASHES..\n"
local_sha256="$(sha256sum "$_X"|cut -d " " -f 1)"
remote_sha256="$(grep -s "$_x" "$fdex"|cut -d ":" -f 2)"
## compare em
[ "$local_sha256" = "$remote_sha256" ] && \
printf "\n ✔️ SHA256sums MATCH! ✔️ \n\n" && quit || \
printf "\n ✖️ ERROR: SHA256sums DON'T MATCH. ✖️ \n\n" && quit
}
ls_bin () {
x="ls"
get_bin_info
[ "$bin_filecount" = "0" ] && printf '%s\n' "LS FAILED: BIN IS EMPTY" && quit
# (default)
# if $remote_filename is empty run default ls:
[ -z "$remote_filename" ] && {
printf '\n%s\n' "FILES: $bin_filecount - TOTAL SIZE: $bin_total_size_readable - READ-ONLY: $bin_readonly"
printf '%s%s\n' "$t" "<FNAME> - <FSIZE> - <FTYPE>"
cut -d ":" -f 3,4,8 "$fdex"| sed "s#:# - #g"
printf '%s\n%s\n%s\n' "$t" "$fb/$bin_id" "this bin expires: $bin_expirydate_rel"
return
}
# (single file)
# if the user specified a filename after ls, check if the requested file exists and show its metadata:
[ -n "$remote_filename" ] && {
verify_remote_filename_existence
urlencode_filename
printf '\n%s\n%s\n' "$t" "NAME: $remote_filename"
printf '%s\n' "SIZE: $(grep -m1 "$remote_filename" "$fdex"| cut -d ':' -f 4)"
printf '%s\n' "SIZE (in bytes): $(grep -m1 "$remote_filename" "$fdex"| cut -d ':' -f 5)"
printf '%s\n' "CREATED: $(grep -m1 "$remote_filename" "$fdex"| cut -d ':' -f 6)"
printf '%s\n' "UPDATED: $(grep -m1 "$remote_filename" "$fdex"| cut -d ':' -f 7)"
printf '%s\n' "SHA256: $(grep -m1 "$remote_filename" "$fdex"| cut -d ':' -f 2)"
printf '%s\n%s' "URL: $fb/$bin_id/$urlencoded_filename" "$t"
}
}
lock_bin () {
get_bin_info
# asks if you want to put the bin in read-only mode (defaults to no)
printf "\nWARNING! \n\nDO YOU REALLY WANT TO LOCK THE BIN:?\n\nTHIS CAN'T BE UNDONE! \n"
printf '%s' "[Y/N] "
read -r yn
case $yn in
y|Y) curl -fsSL -X "PUT" "$fb/$bin_id" -H "accept: application/json" && printf '%s\n' "$fb/$bin_id IS NOW READ-ONLY." && quit ;;
*) printf '\n%s\n' "LOCK CANCELED"; quit ;;
esac
}
delete_bin () {
get_bin_info
# asks if you want to delete the entire bin (defaults to no)
printf "\nWARNING! \n\nDO YOU REALLY WANT TO DELETE THE ENTIRE BIN?\n\nTHIS CAN'T BE UNDONE! \n"
printf '%s' "[Y/N] "
read -r yn
case $yn in
y|Y) curl -fsSL -X "DELETE" "$fb/$bin_id" -H "accept: application/json"; quit ;;
*) printf '\n%s\n' "DELETE CANCELED"; quit ;;
esac
}
delete_file () {
get_bin_info; verify_remote_filename_existence; urlencode_filename
# asks if you want to delete a single file from the bin (also defaults to no)
printf '\n%s\n\n%s\ņ\n%s\n\n' "WARNING!" "DO YOU REALLY WANT TO DELETE THE FILE: '$remote_filename' ?" "THIS CAN'T BE UNDONE!"
printf '%s' "[Y/N] "
read -r yn
case $yn in
y|Y) curl -fsSL -X "DELETE" "$fb/$bin_id/$urlencoded_filename" -H "accept: application/json"; quit ;;
*) printf '\n%s\n' "DELETE CANCELED"; quit ;;
esac
}
qr_bin () {
## gets qr-code containing the bin url as .png and saves it.
curl -fsSL -X "GET" "$fb/qr/$bin_id" \
-H "accept: image/png" \
-o "QR-$bin_id.png" && \
printf '%s\n' "QR-CODE SAVED"
## if one happens to have qrencode installed, show qr-code right in the terminal:
command -v 'qrencode' >/dev/null && \
printf '\n%s\n%s\n' "$fb/$bin_id" "$t" && \
printf '%s\n' "$fb/$bin_id"| qrencode -t utf8
}
check4error () {
### basic errorchecking using exit codes and conditions
## write exit code of last command to variable
## (which we get by forwarding it as first param)
exitcode="$1"
## if errorcode is anything else than 0, print the captured exit code and exit
[ "$exitcode" != "0" ] && printf '%s\n' "EXIT CODE: $exitcode" && quit
}
cleanup () { [ -n "$tmp" ] && [ -d "$tmp" ] && rm -fr "$tmp" ;}
quit () { cleanup; exit ;}
show_help () {
printf '\n /// filebin-cli // Author: ConzZah ©️ 2026 // LICENSE: MIT ///
\nOPTIONS:\n
ls, list list files in bin\n
u, ul, upload upload files to bin\n
d, dl, download download files from bin\n
l, lock lock bin\n
del, delete delete entire bin or single file\n
qr generate a qr-code with the bin url\n
USAGE: ./filebin-cli <FILEBIN ID/URL> OPTION <PATH/TO/FILE>\n'
}
###### launch ######
# ( $1 refers to filebin id / url, $2 to option, $3 to filepath / name, and $4 + to additional options )
init
## filebin url handling
## if $1 has any value and contains the filebin url, get rid of it with sed, so only the bin_id remains, & use set to reassign positional parameters
case "$1" in
"$fb"*)
formatted_bin_id="$(printf '%s\n' "$1"| sed "s#${fb}/##g")"
set -- "$formatted_bin_id" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9";;
esac
## in any case, "$1" will be assigned to $bin_id
[ -n "$1" ] && bin_id="$1"
### OPTIONS ###
case "$2" in
ls|LS|list) remote_filename="$3"; ls_bin; quit ;;
d|D|dl|DL|download) remote_filename="$3"; download_from_bin; quit ;;
u|U|ul|UL|upload) local_file="$3"; upload_to_bin; quit ;;
l|L|lock|LOCK) lock_bin; quit ;;
del|DEL|delete|DELETE) x="delete"; [ -z "$3" ] && delete_bin && quit || remote_filename="$3" && delete_file && quit ;;
qr|QR) qr_bin; quit ;;
esac
### END OF OPTIONS ###
[ -n "$1" ] && show_help && quit || [ -z "$1" ] && show_help && quit