1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-11-05 02:49:02 -08:00

only copy files if they contain troublesome characters

This commit is contained in:
Connor Olding 2021-09-16 22:40:13 -07:00
parent 43813a2cd8
commit 545ab4bb5d

24
sh/oxo
View file

@ -62,7 +62,21 @@ oxo() { ### @-
: $((fails+=1)) : $((fails+=1))
printf "$bad" "oxo: skipping empty file: $f" >&2 printf "$bad" "oxo: skipping empty file: $f" >&2
else else
local kib="$(du -k "$f" | cut -f1)" local g="$f"
# restrict the filepath to simple ascii characters that curl likes.
if [ -n "$(printf '%s' "$f" | tr -d \''0-9A-Za-z !#$%&()*+,./;=@[]^_`{}~-')" ]; then
# copy the user's file to a temporary path and use that instead.
g=/tmp/oxo
#printf "$warn" "oxo: special characters found: $f" >&2
if ! cp -p -- "$f" "$g"; then
: $((fails+=1))
printf "$bad" "oxo: failed to copy file: $f" >&2
continue
fi
fi
local kib="$(du -k -- "$f" | cut -f1)"
if [ "$kib" -gt 524288 ]; then if [ "$kib" -gt 524288 ]; then
: $((fails+=1)) : $((fails+=1))
@ -78,17 +92,13 @@ oxo() { ### @-
local d=$(( b*c/8192+3*kib/16+1-32768 )) local d=$(( b*c/8192+3*kib/16+1-32768 ))
local r=$(( (d*-335+16384)/32768+30 )) local r=$(( (d*-335+16384)/32768+30 ))
if ! cp -p "$f" /tmp/0x0; then if ! curl -F"file=@\"$g\"" https://0x0.st; then
: $((fails+=1))
printf "$bad" "oxo: failed to copy file: $f" >&2
elif ! curl -F'file=@/tmp/0x0' https://0x0.st; then
: $((fails+=1)) : $((fails+=1))
printf "$bad" "oxo: failed to upload file: $f" >&2 printf "$bad" "oxo: failed to upload file: $f" >&2
else else
: $((succs+=1)) : $((succs+=1))
printf "$look" "oxo: expires in $r days: $f" >&2 printf "$look" "oxo: expires in $r days: $f" >&2
fi fi
#rm -f /tmp/0x0
fi fi
done done
@ -103,6 +113,8 @@ oxo() { ### @-
printf "$err" "oxo: failed to upload $fails file$s" >&2 printf "$err" "oxo: failed to upload $fails file$s" >&2
fi fi
[ ! -e /tmp/oxo ] || rm /tmp/oxo
return $fails return $fails
} }