## vim:ft=zsh ## put - make a file available via HTTP ## supports as many 'failover' hosts as an array can hold ## Usage: put ## Copyright © 2008 - 2010 by Birte Friesel ## License: WTFPL typeset hostname ssh_name typeset -a hosts typeset target_dir server_prefix max_size typeset file remote_file illegal typeset -i pic make_random zmodload zsh/stat if [[ -z ${commands[curl]} ]]; then function curl { return 0; } print -P "%B%F{yellow}curl not present, won't be able to check upload success%f%b" fi function put_nocturne { hostname='nocturne.derf0.net' ssh_name='nocturne' target_dir='web/default/out/tmp' server_prefix='https://nocturne.derf0.net/tmp/' max_size=0 } function put_arclight { hostname='arclight.derf0.net' ssh_name='arclight' target_dir='web/default/out/tmp' server_prefix='https://arclight.derf0.net/tmp/' max_size=0 } function put_chaosdorf { hostname='extern.chaosdorf.de' ssh_name='extern.chaosdorf.de' target_dir='public_html/tmp' server_prefix='https://chaosdorf.de/~derf/tmp/' max_size=0 } hosts=(nocturne arclight chaosdorf) while [[ ${1} == -* ]] { case ${1} in -|--) shift; break ;; -r) make_random=1 ;; *) if [[ -n ${functions[put_${1#-}]} ]]; then hosts=(${1#-}) else (( $(eval echo '$#'hosts_${1#-}) )) && eval hosts=\(\$hosts_${1#-}\) fi ;; esac shift } if [[ ! -e ${1} ]] { echo "${1}: No such file" >&2 return 1 } if [[ -d ${1} ]] { tar -C ${1:h} -cJf /tmp/${1:t}.tar.xz ${1:t} file=/tmp/${1:t}.tar.xz } else { file=${1} } remote_file=${file:t} if (( make_random )) { remote_file=$(mktemp -u XXXXXX) } while (( ${#hosts} )) { put_${hosts[1]} ping -c 1 ${hostname} &> /dev/null || ping6 -c 1 ${hostname} &> /dev/null if (( ? == 0 )) { if (( max_size > 0 && $(zstat +size ${file}) > max_size && ${#hosts} > 1 )) { shift hosts continue } if [[ ${remote_file} == ${~illegal} ]] { remote_file=${remote_file:r} } lftp -c "connect sftp://${ssh_name}; cd ${target_dir}; put '${file}'"\ " -o '${remote_file}'; chmod 644 '${remote_file}'" remote_file=${remote_file// /%20} remote_file=${remote_file//\#/%23} remote_file=${remote_file//\[/%5B} remote_file=${remote_file//\]/%5D} url=${server_prefix}${remote_file} if $(curl -fI ${server_prefix}${remote_file} &> /dev/null); then echo ${server_prefix}${remote_file} return 0 fi } shift hosts } return 1