Skip to content

Commit

Permalink
shorten urls and dereference symlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielFGray committed Jan 9, 2017
1 parent f858e26 commit b8b022d
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions tekup
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ OPTIONS:
HELP
}

has() {
local verbose
if [[ $1 = '-v' ]]; then
verbose=1
shift
fi
for c; do c="${c%% *}"
if ! command -v "$c" &> /dev/null; then
(( "$verbose" > 0 )) && err "$c not found"
return 1
fi
done
}

upload() {
local file mime url post curl_verbosity
file="$1"
Expand All @@ -47,7 +61,7 @@ upload() {
return 1
fi

mime=$(file -ib "$file")
mime=$(file -Lib "$file")
mime="${mime%%/*}"

if (( verbose < 1 )); then
Expand All @@ -58,24 +72,24 @@ upload() {
curl_verbosity='-v'
fi

if [[ $mime == 'text' ]]; then
if [[ $mime = 'text' ]]; then
post=$(command curl ${curl_verbosity} --data "title=${file##*/}" --data-urlencode "code=$(< "${file}")" https://api.teknik.io/v1/Paste)
else
post=$(command curl ${curl_verbosity} -F "contentType=${mime}" -F "file=@${file}" https://api.teknik.io/v1/Upload)
fi

(( verbose > 0 )) && info "response: ${c_reset}${post}"

[[ -z "$post" || "$post" != *'http'* || "$post" == *'error'* ]] && die "error uploading ${file}"
[[ -z "$post" || "$post" != *'http'* || "$post" = *'error'* ]] && die "error uploading ${file}"

url=$(grep -oP 'https://[[:alnum:]?=%/_.:,;~@!#$&()*+-]+' <<< "$post")
shorten="${url/upload/u}"
shorten="${url/paste/p}"
printf '%s: %s\n' "$file" "$shorten"
url="${url/upload/u}"
url="${url/paste/p}"
printf '%s: %s\n' "$file" "$url"
}
export -f upload

command -v curl &> /dev/null || die 'curl not found'
has -v curl || die

OPTERR=0
while getopts "hvp:" opt; do
Expand Down

0 comments on commit b8b022d

Please sign in to comment.