mirror of
https://github.com/notwa/rc
synced 2024-11-05 07:29:04 -08:00
117 lines
3.8 KiB
Bash
Executable file
117 lines
3.8 KiB
Bash
Executable file
#!/usr/bin/env sh
|
|
# compat: +ash +bash +dash +zsh
|
|
|
|
# **TL;DR RULES:**
|
|
# * no executables (for the most part)
|
|
# * no personal info (yours or otherwise)
|
|
# * no right-wing nonsense
|
|
# * no piracy or NSFW media
|
|
# * no command & control
|
|
# * no continuous integration
|
|
# * no backups, and no minecraft (for real)
|
|
# * nothing that's illegal in Germany
|
|
|
|
oxo() { ### @-
|
|
### upload files (or stdin) to [0x0.st.](https://0x0.st)
|
|
### this script exits with the number of failed uploads; up to 255 at once.
|
|
### file retention period (30 to 365 days) is only computed for arguments.
|
|
### directories are skipped. please review the terms of service
|
|
### [on the website](https://0x0.st) before uploading files.
|
|
###
|
|
### ```
|
|
### $ echo test | oxo
|
|
### https://0x0.st/sj2.txt
|
|
### oxo: successfully uploaded 1/1 file
|
|
###
|
|
### $ oxo ~/play/{hey,you,fake,empty}
|
|
### https://0x0.st/-3rz.txt
|
|
### oxo: expires in 365 days: /home/notwa/play/hey
|
|
### https://0x0.st/-3ri.txt
|
|
### oxo: expires in 365 days: /home/notwa/play/you
|
|
### oxo: no such file: /home/notwa/play/fake
|
|
### oxo: skipping empty file: /home/notwa/play/empty
|
|
### oxo: successfully uploaded 2/4 files
|
|
### oxo: failed to upload 2/4 files
|
|
### ```
|
|
|
|
local look='\033[1m%s\033[0m\n'
|
|
local bad='\033[31m%s\033[0m\n'
|
|
local warn='\033[1;33m%s\033[0m\n'
|
|
local err='\033[1;31m%s\033[0m\n'
|
|
|
|
local hits=0 total=0
|
|
local f= s=
|
|
|
|
if [ $# -eq 0 ]; then
|
|
curl -f -F'file=@-' https://0x0.st && : $((hits+=1)) || true
|
|
total=1
|
|
elif [ $# -gt 255 ]; then
|
|
printf "$err" "oxo: too many arguments (max: 255)" >&2
|
|
return 255
|
|
else
|
|
total=$#
|
|
fi
|
|
|
|
for f; do
|
|
if [ ! -e "$f" ]; then
|
|
printf "$bad" "oxo: no such file: $f" >&2
|
|
elif [ -d "$f" ]; then
|
|
printf "$bad" "oxo: skipping directory: $f" >&2
|
|
elif [ ! -s "$f" ]; then
|
|
printf "$bad" "oxo: skipping empty file: $f" >&2
|
|
else
|
|
local g="$f"
|
|
|
|
# restrict the filepath to simple ascii characters that curl likes.
|
|
if [ "$f" != "${f#*[! -~]}" ] || [ "$f" != "${f#*[\":<>?\\|]}" ]; then
|
|
# copy the user's file to a temporary path and use that instead.
|
|
g=/tmp/oxo.tmp
|
|
#printf "$warn" "oxo: special characters found: $f" >&2
|
|
if ! cp -p -- "$f" "$g"; then
|
|
printf "$bad" "oxo: failed to copy file: $f" >&2
|
|
continue
|
|
fi
|
|
fi
|
|
|
|
local kib="$(du -k -- "$f")"
|
|
kib="${kib%%[!0-9]*}" # strip the path, keep the size
|
|
|
|
if [ "$kib" -gt 524288 ]; then
|
|
printf "$bad" "oxo: file too large: $f" >&2
|
|
continue
|
|
fi
|
|
|
|
# compute retention using (fixed point) integers only.
|
|
# retention = 30 - (365 - 30) * ([file size] / [512 MiB] - 1)**3
|
|
local a=$(( kib/16+1 ))
|
|
local b=$(( (kib+64)/128-12288 ))
|
|
local c=$(( a*a/16384 ))
|
|
local d=$(( b*c/8192+3*kib/16+1-32768 ))
|
|
local r=$(( (d*-335+16384)/32768+30 ))
|
|
|
|
if ! curl -f -F"file=@\"$g\"" https://0x0.st; then
|
|
printf "$bad" "oxo: failed to upload file: $f" >&2
|
|
else
|
|
: $((hits+=1))
|
|
printf "$look" "oxo: expires in $r days: $f" >&2
|
|
fi
|
|
fi
|
|
done
|
|
|
|
if [ $hits -gt 0 ]; then
|
|
[ $hits -eq 1 ] && s= || s=s
|
|
printf "$look" "oxo: successfully uploaded $hits/$total file$s" >&2
|
|
fi
|
|
|
|
local fails=$((total-hits))
|
|
if [ $fails -gt 0 ]; then
|
|
[ $fails -eq 1 ] && s= || s=s
|
|
printf "$err" "oxo: failed to upload $fails/$total file$s" >&2
|
|
fi
|
|
|
|
[ ! -e /tmp/oxo ] || rm /tmp/oxo
|
|
|
|
return $fails
|
|
}
|
|
|
|
[ -n "${preload+-}" ] || oxo "$@"
|