sharefile

scripts for quickly sharing data to servers
git clone git://jb55.com/sharefile
Log | Files | Refs | README

sharefile (1320B)


      1 #!/usr/bin/env bash
      2 
      3 set -e
      4 
      5 urlencode() {
      6   perl '-MURI::Escape' -e 'print uri_escape($ARGV[0]);' "$1"
      7 }
      8 
      9 name=""
     10 errord=0
     11 OPTIND=1
     12 
     13 show_help () {
     14   echo "sharefile [-n newname.png] <FILE>"
     15   echo ""
     16   echo "    -n NAME         set the destination file name"
     17   echo "    -d DESTINATION  set the destination folder. eg: me.com:files"
     18   echo "                    export SHAREFILE_HOST=me.com:files/ to set default"
     19   echo ""
     20   echo "    -u URL          set the destination folder. eg: me.com:files"
     21   echo "                    export SHAREFILE_URL=http://me.com/files/ to set default"
     22   exit 1
     23 }
     24 
     25 missing_env() {
     26   >&2 echo "missing environment variable $1"
     27   exit 1
     28 }
     29 
     30 while getopts "h?n:u:d:" opt; do
     31   case "$opt" in
     32     h|\?)
     33       show_help
     34       ;;
     35     n)
     36       name="$OPTARG"
     37       ;;
     38     d)
     39       SHAREFILE_HOST="$OPTARG"
     40       ;;
     41     u)
     42       SHAREFILE_URL="$OPTARG"
     43       ;;
     44   esac
     45 done
     46 
     47 shift $((OPTIND-1))
     48 [ "$1" = "--" ] && shift
     49 
     50 file="$1"
     51 
     52 [[ -z $SHAREFILE_HOST ]] && missing_env "SHAREFILE_HOST, eg: me.com:public/files/"
     53 [[ -z $SHAREFILE_URL ]] && missing_env "SHAREFILE_URL, eg: http://me.com/files/"
     54 [[ -z "$file" ]] && show_help
     55 
     56 name="${name:-"$(basename "$file")"}"
     57 dest="${SHAREFILE_HOST}${name}"
     58 
     59 rsync -zq --chmod=a=r "$file" "$dest"
     60 
     61 echo "${SHAREFILE_URL}$(urlencode "$(basename "${name}")")"