2024-02-14 07:54:31 +01:00
|
|
|
#!/bin/sh -e
|
|
|
|
|
2024-12-23 16:36:55 +01:00
|
|
|
# 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"
|
|
|
|
|
2024-12-23 14:32:10 +01:00
|
|
|
for arg in "$@"
|
|
|
|
do
|
|
|
|
case "$arg" in
|
|
|
|
sh) _ext=".sh" ;;
|
|
|
|
ksh) _ext=".ksh" ;;
|
|
|
|
txt) _ext=".txt" ;;
|
|
|
|
log) _ext=".log" ;;
|
2024-12-23 16:36:55 +01:00
|
|
|
gz) _gz=1 ;;
|
|
|
|
tgz) _tgz=1 ;;
|
2024-12-23 14:32:10 +01:00
|
|
|
rm) _rm=1 ;;
|
2024-12-23 16:36:55 +01:00
|
|
|
rml) _rmlast=1 ;;
|
2024-12-23 14:32:10 +01:00
|
|
|
ls) _ls=1 ;;
|
2024-12-23 16:36:55 +01:00
|
|
|
l|last) _last="-1" ;;
|
|
|
|
-[0-9]*) _last="$arg" ;;
|
|
|
|
ren|mv) _mv=1 ;;
|
|
|
|
renl|mvl) _mvlast=1 ;;
|
2024-12-23 14:32:10 +01:00
|
|
|
*) break; ;;
|
|
|
|
esac
|
|
|
|
shift;
|
|
|
|
done
|
|
|
|
|
2024-12-23 16:36:55 +01:00
|
|
|
remote_list() {
|
|
|
|
ssh $_sshhost \
|
|
|
|
"cd $_rpath/ \
|
2024-12-23 15:26:32 +01:00
|
|
|
&& ls -1tr"
|
|
|
|
}
|
|
|
|
|
2024-12-23 17:32:40 +01:00
|
|
|
copy_and_print() {
|
|
|
|
echo "$_weburi/$(basename "$1")" \
|
|
|
|
| sed 's/ /%20/g' \
|
|
|
|
| xclip -f -r
|
|
|
|
echo
|
|
|
|
}
|
|
|
|
|
|
|
|
# handle stdin
|
|
|
|
if [[ ! -t 0 ]]
|
|
|
|
then
|
|
|
|
fname="$1"
|
|
|
|
[ -z "$fname" ] \
|
|
|
|
&& fname="$(date +"%Y-%m-%d_%H%M%S").txt"
|
|
|
|
cat - | ssh $_sshhost "cat - > \"$_rpath/$fname\""
|
|
|
|
copy_and_print "$fname"
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2024-12-23 15:26:32 +01:00
|
|
|
if [ -n "$_ls" ]
|
|
|
|
then
|
2024-12-23 16:36:55 +01:00
|
|
|
remote_list \
|
2024-12-23 15:26:32 +01:00
|
|
|
| while read f
|
|
|
|
do
|
2024-12-23 16:36:55 +01:00
|
|
|
echo "$_weburi/$(echo "$f" | sed 's/ /%20/g') ($f)"
|
2024-12-23 15:26:32 +01:00
|
|
|
done
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "$_last" ]
|
|
|
|
then
|
2024-12-23 16:36:55 +01:00
|
|
|
remote_list \
|
2024-12-23 15:26:32 +01:00
|
|
|
| tail $_last \
|
|
|
|
| while read f
|
|
|
|
do
|
2024-12-23 16:36:55 +01:00
|
|
|
echo "$_weburi/$(echo "$f" | sed 's/ /%20/g') ($f)"
|
2024-12-23 15:26:32 +01:00
|
|
|
done
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "$_mv" ]
|
|
|
|
then
|
2024-12-23 16:36:55 +01:00
|
|
|
ssh $_sshhost \
|
|
|
|
"cd $_rpath/ \
|
2024-12-23 15:26:32 +01:00
|
|
|
&& mv -v \"$1\" \"$2\""
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "$_mvlast" ]
|
|
|
|
then
|
2024-12-23 16:36:55 +01:00
|
|
|
lastfile="$(remote_list | tail -1)"
|
|
|
|
ssh $_sshhost \
|
|
|
|
"cd $_rpath/ \
|
2024-12-23 15:26:32 +01:00
|
|
|
&& mv -v \"$lastfile\" \"$2\""
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "$_rm" ]
|
2024-12-22 09:05:32 +01:00
|
|
|
then
|
2024-12-23 14:32:10 +01:00
|
|
|
for file in "$@"
|
2024-12-22 09:05:32 +01:00
|
|
|
do
|
2024-12-23 16:36:55 +01:00
|
|
|
ssh $_sshhost \
|
|
|
|
"cd $_rpath/ \
|
2024-12-23 15:26:32 +01:00
|
|
|
&& rm -v \"$(basename "$file")\"" \
|
2024-12-23 14:32:10 +01:00
|
|
|
|| true
|
2024-12-22 09:05:32 +01:00
|
|
|
done
|
2024-12-23 14:32:10 +01:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2024-12-23 15:26:32 +01:00
|
|
|
if [ -n "$_rmlast" ]
|
2024-12-23 14:32:10 +01:00
|
|
|
then
|
2024-12-23 16:36:55 +01:00
|
|
|
lastfile="$(remote_list | tail -1)"
|
|
|
|
ssh $_sshhost \
|
|
|
|
"cd $_rpath/ \
|
2024-12-23 15:26:32 +01:00
|
|
|
&& rm -v \"$lastfile\"" \
|
|
|
|
|| true
|
2024-12-23 14:32:10 +01:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
2024-12-23 15:26:32 +01:00
|
|
|
if [ $# -gt 0 ]
|
2024-12-22 09:05:32 +01:00
|
|
|
then
|
2024-12-23 17:32:40 +01:00
|
|
|
for item in "$@"
|
2024-12-22 09:05:32 +01:00
|
|
|
do
|
2024-12-23 17:32:40 +01:00
|
|
|
# the whole handling shall run in a subshell so all variables
|
|
|
|
# are reset when we leave the scope
|
|
|
|
# note, that this requires us to return, instead fo continue
|
|
|
|
# to leave the loop iteration
|
|
|
|
(
|
|
|
|
|
|
|
|
[ ! -e "$item" ] \
|
|
|
|
&& echo "File or directory not found: $item" \
|
2024-12-23 15:26:32 +01:00
|
|
|
&& exit 1
|
2024-12-23 16:36:55 +01:00
|
|
|
|
2024-12-23 17:32:40 +01:00
|
|
|
# target filename
|
|
|
|
file="$(basename "$item")$_ext"
|
|
|
|
|
2024-12-23 16:36:55 +01:00
|
|
|
# handle file (plain), no archive mode
|
2024-12-23 17:32:40 +01:00
|
|
|
if [ -f "$item" ] && [ -z "$_tgz" ] && [ -z "$_gz" ]
|
2024-12-23 16:36:55 +01:00
|
|
|
then
|
|
|
|
# make sure the file is least owner rw and group r
|
|
|
|
# the sticky bit in the destiation folder will assign user+group
|
2024-12-23 17:32:40 +01:00
|
|
|
chmod u+rw,g+r "$item"
|
|
|
|
echo "Uploading $item as $file"
|
|
|
|
scp -q "$item" "$_sshhost:\"$_rpath/$file\""
|
|
|
|
copy_and_print "$file"
|
|
|
|
return
|
2024-12-23 16:36:55 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
# directories can only be handled as tgz archive
|
2024-12-23 17:32:40 +01:00
|
|
|
[ -d "$item" ] && _tgz=1
|
2024-12-23 16:36:55 +01:00
|
|
|
|
|
|
|
# handle file or directory (tgz mode)
|
|
|
|
if [ -n "$_tgz" ]
|
|
|
|
then
|
2024-12-23 17:32:40 +01:00
|
|
|
file="$file.tgz"
|
|
|
|
echo "Uploading $item as $file"
|
|
|
|
# moving closer to the item to not archive the whole path
|
|
|
|
cd $(dirname "$item")
|
|
|
|
tar czf - "$(basename "$item")" \
|
|
|
|
| ssh $_sshhost "pv - > \"$_rpath/$file\""
|
|
|
|
copy_and_print "$file"
|
|
|
|
return
|
2024-12-23 16:36:55 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
# handle file (gz mode)
|
2024-12-23 17:32:40 +01:00
|
|
|
if [ -f "$item" ] && [ -n "$_gz" ]
|
2024-12-23 16:36:55 +01:00
|
|
|
then
|
2024-12-23 17:32:40 +01:00
|
|
|
file="$file.gz"
|
|
|
|
echo "Uploading \"$item\" as \"$file\""
|
|
|
|
cat "$item" | gzip \
|
|
|
|
| ssh $_sshhost "pv - > \"$_rpath/$file\""
|
|
|
|
copy_and_print "$file"
|
|
|
|
return
|
2024-12-23 16:36:55 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
# hopefully never reached
|
2024-12-23 17:32:40 +01:00
|
|
|
echo "Unhandled situation for: $item"
|
|
|
|
|
|
|
|
) # leave scope & reset variables
|
2024-12-22 09:05:32 +01:00
|
|
|
done
|
|
|
|
fi
|
2024-12-23 15:26:32 +01:00
|
|
|
|
|
|
|
if [ $# -eq 0 ]
|
|
|
|
then
|
2024-12-23 16:36:55 +01:00
|
|
|
echo "usage: upload [command] [ext] [<files>]"
|
2024-12-23 17:32:40 +01:00
|
|
|
echo " upload [filename] < file"
|
|
|
|
echo " cat file | upload [filename]"
|
|
|
|
echo
|
|
|
|
echo " <files> - files or directories to upload"
|
|
|
|
echo
|
|
|
|
echo " Commands:"
|
2024-12-23 16:36:55 +01:00
|
|
|
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"
|
2024-12-23 17:32:40 +01:00
|
|
|
echo
|
|
|
|
echo " Extensions:"
|
2024-12-23 16:36:55 +01:00
|
|
|
echo " sh, ksh, txt, log - add extension to file"
|
2024-12-23 17:32:40 +01:00
|
|
|
echo
|
|
|
|
echo "Special Case:"
|
|
|
|
echo " When data is piped to the script it expects only one"
|
|
|
|
echo " optional argument, which would be the target filename."
|
|
|
|
echo
|
2024-12-23 15:26:32 +01:00
|
|
|
exit 2
|
|
|
|
fi
|