Update 2024-12-23 18:21 OpenBSD/amd64-t14
This commit is contained in:
parent
ac58f6baa9
commit
1bb57985ca
104
.bin/upload
104
.bin/upload
@ -3,6 +3,10 @@
|
||||
# manage files in a webroot
|
||||
# needs: sh (cp,rm), openssh (ssh,scp), gzip, tar, sed, tail, xclip
|
||||
|
||||
###
|
||||
### CONFIGURATION
|
||||
###
|
||||
|
||||
# remote patch uploaded files should go
|
||||
_rpath="/home/www/htdocs/ptrace/paste"
|
||||
# web url where the uploaded files can be accessed
|
||||
@ -10,33 +14,56 @@ _weburi="https://ptrace.org"
|
||||
# ssh user@host
|
||||
_sshhost="sdk@codevoid.de"
|
||||
|
||||
###
|
||||
### SET ARGUMENT SWITCHES
|
||||
###
|
||||
|
||||
for arg in "$@"
|
||||
do
|
||||
case "$arg" in
|
||||
# extensions
|
||||
sh) _ext=".sh" ;;
|
||||
ksh) _ext=".ksh" ;;
|
||||
txt) _ext=".txt" ;;
|
||||
log) _ext=".log" ;;
|
||||
# archive mode
|
||||
gz) _gz=1 ;;
|
||||
tgz) _tgz=1 ;;
|
||||
# commands
|
||||
rm) _rm=1 ;;
|
||||
rml) _rmlast=1 ;;
|
||||
ls) _ls=1 ;;
|
||||
l|last) _last="-1" ;;
|
||||
-[0-9]*) _last="$arg" ;;
|
||||
fixl) _fixperm=1 ;;
|
||||
ren|mv) _mv=1 ;;
|
||||
renl|mvl) _mvlast=1 ;;
|
||||
-h) ;; # swallow
|
||||
*) break; ;;
|
||||
esac
|
||||
shift;
|
||||
done
|
||||
|
||||
###
|
||||
### FUNCTIONS
|
||||
###
|
||||
|
||||
# fetches remote file list
|
||||
remote_list() {
|
||||
ssh $_sshhost \
|
||||
"cd $_rpath/ \
|
||||
&& ls -1tr"
|
||||
}
|
||||
|
||||
# formats file list output
|
||||
remote_list_print() {
|
||||
while read f
|
||||
do
|
||||
echo "$_weburi/$(echo "$f" \
|
||||
| sed 's/ /%20/g') ($f)"
|
||||
done
|
||||
}
|
||||
|
||||
copy_and_print() {
|
||||
echo "$_weburi/$(basename "$1")" \
|
||||
| sed 's/ /%20/g' \
|
||||
@ -44,7 +71,12 @@ copy_and_print() {
|
||||
echo
|
||||
}
|
||||
|
||||
# handle stdin
|
||||
###
|
||||
### MAIN PROGRAM
|
||||
###
|
||||
|
||||
# HANDLE CASE: "STDIN"
|
||||
# do: upload with provided or generated filename
|
||||
if [[ ! -t 0 ]]
|
||||
then
|
||||
fname="$1"
|
||||
@ -55,27 +87,24 @@ then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# HANDLE CASE: "list all remote files"
|
||||
if [ -n "$_ls" ]
|
||||
then
|
||||
remote_list \
|
||||
| while read f
|
||||
do
|
||||
echo "$_weburi/$(echo "$f" | sed 's/ /%20/g') ($f)"
|
||||
done
|
||||
| remote_list_print
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# HANDLE CASE: "list last N files"
|
||||
if [ -n "$_last" ]
|
||||
then
|
||||
remote_list \
|
||||
| tail $_last \
|
||||
| while read f
|
||||
do
|
||||
echo "$_weburi/$(echo "$f" | sed 's/ /%20/g') ($f)"
|
||||
done
|
||||
| remote_list_print
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# HANDLE CASE: rename remote file
|
||||
if [ -n "$_mv" ]
|
||||
then
|
||||
ssh $_sshhost \
|
||||
@ -84,6 +113,7 @@ then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# HANDLE CASE: rename the last uploaded remote file
|
||||
if [ -n "$_mvlast" ]
|
||||
then
|
||||
lastfile="$(remote_list | tail -1)"
|
||||
@ -93,6 +123,7 @@ then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# HANDLE CASE: delete remote file
|
||||
if [ -n "$_rm" ]
|
||||
then
|
||||
for file in "$@"
|
||||
@ -105,50 +136,59 @@ then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# HANDLE CASE: delete last uploaded remote file
|
||||
if [ -n "$_rmlast" ]
|
||||
then
|
||||
lastfile="$(remote_list | tail -1)"
|
||||
echo "rm $lastfile"
|
||||
ssh $_sshhost \
|
||||
"cd $_rpath/ \
|
||||
&& rm -v \"$lastfile\"" \
|
||||
&& rm -f \"$lastfile\"" \
|
||||
|| true
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# HANDLE CASE: fix permission of last file
|
||||
if [ -n "$_fixperm" ]
|
||||
then
|
||||
lastfile="$(remote_list | tail -1)"
|
||||
echo "Fixing: $lastfile"
|
||||
ssh $_sshhost "cd $_rpath/ \
|
||||
&& chmod u+rw,g+r \"$lastfile\"; \
|
||||
ls -lh \"$lastfile\""
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# HANDLE CASE: upload of files and folders
|
||||
if [ $# -gt 0 ]
|
||||
then
|
||||
for item in "$@"
|
||||
do
|
||||
# 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
|
||||
# are reset when we leave the scope. Note, that this requires
|
||||
# us to "return", instead of "continue" skip to the next loop
|
||||
# iteration
|
||||
(
|
||||
|
||||
[ ! -e "$item" ] \
|
||||
&& echo "File or directory not found: $item" \
|
||||
&& exit 1
|
||||
&& return
|
||||
|
||||
# target filename
|
||||
file="$(basename "$item")$_ext"
|
||||
|
||||
# handle file (plain), no archive mode
|
||||
# CASE "file" "no archive"
|
||||
if [ -f "$item" ] && [ -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 "$item"
|
||||
echo "Uploading $item as $file"
|
||||
scp -q "$item" "$_sshhost:\"$_rpath/$file\""
|
||||
copy_and_print "$file"
|
||||
return
|
||||
fi
|
||||
|
||||
# directories can only be handled as tgz archive
|
||||
# CASE "folder"
|
||||
[ -d "$item" ] && _tgz=1
|
||||
|
||||
# handle file or directory (tgz mode)
|
||||
# CASE "tgz archive mode"
|
||||
if [ -n "$_tgz" ]
|
||||
then
|
||||
file="$file.tgz"
|
||||
@ -161,7 +201,7 @@ then
|
||||
return
|
||||
fi
|
||||
|
||||
# handle file (gz mode)
|
||||
# CASE "gz archive mode"
|
||||
if [ -f "$item" ] && [ -n "$_gz" ]
|
||||
then
|
||||
file="$file.gz"
|
||||
@ -179,24 +219,28 @@ then
|
||||
done
|
||||
fi
|
||||
|
||||
# USAGE
|
||||
if [ $# -eq 0 ]
|
||||
then
|
||||
echo "usage: upload [command] [ext] [<files>]"
|
||||
echo " upload [filename] < file"
|
||||
echo " cat file | upload [filename]"
|
||||
echo
|
||||
echo " <files> - files or directories to upload"
|
||||
echo " <files> - files or directories to upload"
|
||||
echo
|
||||
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 " 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"
|
||||
echo " fixl - fix permission on last uploaded file"
|
||||
echo
|
||||
echo " Extensions:"
|
||||
echo " sh, ksh, txt, log - add extension to file"
|
||||
echo " sh, ksh, txt, log - add extension to file"
|
||||
echo " gz, tgz - uploads file as archive (tgz is always used for folders)"
|
||||
echo
|
||||
echo "Special Case:"
|
||||
echo " When data is piped to the script it expects only one"
|
||||
|
Loading…
Reference in New Issue
Block a user