diff --git a/.bin/upload b/.bin/upload index a6a2df3..c20dcda 100755 --- a/.bin/upload +++ b/.bin/upload @@ -1,5 +1,15 @@ #!/bin/sh -e +# manage files in a webroot +# needs: sh (cp,rm), openssh (ssh,scp), gzip, tar, sed, tail, xclip + +# remote patch uploaded files should go +_rpath="/home/www/htdocs/ptrace/paste" +# web url where the uploaded files can be accessed +_weburi="https://ptrace.org" +# ssh user@host +_sshhost="sdk@codevoid.de" + for arg in "$@" do case "$arg" in @@ -7,57 +17,60 @@ do ksh) _ext=".ksh" ;; txt) _ext=".txt" ;; log) _ext=".log" ;; + gz) _gz=1 ;; + tgz) _tgz=1 ;; rm) _rm=1 ;; - rmlast) _rmlast=1 ;; + rml) _rmlast=1 ;; ls) _ls=1 ;; - -[0-9]?) _last="$arg" ;; - mv) _mv=1 ;; - mvlast) _mvlast=1 ;; + l|last) _last="-1" ;; + -[0-9]*) _last="$arg" ;; + ren|mv) _mv=1 ;; + renl|mvl) _mvlast=1 ;; *) break; ;; esac shift; done -upload_list() { - ssh sdk@codevoid.de \ - "cd /home/www/htdocs/ptrace/paste/ \ +remote_list() { + ssh $_sshhost \ + "cd $_rpath/ \ && ls -1tr" } if [ -n "$_ls" ] then - upload_list \ + remote_list \ | while read f do - echo "https://ptrace.org/$(echo "$f" | sed 's/ /%20/g') ($f)" + echo "$_weburi/$(echo "$f" | sed 's/ /%20/g') ($f)" done exit 0 fi if [ -n "$_last" ] then - upload_list \ + remote_list \ | tail $_last \ | while read f do - echo "https://ptrace.org/$(echo "$f" | sed 's/ /%20/g') ($f)" + echo "$_weburi/$(echo "$f" | sed 's/ /%20/g') ($f)" done exit 0 fi if [ -n "$_mv" ] then - ssh sdk@codevoid.de \ - "cd /home/www/htdocs/ptrace/paste/ \ + ssh $_sshhost \ + "cd $_rpath/ \ && mv -v \"$1\" \"$2\"" exit 0 fi if [ -n "$_mvlast" ] then - lastfile="$(upload_list | tail -1)" - ssh sdk@codevoid.de \ - "cd /home/www/htdocs/ptrace/paste/ \ + lastfile="$(remote_list | tail -1)" + ssh $_sshhost \ + "cd $_rpath/ \ && mv -v \"$lastfile\" \"$2\"" exit 0 fi @@ -66,8 +79,8 @@ if [ -n "$_rm" ] then for file in "$@" do - ssh sdk@codevoid.de \ - "cd /home/www/htdocs/ptrace/paste/ \ + ssh $_sshhost \ + "cd $_rpath/ \ && rm -v \"$(basename "$file")\"" \ || true done @@ -76,43 +89,81 @@ fi if [ -n "$_rmlast" ] then - lastfile="$(upload_list | tail -1)" - ssh sdk@codevoid.de \ - "cd /home/www/htdocs/ptrace/paste/ \ + lastfile="$(remote_list | tail -1)" + ssh $_sshhost \ + "cd $_rpath/ \ && rm -v \"$lastfile\"" \ || true exit 0 fi -echo $# +copy_and_print() { + echo "$_weburi/$(basename "$1")" \ + | sed 's/ /%20/g' \ + | xclip -f -r + echo +} if [ $# -gt 0 ] then for file in "$@" do - [ ! -f "$file" ] \ - && echo "file not found: $file" \ + [ ! -e "$file" ] \ + && echo "File or directory not found: $file" \ && exit 1 - # make sure the file is least owner rw and group r - # the sticky bit in the destiation folder will assign user+group - chmod u+rw,g+r "$file" - scp "$file" "sdk@codevoid.de:/home/www/htdocs/ptrace/paste/${file}${_ext}" - echo "https://ptrace.org/$(basename "$file")${_ext}" \ - | sed 's/ /%20/g' \ - | xclip -f -r - echo + + # handle file (plain), no archive mode + if [ -f "$file" ] && [ -z "$_tgz" ] && [ -z "$_gz" ] + then + # make sure the file is least owner rw and group r + # the sticky bit in the destiation folder will assign user+group + chmod u+rw,g+r "$file" + echo "Uploading $file" + scp -q "$file" "$_sshhost:$_rpath/${file}${_ext}" + copy_and_print "${file}${_ext}" + continue + fi + + # directories can only be handled as tgz archive + [ -d "$file" ] && _tgz=1 + + # handle file or directory (tgz mode) + if [ -n "$_tgz" ] + then + echo "Uploading $file as ${file}${_ext}" + _ext="$_ext.tgz" + tar czf - "$file" | ssh $_sshhost "pv - > $_rpath/${file}${_ext}" + copy_and_print "${file}${_ext}" + continue + fi + + # handle file (gz mode) + if [ -f "$file" ] && [ -n "$_gz" ] + then + echo "Uploading $file as ${file}${_ext}" + _ext="$_ext.gz" + cat "$file" gzip -o - "$file" | $_sshhost "pv - > $_rpath/${file}${_ext}" + copy_and_print "${file}${_ext}" + continue + fi + + # hopefully never reached + echo "Unhandled situation for: $file" done fi if [ $# -eq 0 ] then - echo "usage: upload [command] []" + echo "usage: upload [command] [ext] []" echo " commands:" - echo " rm - remove files" - echo " rmlast - remove last upload" - echo " ls - list files" - echo " -0..N - list N last uploads" - echo " mv - rename file" - echo " - upload files" + echo " rm - remove files" + echo " rml - remove last upload" + echo " ls - list files" + echo " -0..N - list N last uploads" + echo " ren - rename file" + echo " renl - rename last uploaded file" + echo " extensions:" + echo " sh, ksh, txt, log - add extension to file" + echo " - upload files" exit 2 fi