Support for the new shortlink feature.

This commit is contained in:
Jack L. Frost 2015-06-21 16:05:12 +03:00
parent 30c57c8685
commit 9424b975ed

91
sup
View File

@ -31,6 +31,7 @@ sup.usage() {
-F # Make a fullscreen shot instead of prompting for a window/area. Implies -s. -F # Make a fullscreen shot instead of prompting for a window/area. Implies -s.
-D <num> # Delay the shot by <num> seconds. -D <num> # Delay the shot by <num> seconds.
-p # Make the file private. Requires \$secret to be set in the config. -p # Make the file private. Requires \$secret to be set in the config.
-u # Generate a shortlink from URL.
Config options (~/.suprc): Config options (~/.suprc):
secret # Your personal token. Get it at https://zfh.so/settings_form secret # Your personal token. Get it at https://zfh.so/settings_form
@ -53,12 +54,39 @@ sup.get_hash() {
} }
sup.upload() { sup.upload() {
if (( flag_scrot )); then
file=$(sup.scrot) || { return 1; }
else
(( $# )) || {
sup.usage
return 1
}
target="$1"
if sup.if_url "$target"; then
file=$(sup.mktemp "$cfg_tmp_dir")
curl -skL "$target" > "$file" || {
sup.err "Could not download file."
return 1
}
else
file="$target"
[[ -f "$file" ]] || {
sup.err "No such file: ${file}"
return 1
}
fi
fi
sup.get_hash "$file" sup.get_hash "$file"
if curl -fsL "${cfg_service_url}/hell/${file_hash}" &>/dev/null; then if curl -fsL "${cfg_service_url}/api?mode=file&file=${file_hash}" >/dev/null; then
printf '%s\n' "${cfg_service_url}/hell/${file_hash}" printf '%s\n' "${cfg_service_url}/hell/${file_hash}"
else else
curl -fsL \ if curl -fsL \
-F file="@$file" \ -F file="@$file" \
-F upload_mode='api' \ -F upload_mode='api' \
-F flag_private="$flag_private" \ -F flag_private="$flag_private" \
@ -66,6 +94,23 @@ sup.upload() {
-F submit="" \ -F submit="" \
-A 'zerofiles.org upload script' \ -A 'zerofiles.org upload script' \
"$cfg_service_url/upload" "$cfg_service_url/upload"
then
if (( flag_rm )); then
sup.msg "Removing file: $file"
rm "$file"
fi
else
sup.err 'Something has gone wrong with the upload.'
return 7
fi
fi
}
sup.url() {
declare url=$1
if ! curl -fsL "$cfg_service_url/api?mode=url_add&url=${url}&raw=1"; then
sup.err 'Something went wrong!'
fi fi
} }
@ -167,6 +212,8 @@ main() {
flag_scrot_fullscreen='1' flag_scrot_fullscreen='1'
;; ;;
-u|--url) action='url';;
-h|--help|--usage) sup.usage; return;; -h|--help|--usage) sup.usage; return;;
--) shift; break;; --) shift; break;;
@ -178,42 +225,10 @@ main() {
sup.env sup.env
if (( flag_scrot )); then case "${action:-upload}" in
file=$(sup.scrot) || { return 1; } upload) sup.upload;;
else url) sup.url "$1";;
(( $# )) || { esac
sup.usage
return 1
}
target="$1"
if sup.if_url "$target"; then
file=$(sup.mktemp "$cfg_tmp_dir")
curl -skL "$target" > "$file" || {
sup.err "Could not download file."
return 1
}
else
file="$target"
[[ -f "$file" ]] || {
sup.err "No such file: ${file}"
return 1
}
fi
fi
if sup.upload; then
if (( "$flag_rm" )); then
sup.msg "Removing file: $file"
rm "$file"
fi
else
sup.err 'Something has gone wrong with the upload.'
return 7
fi
} }
sup.set_argv "$@" sup.set_argv "$@"