diff options
author | clsr <clsr@clsr.net> | 2016-06-21 17:13:17 +0200 |
---|---|---|
committer | clsr <clsr@clsr.net> | 2016-06-21 17:13:17 +0200 |
commit | 53162ba80c540ced3244548640706d4b35a09bb5 (patch) | |
tree | 04a0ba81f672f5b19a8c944f304532d984be2f70 | |
parent | 141774f4c31917ee2689ec9ba0f7920e0aff2126 (diff) | |
download | gomf-tools-53162ba80c540ced3244548640706d4b35a09bb5.tar.gz gomf-tools-53162ba80c540ced3244548640706d4b35a09bb5.zip |
Add gomfload
-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 |