Skip to content

Instantly share code, notes, and snippets.

@Ser-Gen
Forked from loyd/ovrload
Last active August 29, 2015 14:13
Show Gist options
  • Save Ser-Gen/2247eaa5ea32dbad3f8e to your computer and use it in GitHub Desktop.
Save Ser-Gen/2247eaa5ea32dbad3f8e to your computer and use it in GitHub Desktop.
#!/bin/bash
# ovrload.ru file uploader
# ovrload — former troloload
VERSION='v0.1.3'
URL='http://ovrload.ru'
SHORTER_URL='http://tinyurl.com'
FAILURES=0 # number of upload fails
LONGTIME=0 # whether to make long-time links
SHORT=0 # whether to make short links
COPY=0 # whether to copy link
EXTENSION= # forced extension for upload
usage() {
echo "Usage:"
echo " $(basename $0) [-l|s|c] [-e <ext>] [files...] Upload files (use '-' for stdin)"
echo " $(basename $0) [-h|v] Show meta information"
echo "Options:"
echo " -l Make long-time links"
echo " -s Make short links (uses tinyurl.com)"
echo " -e Forced extenstion (good with stdin)"
echo " -h Show this message and quit"
echo " -c Copy link"
echo " -v Show version ($VERSION) number and quit"
}
gen_name() {
local ext="$EXTENSION"
if [ -z "$ext" ] && [ "$1" != '-' ]; then
local filename=$(basename "$1")
ext=".${filename##*.}"
# No extenstion
if [ "$ext" == ".$filename" ]; then ext=''; fi
fi
printf "$(< /dev/urandom tr -dc a-zA-Z0-9 | head -c16)$ext"
}
upload() {
local name=$(gen_name "$1")
local link=$(curl -sL -F 'fileframe=true' \
-F "file=@$1;filename=$name" \
-F "temp=$((LONGTIME == 0))" \
"$URL" | grep -o "\"$URL.*\"" | head -n 1)
echo "${link:1:-1}"
}
shorten() {
echo $(curl -s "$SHORTER_URL/api-create.php?url=$1")
}
## Analysis of options
while getopts ":lsce:hv" OPT; do
case "$OPT" in
l)
LONGTIME=1
;;
s)
SHORT=1
;;
e)
EXTENSION=$OPTARG
if [ "${EXTENSION:0:1}" != '.' ]; then
EXTENSION=".$EXTENSION"
fi
;;
c)
COPY=1
;;
h)
usage
exit 0
;;
v)
echo $VERSION
exit 0
;;
\?)
usage >&2
exit 42
;;
esac
done
shift $((OPTIND-1))
## Process files
if [ $# -eq 0 ]; then
usage >&2
exit 42;
fi
for FILE; do
RESULT=$(upload "$FILE")
if [ "$FILE" == '-' ]; then FILE='(stdin)'; fi
if [[ "$RESULT" =~ ^"$URL" ]] && [ $SHORT -ne 0 ]; then
RESULT=$(shorten "$RESULT")
fi
if [[ "$RESULT" =~ ^http:// ]]; then
if [ $COPY -ne 0 ]; then
echo -n "$RESULT" | xclip -filter -selection clipboard
else
echo "$FILE: $RESULT"
fi
else
echo "$FILE: fail"
let "FAILURES+=1"
fi
done
exit $FAILURES
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment