#!/bin/bash ver="0.4.1" syntax="Syntax: $0 config_name [file]" # defaults remote_user=$USER remote_ident=$HOME/.ssh/id_rsa random_name=no chmod=u=rwx,g=rw,o=r is_screenshot=no screenshot_direct=no screenshot_direct_format=png delete_after_upload=no track_progress=no track_interval=30 LC_ALL="en_US.UTF-8" PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin" CFGDIR="$HOME/.esupload" # todo: look for a native way urlEncode() { echo -n "$1" | perl -MURI::Escape -ne 'print uri_escape($_)' } checkDep() { for prog in "$@"; do command -v $prog >/dev/null 2>&1 || { echo -e "Please install these dependencies first: \n$@" 1>&2 && exit 1; } done } nameGen() { perl -e 'srand; open FILE, "; $name = ucfirst($arr[rand @arr]).ucfirst($arr[rand @arr]).ucfirst($arr[rand @arr]); $name =~ s/\R//g; print $name;' } cleanUp() { if [ -d "$1" ]; then rm -rf "$1" > /dev/null 2>&1 fi } # just to be on the safe side, everything except terminal-notifier and pv should be available out-of-the-box checkDep pv terminal-notifier sips screencapture hexdump perl tempdir=$(mktemp -d) trap "{ cleanUp $tempdir; }" EXIT SIGINT SIGTERM if [ -d "$CFGDIR" ]; then if [ -z "$1" ]; then echo $syntax 1>&2 && exit 1; fi source "$CFGDIR/$1" > /dev/null 2>&1 || { echo -e "Error while reading profile: $1" 1>&2 && exit 1; } if [ -z "$remote_host" ] || [ -z "$remote_dir" ] || [ -z "$remote_url" ]; then echo -e "Those params are required: \nremote_host remote_dir remote_url\n\nPlease provide them in $CFGDIR/$1" 1>&2 && exit 1 fi id=$(hexdump -n 16 -v -e '/1 "%02X"' /dev/urandom | tr '[:upper:]' '[:lower:]') localfile="$2" if [ "$screenshot_direct" == "yes" ]; then localfile="$tempdir/$id.$screenshot_direct_format" screencapture -i -r -t $screenshot_direct_format $localfile > /dev/null 2>&1 fi if [ -z "$localfile" ]; then echo $syntax 1>&2 && exit 1; fi if [ -f "$localfile" ]; then localfilename=$(basename "$localfile") localfileext=${localfilename##*.} terminal-notifier -title "$localfilename" -group "$id" -message "uploading" > /dev/null 2>&1 if [ "$is_screenshot" == "yes" ]; then if [ "$screenshot_direct" != "yes" ]; then cp "$localfile" "$tempdir" > /dev/null 2>&1 || echo "Error while copying file to temp dir" 1>&2 fi retina=$(system_profiler SPDisplaysDataType | grep "Retina:" | tr -d '[[:space:]]') retina10124=$(system_profiler SPDisplaysDataType | grep "Resolution:.*Retina" | wc -l | tr -d '[[:space:]]') if [ "$retina" == "Retina:Yes" ] || [ "$retina10124" == "1" ]; then height=$(sips -g pixelHeight "$tempdir/$localfilename" | grep 'pixelHeight' | cut -d: -f2) heighthalf=$(($height / 2)) if [ "$heighthalf" -gt "0" ]; then sips --resampleHeight "$heighthalf" "$tempdir/$localfilename" --out "$tempdir/$localfilename" > /dev/null 2>&1 fi fi loc="$tempdir/$localfilename" else loc="$localfile" fi if [ "$random_name" == "yes" ]; then rem="$remote_dir/$id.$localfileext" url="$remote_url/$id.$localfileext" urlenc="$remote_url/"$(urlEncode "$id.$localfileext") elif [ "$random_name" == "namegen" ]; then namegen=$(nameGen) rem="$remote_dir/$namegen.$localfileext" url="$remote_url/$namegen.$localfileext" urlenc="$remote_url/"$(urlEncode "$namegen.$localfileext") else rem="$remote_dir/$localfilename" url="$remote_url/$localfilename" urlenc="$remote_url/"$(urlEncode "$localfilename") fi if [ "$track_progress" == "yes" ]; then res=1 while read line; do if [ "$line" -eq "$line" ] 2>/dev/null; then # is it a number? if [ "$line" != "100" ] && [ "$line" != "0" ]; then terminal-notifier -title "$localfilename" -group "$id" -message "$line% uploaded" > /dev/null 2>&1 fi elif [ "$line" == "res:0 0" ]; then res=0 else echo "$line" 1>&2 # in case of error fi done < <((pv -n -i $track_interval "$loc" | ssh -q -i "$remote_ident" "$remote_user@$remote_host" "cat > '$rem' && chmod $chmod '$rem'"; echo "res:${PIPESTATUS[*]}") 2>&1) # what's happening here: # we're sending a file through pv and ssh, since scp and rsync can't read files from stdin and there is no reliable way to monitor their progress # after that we're also reading PIPESTATUS to check the result else rem=$(printf %q "$rem") rsync -q --chmod=$chmod -e "ssh -i \"$remote_ident\"" "$loc" "$remote_user@$remote_host:$rem" > /dev/null res=$? fi if [ $res -ne 0 ]; then terminal-notifier -title "$localfilename" -group "$id" -message "failed to upload" > /dev/null 2>&1 else echo -n "$urlenc" | pbcopy terminal-notifier -title "$localfilename" -group "$id" -message "uploaded" -open "$url" > /dev/null 2>&1 fi if [ "$delete_after_upload" == "yes" ]; then rm -f "$localfile" > /dev/null 2>&1 fi else echo "File $localfile is not found" 1>&2 && exit 1 fi else echo -e "Can't find config directory: $CFGDIR \nFor usage information please visit https://github.com/deseven/esupload" 1>&2 && exit 1 fi