diff options
-rwxr-xr-x | gomfload.sh | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/gomfload.sh b/gomfload.sh new file mode 100755 index 0000000..d3963f9 --- /dev/null +++ b/gomfload.sh @@ -0,0 +1,85 @@ +#!/bin/sh + +# This software is released into public domain. +# It is provided "as is", without warranties or conditions of any kind. +# Anyone is free to use, modify, redistribute and do anything with this software. + +# requires: POSIX sh, curl, tr + +host="${GOMF_HOST:-https://example.com/}" + +prog="$(basename "$0")" +stdin=1 + +usage() { + printf "Usage: %s < file\n" "$prog" + printf " command | %s\n" "$prog" + printf " %s file1 file2 file3 ...\n" "$prog" + exit +} + +upload() { + file='' + if [ $# -ge 1 ]; then + filename="$(printf "%s" "$(basename "$1")" | tr -c -d 'a-zA-Z0-9_.+ -')" + if [ -n "$filename" ]; then + file=";filename=$filename" + fi + fi + url="$(curl --progress-bar -F "files[]=@-$file" "${host%/}/upload.php?output=gyazo")" + case "$url" in + http*) + printf "%s\n" "$url" + ;; + ?*) + printf "%s: error: %s\n" "$prog" "$url" >&2 + return 1 + ;; + '') + return 1 + ;; + esac +} + +upload_file() { + stdin=0 + url="$(upload "$1" < "$1")" + if [ $? -ne 0 ]; then + exit 1 + fi + printf "%s: %s\n" "$1" "$url" +} + +while [ $# -gt 0 ]; do + case "$1" in + -h|--help) + usage + ;; + + --) + shift + break + ;; + + -?*) + printf "%s: unrecognized option '%s'\n" "$prog" "$1" >&2 + exit 2 + ;; + + *) + upload_file "$1" + ;; + esac + + shift +done + +while [ $# -gt 0 ]; do + upload_file "$1" + shift +done + +if [ "$stdin" -ne 0 ]; then + upload + exit $? +fi |