2021-09-15 21:50:21 -07:00
|
|
|
#!/usr/bin/env sh
|
2024-07-23 00:04:57 -07:00
|
|
|
# compat: +ash +bash +dash +hush -ksh +mksh +oksh +osh +posh +yash +zsh
|
2021-09-15 21:50:21 -07:00
|
|
|
|
|
|
|
# **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
|
2021-09-28 13:01:04 -07:00
|
|
|
### oxo: successfully uploaded 1/1 file
|
2023-06-15 18:30:43 -07:00
|
|
|
###
|
2021-09-15 21:50:21 -07:00
|
|
|
### $ oxo ~/play/{hey,you,fake,empty}
|
|
|
|
### https://0x0.st/-3rz.txt
|
2021-09-15 23:02:41 -07:00
|
|
|
### oxo: expires in 365 days: /home/notwa/play/hey
|
2021-09-15 21:50:21 -07:00
|
|
|
### https://0x0.st/-3ri.txt
|
2021-09-15 23:02:41 -07:00
|
|
|
### oxo: expires in 365 days: /home/notwa/play/you
|
2021-09-15 21:50:21 -07:00
|
|
|
### oxo: no such file: /home/notwa/play/fake
|
|
|
|
### oxo: skipping empty file: /home/notwa/play/empty
|
2021-09-28 13:01:04 -07:00
|
|
|
### oxo: successfully uploaded 2/4 files
|
|
|
|
### oxo: failed to upload 2/4 files
|
2021-09-15 21:50:21 -07:00
|
|
|
### ```
|
|
|
|
|
|
|
|
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'
|
|
|
|
|
2021-09-16 22:48:20 -07:00
|
|
|
local hits=0 total=0
|
|
|
|
local f= s=
|
|
|
|
|
2021-09-15 21:50:21 -07:00
|
|
|
if [ $# -eq 0 ]; then
|
2024-03-21 05:14:28 -07:00
|
|
|
curl -f -F'file=@-' https://0x0.st && : $((hits+=1)) || true
|
2021-09-16 22:48:20 -07:00
|
|
|
total=1
|
2021-09-15 21:50:21 -07:00
|
|
|
elif [ $# -gt 255 ]; then
|
2021-09-15 22:42:58 -07:00
|
|
|
printf "$err" "oxo: too many arguments (max: 255)" >&2
|
2021-09-15 21:50:21 -07:00
|
|
|
return 255
|
2021-09-16 22:48:20 -07:00
|
|
|
else
|
|
|
|
total=$#
|
2021-09-16 13:35:44 -07:00
|
|
|
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
|
2021-09-16 22:40:13 -07:00
|
|
|
local g="$f"
|
|
|
|
|
|
|
|
# restrict the filepath to simple ascii characters that curl likes.
|
2021-10-07 10:57:31 -07:00
|
|
|
if [ "$f" != "${f#*[! -~]}" ] || [ "$f" != "${f#*[\":<>?\\|]}" ]; then
|
2021-09-16 22:40:13 -07:00
|
|
|
# copy the user's file to a temporary path and use that instead.
|
2023-06-15 18:30:43 -07:00
|
|
|
g=/tmp/oxo.tmp
|
2021-09-16 22:40:13 -07:00
|
|
|
#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
|
|
|
|
|
2021-10-07 11:03:49 -07:00
|
|
|
local kib="$(du -k -- "$f")"
|
|
|
|
kib="${kib%%[!0-9]*}" # strip the path, keep the size
|
2021-09-16 13:35:44 -07:00
|
|
|
|
|
|
|
if [ "$kib" -gt 524288 ]; then
|
|
|
|
printf "$bad" "oxo: file too large: $f" >&2
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
# compute retention using (fixed point) integers only.
|
2021-09-16 14:28:23 -07:00
|
|
|
# retention = 30 - (365 - 30) * ([file size] / [512 MiB] - 1)**3
|
2021-09-16 13:35:44 -07:00
|
|
|
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 ))
|
|
|
|
|
2024-03-21 05:14:28 -07:00
|
|
|
if ! curl -f -F"file=@\"$g\"" https://0x0.st; then
|
2021-09-16 13:35:44 -07:00
|
|
|
printf "$bad" "oxo: failed to upload file: $f" >&2
|
2021-09-15 21:50:21 -07:00
|
|
|
else
|
2021-09-16 22:48:20 -07:00
|
|
|
: $((hits+=1))
|
2021-09-16 13:35:44 -07:00
|
|
|
printf "$look" "oxo: expires in $r days: $f" >&2
|
2021-09-15 21:50:21 -07:00
|
|
|
fi
|
2021-09-16 13:35:44 -07:00
|
|
|
fi
|
|
|
|
done
|
2021-09-15 21:50:21 -07:00
|
|
|
|
2021-09-16 22:48:20 -07:00
|
|
|
if [ $hits -gt 0 ]; then
|
|
|
|
[ $hits -eq 1 ] && s= || s=s
|
2021-09-24 19:59:09 -07:00
|
|
|
printf "$look" "oxo: successfully uploaded $hits/$total file$s" >&2
|
2021-09-15 21:50:21 -07:00
|
|
|
fi
|
|
|
|
|
2021-09-16 22:52:30 -07:00
|
|
|
local fails=$((total-hits))
|
2021-09-15 21:50:21 -07:00
|
|
|
if [ $fails -gt 0 ]; then
|
|
|
|
[ $fails -eq 1 ] && s= || s=s
|
2021-09-24 19:59:09 -07:00
|
|
|
printf "$err" "oxo: failed to upload $fails/$total file$s" >&2
|
2021-09-15 21:50:21 -07:00
|
|
|
fi
|
|
|
|
|
2021-09-16 22:40:13 -07:00
|
|
|
[ ! -e /tmp/oxo ] || rm /tmp/oxo
|
|
|
|
|
2021-09-15 21:50:21 -07:00
|
|
|
return $fails
|
|
|
|
}
|
|
|
|
|
|
|
|
[ -n "${preload+-}" ] || oxo "$@"
|