1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-11-05 05:49:02 -08:00
rc/sh/oxo
2021-09-15 23:02:41 -07:00

103 lines
3.4 KiB
Bash
Executable file

#!/usr/bin/env sh
# YES_ZSH
# YES_BASH
# YES_DASH
# YES_ASH
# **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 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 files
### oxo: failed to upload 2 files
### ```
local succs=0
local fails=0
local f= s=
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'
if [ $# -eq 0 ]; then
curl -F'file=@-' https://0x0.st && : $((succs+=1)) || : $((fails+=1))
elif [ $# -gt 255 ]; then
printf "$err" "oxo: too many arguments (max: 255)" >&2
return 255
else
for f; do
if [ ! -e "$f" ]; then
printf "$bad" "oxo: no such file: $f" >&2
: $((fails+=1))
elif [ -d "$f" ]; then
printf "$bad" "oxo: skipping directory: $f" >&2
: $((fails+=1))
elif [ ! -s "$f" ]; then
printf "$bad" "oxo: skipping empty file: $f" >&2
: $((fails+=1))
else
local kib="$(du -k "$f" | cut -f1)"
# compute retention using (fixed point) integers only.
# retention = (365 - 30) * ([file size] / [512 MiB] - 1)**3 + 30
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 ! cp -p "$f" /tmp/0x0; then
: $((fails+=1))
printf "$bad" "oxo: failed to copy file: $f" >&2
elif ! curl -F'file=@/tmp/0x0' https://0x0.st; then
: $((fails+=1))
printf "$bad" "oxo: failed to upload file: $f" >&2
else
: $((succs+=1))
printf "$look" "oxo: expires in $r days: $f" >&2
fi
#rm -f /tmp/0x0
fi
done
fi
if [ $succs -gt 0 ]; then
[ $fails -eq 0 ] && fmt="$look" || fmt="$warn"
[ $succs -eq 1 ] && s= || s=s
printf "$fmt" "oxo: successfully uploaded $succs file$s" >&2
fi
if [ $fails -gt 0 ]; then
[ $fails -eq 1 ] && s= || s=s
printf "$err" "oxo: failed to upload $fails file$s" >&2
fi
return $fails
}
[ -n "${preload+-}" ] || oxo "$@"