170 lines
4.0 KiB
Bash
Executable File
170 lines
4.0 KiB
Bash
Executable File
#!/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
|
|
sh) _ext=".sh" ;;
|
|
ksh) _ext=".ksh" ;;
|
|
txt) _ext=".txt" ;;
|
|
log) _ext=".log" ;;
|
|
gz) _gz=1 ;;
|
|
tgz) _tgz=1 ;;
|
|
rm) _rm=1 ;;
|
|
rml) _rmlast=1 ;;
|
|
ls) _ls=1 ;;
|
|
l|last) _last="-1" ;;
|
|
-[0-9]*) _last="$arg" ;;
|
|
ren|mv) _mv=1 ;;
|
|
renl|mvl) _mvlast=1 ;;
|
|
*) break; ;;
|
|
esac
|
|
shift;
|
|
done
|
|
|
|
remote_list() {
|
|
ssh $_sshhost \
|
|
"cd $_rpath/ \
|
|
&& ls -1tr"
|
|
}
|
|
|
|
if [ -n "$_ls" ]
|
|
then
|
|
remote_list \
|
|
| while read f
|
|
do
|
|
echo "$_weburi/$(echo "$f" | sed 's/ /%20/g') ($f)"
|
|
done
|
|
exit 0
|
|
fi
|
|
|
|
if [ -n "$_last" ]
|
|
then
|
|
remote_list \
|
|
| tail $_last \
|
|
| while read f
|
|
do
|
|
echo "$_weburi/$(echo "$f" | sed 's/ /%20/g') ($f)"
|
|
done
|
|
exit 0
|
|
fi
|
|
|
|
if [ -n "$_mv" ]
|
|
then
|
|
ssh $_sshhost \
|
|
"cd $_rpath/ \
|
|
&& mv -v \"$1\" \"$2\""
|
|
exit 0
|
|
fi
|
|
|
|
if [ -n "$_mvlast" ]
|
|
then
|
|
lastfile="$(remote_list | tail -1)"
|
|
ssh $_sshhost \
|
|
"cd $_rpath/ \
|
|
&& mv -v \"$lastfile\" \"$2\""
|
|
exit 0
|
|
fi
|
|
|
|
if [ -n "$_rm" ]
|
|
then
|
|
for file in "$@"
|
|
do
|
|
ssh $_sshhost \
|
|
"cd $_rpath/ \
|
|
&& rm -v \"$(basename "$file")\"" \
|
|
|| true
|
|
done
|
|
exit 0
|
|
fi
|
|
|
|
if [ -n "$_rmlast" ]
|
|
then
|
|
lastfile="$(remote_list | tail -1)"
|
|
ssh $_sshhost \
|
|
"cd $_rpath/ \
|
|
&& rm -v \"$lastfile\"" \
|
|
|| true
|
|
exit 0
|
|
fi
|
|
|
|
copy_and_print() {
|
|
echo "$_weburi/$(basename "$1")" \
|
|
| sed 's/ /%20/g' \
|
|
| xclip -f -r
|
|
echo
|
|
}
|
|
|
|
if [ $# -gt 0 ]
|
|
then
|
|
for file in "$@"
|
|
do
|
|
[ ! -e "$file" ] \
|
|
&& echo "File or directory not found: $file" \
|
|
&& exit 1
|
|
|
|
# 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] [ext] [<files>]"
|
|
echo " commands:"
|
|
echo " rm <files> - remove files"
|
|
echo " rml - remove last upload"
|
|
echo " ls - list files"
|
|
echo " -0..N - list N last uploads"
|
|
echo " ren <oldname> <newname> - rename file"
|
|
echo " renl <newname> - rename last uploaded file"
|
|
echo " extensions:"
|
|
echo " sh, ksh, txt, log - add extension to file"
|
|
echo " <files> - upload files"
|
|
exit 2
|
|
fi
|