1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-11-14 08:29:03 -08:00
rc/sh/oxo

118 lines
3.8 KiB
Text
Raw Normal View History

2021-09-15 21:50:21 -07:00
#!/usr/bin/env sh
# 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'
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
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
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
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.
2023-06-15 18:30:43 -07:00
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
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.
# 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
: $((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
if [ $hits -gt 0 ]; then
[ $hits -eq 1 ] && s= || s=s
printf "$look" "oxo: successfully uploaded $hits/$total file$s" >&2
2021-09-15 21:50:21 -07:00
fi
local fails=$((total-hits))
2021-09-15 21:50:21 -07:00
if [ $fails -gt 0 ]; then
[ $fails -eq 1 ] && s= || s=s
printf "$err" "oxo: failed to upload $fails/$total file$s" >&2
2021-09-15 21:50:21 -07:00
fi
[ ! -e /tmp/oxo ] || rm /tmp/oxo
2021-09-15 21:50:21 -07:00
return $fails
}
[ -n "${preload+-}" ] || oxo "$@"