2024-02-14 07:54:31 +01:00
|
|
|
#!/bin/sh -e
|
|
|
|
|
2024-12-23 16:36:55 +01:00
|
|
|
# manage files in a webroot
|
2024-12-23 18:34:22 +01:00
|
|
|
# needs:
|
|
|
|
# - sh: cp, rm, mv, echo, basename, dirname
|
|
|
|
# - openssh: ssh, scp
|
|
|
|
# - coreutils: chmod, sed, tail
|
|
|
|
# - archivers: gzip, tar
|
|
|
|
# - xclip
|
2024-12-23 16:36:55 +01:00
|
|
|
|
2024-12-23 18:21:09 +01:00
|
|
|
###
|
|
|
|
### CONFIGURATION
|
|
|
|
###
|
|
|
|
|
2024-12-23 16:36:55 +01:00
|
|
|
# remote patch uploaded files should go
|
|
|
|
_rpath="/home/www/htdocs/ptrace/paste"
|
2024-12-23 18:49:14 +01:00
|
|
|
|
2024-12-23 16:36:55 +01:00
|
|
|
# web url where the uploaded files can be accessed
|
|
|
|
_weburi="https://ptrace.org"
|
2024-12-23 18:49:14 +01:00
|
|
|
|
2024-12-23 16:36:55 +01:00
|
|
|
# ssh user@host
|
|
|
|
_sshhost="sdk@codevoid.de"
|
|
|
|
|
2024-12-23 18:49:14 +01:00
|
|
|
# permissions (for the fixl command)
|
|
|
|
_chown="sdk:www"
|
|
|
|
_chmod="u+rw,g+r"
|
|
|
|
|
2024-12-23 18:21:09 +01:00
|
|
|
###
|
|
|
|
### SET ARGUMENT SWITCHES
|
|
|
|
###
|
|
|
|
|
2024-12-23 14:32:10 +01:00
|
|
|
for arg in "$@"
|
|
|
|
do
|
|
|
|
case "$arg" in
|
2024-12-23 18:21:09 +01:00
|
|
|
# extensions
|
2024-12-23 14:32:10 +01:00
|
|
|
sh) _ext=".sh" ;;
|
|
|
|
ksh) _ext=".ksh" ;;
|
|
|
|
txt) _ext=".txt" ;;
|
|
|
|
log) _ext=".log" ;;
|
2024-12-23 18:21:09 +01:00
|
|
|
# archive mode
|
2024-12-23 16:36:55 +01:00
|
|
|
gz) _gz=1 ;;
|
|
|
|
tgz) _tgz=1 ;;
|
2024-12-23 18:21:09 +01:00
|
|
|
# commands
|
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" ;;
|
2024-12-23 18:21:09 +01:00
|
|
|
fixl) _fixperm=1 ;;
|
2024-12-23 16:36:55 +01:00
|
|
|
ren|mv) _mv=1 ;;
|
|
|
|
renl|mvl) _mvlast=1 ;;
|
2024-12-23 18:21:09 +01:00
|
|
|
-h) ;; # swallow
|
2024-12-23 14:32:10 +01:00
|
|
|
*) break; ;;
|
|
|
|
esac
|
|
|
|
shift;
|
|
|
|
done
|
|
|
|
|
2024-12-23 18:21:09 +01:00
|
|
|
###
|
|
|
|
### FUNCTIONS
|
2024-12-23 21:31:59 +01:00
|
|
|
###
|
2024-12-23 18:21:09 +01:00
|
|
|
|
|
|
|
# fetches remote file list
|
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 18:21:09 +01:00
|
|
|
# formats file list output
|
|
|
|
remote_list_print() {
|
|
|
|
while read f
|
|
|
|
do
|
|
|
|
echo "$_weburi/$(echo "$f" \
|
|
|
|
| sed 's/ /%20/g') ($f)"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2024-12-23 17:32:40 +01:00
|
|
|
copy_and_print() {
|
|
|
|
echo "$_weburi/$(basename "$1")" \
|
|
|
|
| sed 's/ /%20/g' \
|
|
|
|
| xclip -f -r
|
|
|
|
echo
|
|
|
|
}
|
|
|
|
|
2024-12-23 18:21:09 +01:00
|
|
|
###
|
|
|
|
### MAIN PROGRAM
|
|
|
|
###
|
|
|
|
|
2024-12-23 21:31:59 +01:00
|
|
|
# HANDLE CASE: "standard input"
|
|
|
|
# upload with provided or generated filename
|
2024-12-23 17:32:40 +01:00
|
|
|
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 18:21:09 +01:00
|
|
|
# HANDLE CASE: "list all remote files"
|
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 18:21:09 +01:00
|
|
|
| remote_list_print
|
2024-12-23 15:26:32 +01:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2024-12-23 18:21:09 +01:00
|
|
|
# HANDLE CASE: "list last N files"
|
2024-12-23 15:26:32 +01:00
|
|
|
if [ -n "$_last" ]
|
|
|
|
then
|
2024-12-23 16:36:55 +01:00
|
|
|
remote_list \
|
2024-12-23 15:26:32 +01:00
|
|
|
| tail $_last \
|
2024-12-23 18:21:09 +01:00
|
|
|
| remote_list_print
|
2024-12-23 15:26:32 +01:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2024-12-23 18:21:09 +01:00
|
|
|
# HANDLE CASE: rename remote file
|
2024-12-23 15:26:32 +01:00
|
|
|
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\""
|
2024-12-23 18:34:22 +01:00
|
|
|
echo "$2" | remote_list_print
|
2024-12-23 15:26:32 +01:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2024-12-23 18:21:09 +01:00
|
|
|
# HANDLE CASE: rename the last uploaded remote file
|
2024-12-23 15:26:32 +01:00
|
|
|
if [ -n "$_mvlast" ]
|
|
|
|
then
|
2024-12-23 16:36:55 +01:00
|
|
|
lastfile="$(remote_list | tail -1)"
|
|
|
|
ssh $_sshhost \
|
|
|
|
"cd $_rpath/ \
|
2024-12-23 18:34:22 +01:00
|
|
|
&& mv -v \"$lastfile\" \"$1\""
|
|
|
|
echo "$1" | remote_list_print
|
2024-12-23 15:26:32 +01:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2024-12-23 18:21:09 +01:00
|
|
|
# HANDLE CASE: delete remote file
|
2024-12-23 15:26:32 +01:00
|
|
|
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 18:21:09 +01:00
|
|
|
# HANDLE CASE: delete last uploaded remote file
|
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)"
|
2024-12-23 18:21:09 +01:00
|
|
|
echo "rm $lastfile"
|
2024-12-23 16:36:55 +01:00
|
|
|
ssh $_sshhost \
|
|
|
|
"cd $_rpath/ \
|
2024-12-23 18:21:09 +01:00
|
|
|
&& rm -f \"$lastfile\"" \
|
2024-12-23 15:26:32 +01:00
|
|
|
|| true
|
2024-12-23 14:32:10 +01:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2024-12-23 18:21:09 +01:00
|
|
|
# HANDLE CASE: fix permission of last file
|
|
|
|
if [ -n "$_fixperm" ]
|
|
|
|
then
|
|
|
|
lastfile="$(remote_list | tail -1)"
|
|
|
|
echo "Fixing: $lastfile"
|
2024-12-23 18:49:14 +01:00
|
|
|
ssh $_sshhost "cd $_rpath/; \
|
|
|
|
chown $_chown \"$lastfile\"; \
|
|
|
|
chmod $_chmod \"$lastfile\"; \
|
2024-12-23 18:21:09 +01:00
|
|
|
ls -lh \"$lastfile\""
|
|
|
|
exit 0
|
|
|
|
fi
|
2024-12-23 14:32:10 +01:00
|
|
|
|
2024-12-23 18:21:09 +01:00
|
|
|
# HANDLE CASE: upload of files and folders
|
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
|
2024-12-23 18:21:09 +01:00
|
|
|
# are reset when we leave the scope. Note, that this requires
|
|
|
|
# us to "return", instead of "continue" skip to the next loop
|
|
|
|
# iteration
|
2024-12-23 17:32:40 +01:00
|
|
|
(
|
|
|
|
[ ! -e "$item" ] \
|
|
|
|
&& echo "File or directory not found: $item" \
|
2024-12-23 18:21:09 +01:00
|
|
|
&& return
|
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 18:34:22 +01:00
|
|
|
# CASE "file" "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
|
2024-12-23 17:32:40 +01:00
|
|
|
echo "Uploading $item as $file"
|
2024-12-23 18:34:22 +01:00
|
|
|
scp -q "$item" $_sshhost:"$_rpath/$file"
|
2024-12-23 17:32:40 +01:00
|
|
|
copy_and_print "$file"
|
|
|
|
return
|
2024-12-23 16:36:55 +01:00
|
|
|
fi
|
|
|
|
|
2024-12-23 18:21:09 +01:00
|
|
|
# CASE "folder"
|
2024-12-23 17:32:40 +01:00
|
|
|
[ -d "$item" ] && _tgz=1
|
2024-12-23 16:36:55 +01:00
|
|
|
|
2024-12-23 18:21:09 +01:00
|
|
|
# CASE "tgz archive mode"
|
2024-12-23 16:36:55 +01:00
|
|
|
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")" \
|
2024-12-23 18:34:22 +01:00
|
|
|
| ssh $_sshhost "cat - > \"$_rpath/$file\""
|
2024-12-23 17:32:40 +01:00
|
|
|
copy_and_print "$file"
|
|
|
|
return
|
2024-12-23 16:36:55 +01:00
|
|
|
fi
|
|
|
|
|
2024-12-23 18:21:09 +01:00
|
|
|
# CASE "gz archive 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 \
|
2024-12-23 18:34:22 +01:00
|
|
|
| ssh $_sshhost "cat - > \"$_rpath/$file\""
|
2024-12-23 17:32:40 +01:00
|
|
|
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
|
|
|
|
2024-12-23 18:21:09 +01:00
|
|
|
# USAGE
|
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 " Commands:"
|
2024-12-23 18:21:09 +01:00
|
|
|
echo " rm <files> - remove files"
|
|
|
|
echo " rml - remove last upload"
|
|
|
|
echo " ls - list files"
|
|
|
|
echo " l|last - list last file"
|
|
|
|
echo " -0..N - list N last files"
|
|
|
|
echo " mv|ren <oldname> <newname> - rename file"
|
|
|
|
echo " mvl|renl <newname> - rename last uploaded file"
|
2024-12-23 18:49:14 +01:00
|
|
|
echo " fixl - fix permission of last uploaded file"
|
2024-12-23 17:32:40 +01:00
|
|
|
echo
|
|
|
|
echo " Extensions:"
|
2024-12-23 18:21:09 +01:00
|
|
|
echo " sh, ksh, txt, log - add extension to file"
|
|
|
|
echo " gz, tgz - uploads file as archive (tgz is always used for folders)"
|
2024-12-23 17:32:40 +01:00
|
|
|
echo
|
2024-12-23 18:49:14 +01:00
|
|
|
echo " <files> - files or directories to upload (needs to be the last argument)"
|
|
|
|
echo
|
2024-12-23 17:32:40 +01:00
|
|
|
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
|