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 or HostName
|
||||||
SSH=user@host.tld:\~/uploads/share
|
SSH_HOST=user@host.tld
|
||||||
|
# Absolute path where files shall be stored
|
||||||
|
SSH_PATH=/home/user/uploads/share
|
||||||
|
|
||||||
# URL to this directory
|
# URL to this directory
|
||||||
URL=http://uploads.host.tld/share
|
URL=http://uploads.host.tld/share
|
||||||
|
|||||||
42
uas.sh
42
uas.sh
@@ -1,6 +1,6 @@
|
|||||||
#!/bin/bash
|
#!/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
|
# This program is free software: you can redistribute it and/or modify
|
||||||
@@ -26,32 +26,46 @@
|
|||||||
|
|
||||||
BASENAME=$(basename $0)
|
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
|
if [ "$1" = "" ]; then
|
||||||
echo "Usage: $BASENAME <file-to-upload>"
|
echo "Usage: $BASENAME <file-to-upload>"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Transform to full path
|
# Transform to full path
|
||||||
PWD=$(pwd)
|
|
||||||
FILE="$1"
|
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
|
if [ ! -e "$FILE" ]; then
|
||||||
echo "File $FILE does not exist."
|
echo "File $FILE does not exist locally."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Jump to dir of where the executable relies to load config
|
i=1
|
||||||
BINDIR="$(dirname "$(readlink -f "$0")")"
|
FILE_PREFIX_tmp="$FILE_PREFIX"
|
||||||
# Test if config.cfg exists and set needed variables
|
while [ "$ok" != "y" ]; do
|
||||||
if [ ! -e "$BINDIR"/config.cfg ]; then echo "Missing config.cfg file. Edit and rename config.cfg.sample"; exit 1; fi
|
DEST=$(echo "$FILE_PREFIX_tmp$FILE_SUFFIX" | sed 's/ /_/g')
|
||||||
source "$BINDIR"/config.cfg
|
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
|
||||||
|
|
||||||
|
scp "$FILE" ${SSH_HOST}:${SSH_PATH}/"$DEST"
|
||||||
# Replace spaces by underscores for better downloading
|
|
||||||
DEST=$(echo "$FILENAME" | sed 's/ /_/g')
|
|
||||||
|
|
||||||
scp "$FILE" "$SSH"/"$DEST"
|
|
||||||
|
|
||||||
# Output download link
|
# Output download link
|
||||||
echo "File has been uploaded. Download link:"
|
echo "File has been uploaded. Download link:"
|
||||||
|
|||||||
Reference in New Issue
Block a user