check if file already exists remotely, and rename it accordingly to prevent overwriting
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
# SSH host and directory to upload file
|
||||
SSH=user@host.tld:\~/uploads/share
|
||||
# SSH user@host or HostName
|
||||
SSH_HOST=user@host.tld
|
||||
# Absolute path where files shall be stored
|
||||
SSH_PATH=/home/user/uploads/share
|
||||
|
||||
# URL to this directory
|
||||
URL=http://uploads.host.tld/share
|
||||
|
||||
42
uas.sh
42
uas.sh
@@ -1,6 +1,6 @@
|
||||
#!/bin/bash
|
||||
########################################################################
|
||||
# Copyright (C) 2015 Max Mehl <mail@mehl.mx>
|
||||
# Copyright (C) 2018 Max Mehl <mail@mehl.mx>
|
||||
########################################################################
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
@@ -26,32 +26,46 @@
|
||||
|
||||
BASENAME=$(basename $0)
|
||||
|
||||
# Load config
|
||||
BINDIR="$(dirname "$(readlink -f "$0")")"
|
||||
# Test if config.cfg exists and set needed variables
|
||||
if [ ! -e "$BINDIR"/config.cfg ]; then echo "Missing config.cfg file. Edit and rename config.cfg.sample"; exit 1; fi
|
||||
source "$BINDIR"/config.cfg
|
||||
|
||||
if [ "$1" = "" ]; then
|
||||
echo "Usage: $BASENAME <file-to-upload>"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Transform to full path
|
||||
PWD=$(pwd)
|
||||
FILE="$1"
|
||||
FILENAME=$(basename $FILE)
|
||||
FILE_NAME=$(basename $FILE)
|
||||
FILE_PREFIX=${FILE_NAME%.*}
|
||||
FILE_SUFFIX=$([[ "$FILE_NAME" = *.* ]] && echo ".${FILE_NAME##*.}" || echo '')
|
||||
|
||||
if [ ! -e "$FILE" ]; then
|
||||
echo "File $FILE does not exist."
|
||||
echo "File $FILE does not exist locally."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Jump to dir of where the executable relies to load config
|
||||
BINDIR="$(dirname "$(readlink -f "$0")")"
|
||||
# Test if config.cfg exists and set needed variables
|
||||
if [ ! -e "$BINDIR"/config.cfg ]; then echo "Missing config.cfg file. Edit and rename config.cfg.sample"; exit 1; fi
|
||||
source "$BINDIR"/config.cfg
|
||||
i=1
|
||||
FILE_PREFIX_tmp="$FILE_PREFIX"
|
||||
while [ "$ok" != "y" ]; do
|
||||
DEST=$(echo "$FILE_PREFIX_tmp$FILE_SUFFIX" | sed 's/ /_/g')
|
||||
ssh ${SSH_HOST} "test -e ${SSH_PATH}/$DEST"
|
||||
if [ $? == 0 ]; then
|
||||
FILE_PREFIX_tmp="$FILE_PREFIX"-${i}
|
||||
((i++))
|
||||
ok=n
|
||||
else
|
||||
echo "A file with an identical name already exists in the remote directory."
|
||||
echo "Your file will be renamed to prevent overwriting."
|
||||
echo
|
||||
ok=y
|
||||
fi
|
||||
done
|
||||
|
||||
|
||||
# Replace spaces by underscores for better downloading
|
||||
DEST=$(echo "$FILENAME" | sed 's/ /_/g')
|
||||
|
||||
scp "$FILE" "$SSH"/"$DEST"
|
||||
scp "$FILE" ${SSH_HOST}:${SSH_PATH}/"$DEST"
|
||||
|
||||
# Output download link
|
||||
echo "File has been uploaded. Download link:"
|
||||
|
||||
Reference in New Issue
Block a user