1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-11-12 15:59:02 -08:00
rc/sh/oshi

89 lines
2.9 KiB
Bash
Executable file

#!/usr/bin/env sh
# YES_ZSH YES_BASH YES_DASH YES_ASH
__oshi() { ### @oshi
### upload files (or stdin) to [oshi.at.](https://oshi.at)
### this script exits with the number of failed uploads; up to 255 at once.
### directories are skipped. for now, file expiry is hard-coded at 2 weeks.
###
### ```
### $ echo test | oshi
### MANAGE: https://oshi.at/a/7809e5e8a8b5c28555b1e8cadc99b069d08a5d09
### DL: https://oshi.at/ReTn/Dxzy
###
### $ oshi ~/play/{hey,you,fake,empty}
### MANAGE: https://oshi.at/a/9b56e4c5843006d491fabe859ea5746a8f36760c
### DL: https://oshi.at/obFf/hey
### oshi: expires in 14 days: /home/notwa/play/hey
### MANAGE: https://oshi.at/a/f2dc46ae900ca7465a377d7a7942e722f87ff483
### DL: https://oshi.at/JLBc/you
### oshi: expires in 14 days: /home/notwa/play/you
### oshi: no such file: /home/notwa/play/fake
### oshi: skipping directory: /home/notwa/play/empty
### oshi: successfully uploaded 2/4 files
### oshi: failed to upload 2/4 files
### ```
inform() { printf '\033[1moshi: %s\033[m\n' "$*"; } >&2
mutter() { printf '\033[31moshi: %s\033[m\n' "$*"; } >&2
lament() { printf '\033[1;33moshi: %s\033[m\n' "$*"; } >&2
recede() { printf '\033[1;31moshi: %s\033[m\n' "$*"; } >&2
hits=0 total=0
if [ $# = 0 ]; then
curl -f -F f=@- -F expire=20160 -F randomizefn=1 https://oshi.at && hits=1 || true
total=1
elif [ $# -gt 255 ]; then
recede "too many arguments (max: 255)"
return 255
else
total=$#
fi
for f; do
if [ ! -e "$f" ]; then
mutter "no such file: $f"
continue
elif [ -d "$f" ]; then
mutter "skipping directory: $f"
continue
elif [ ! -s "$f" ]; then
mutter "skipping empty file: $f"
continue
fi
# i have no idea why, but mingw64 curl on msys2 is replacing each unicode codepoint with a question mark.
# the irony is that it seems to be properly decoding the UTF-8 encoding and then replacing it all anyway.
[ -d /C ] && g="$(LC_ALL= cygpath -ws "$f")" || g="$f"
bytes="$(du -b -- "$f")" || bytes=0
if [ "$bytes" -gt 5000000000 ] 2>&-; then
mutter "file too large: $f"
continue
fi
if curl -fg -F f=@"$g" -F expire=20160 -F filename="${f##*/}" https://oshi.at; then
inform "expires in 14 days: $f"
: $((hits+=1))
else
mutter "failed to upload file: $f"
fi
done
if [ $hits -gt 0 ]; then
[ $hits = 1 ] && s= || s=s
inform "successfully uploaded $hits/$total file$s"
fi
fails=$((total-hits))
if [ $fails -gt 0 ]; then
[ $fails = 1 ] && s= || s=s
recede "failed to upload $fails/$total file$s"
fi
return $fails
}
oshi()(__oshi "$@")
[ -n "${preload+-}" ] || __oshi "$@"