pstsrv: rewrite, drop ix.io, add uguu.se

This commit is contained in:
Lucas 2023-06-19 15:56:43 +00:00
parent 9f004d4faf
commit 432832cd5a

View File

@ -1,6 +1,6 @@
#!/bin/sh #!/bin/sh
# pstsrv # pstsrv
# Written in 2020 by Lucas # Written in 2020-2021,2023 by Lucas
# CC0 1.0 Universal/Public domain - No rights reserved # CC0 1.0 Universal/Public domain - No rights reserved
# #
# To the extent possible under law, the author(s) have dedicated all # To the extent possible under law, the author(s) have dedicated all
@ -12,7 +12,7 @@
usage() usage()
{ {
printf "Usage: %s [-T] service [files...]\n" "${0##*/}" >&2 printf "Usage: %s [-x proxy] service file [file ...]\n" "${0##*/}" >&2
exit 1 exit 1
} }
@ -22,91 +22,85 @@ err()
exit 1 exit 1
} }
check_prog()
{
if ! command -v "$1" >/dev/null 2>&1; then
printf "%s: missing required program %s\n" \
"${0##*/}" "$1" >&2
return 1
fi
return 0
}
check_runtime() check_runtime()
{ {
rc=0 _s=
for prog; do
check_prog "$prog" || rc=1 for _prog; do
command -v "$_prog" >/dev/null 2>&1 || _s=${_s:+"$_s "}$_prog
done done
return $rc
if [ -n "$_s" ]; then
err "missing required programs: $_s"
fi
} }
pst_catbox_moe() _curl()
{ {
curl $tor_flags -F "reqtype=fileupload" -F "fileToUpload=@$1" \ curl ${proxy:+-x "$proxy"} -Ss
}
impl_catbox_moe()
{
check_runtime curl
_curl -F "reqtype=fileupload" -F "fileToUpload=@$1" \
https://catbox.moe/user/api.php https://catbox.moe/user/api.php
} }
pst_ix_io() impl_pst_moe()
{ {
curl $tor_flags -F "f:1=@$1" http://ix.io/ check_runtime curl
_curl -F "content=<$1" -F "expire_after=86400" https://pst.moe/paste
} }
pst_pst_moe() impl_uguu()
{ {
curl $tor_flags -F "content=<$1" -F "expire_after=86400" \ check_runtime curl jq
https://pst.moe/paste
}
do_upload() _jq_prog='
{ if .success then
uploader=$1 (.files[] | [.url, .name] | @tsv)
else
"Error \(.errorcode): \(.description)"
end
'
for _f; do
shift shift
set -- "$@" -F "files[]=@$_f"
rc=0
if [ $# -eq 0 ]; then
$uploader - || rc=1
else
for arg; do
$uploader "$arg" || rc=1
done done
fi _curl "$@" "https://$uguu_host/upload.php" | jq -r "$_jq_prog"
return $rc
} }
tor_flags="-x socks5h://localhost:9050" proxy=
while getopts T flag; do while getopts x: flag; do
case $flag in case $flag in
T) tor_flags= ;; x) proxy=$OPTARG ;;
*) usage ;; *) usage ;;
esac esac
done done
shift $((OPTIND - 1)) shift $((OPTIND - 1))
if [ $# -eq 0 ]; then if [ $# -lt 2 ]; then
usage usage
fi fi
srv=$1 srv=$1
shift shift
check_runtime curl ${tor_flags:+tor}
url=
[ $# -eq 0 ] && set -- -
case $srv in case $srv in
catbox.moe) catbox.moe)
url=$(do_upload pst_catbox_moe "$@") impl=impl_catbox_moe
;;
ix.io)
url=$(do_upload pst_ix_io "$@")
;; ;;
pst.moe) pst.moe)
url=$(do_upload pst_pst_moe "$@") impl=impl_pst_moe
;;
uguu.se|cockfile.com)
impl=impl_uguu uguu_host=$srv
;; ;;
*) *)
err "unknown service $srv" err "unknown service $srv"
;; ;;
esac esac
[ $? -eq 0 ] || err "Couldn't upload files" $impl "$@"
printf "%s\n" "$url"