2015-07-07 15:26:41 +02:00
|
|
|
#!/bin/bash
|
|
|
|
########################################################################
|
2019-03-12 15:10:30 +01:00
|
|
|
# Copyright (C) 2019 Max Mehl <mail@mehl.mx>
|
2015-07-07 15:26:41 +02:00
|
|
|
########################################################################
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
########################################################################
|
|
|
|
#
|
|
|
|
# This script enables users to easily upload files via a SSH connection
|
|
|
|
# and creates a link to download them.
|
|
|
|
#
|
|
|
|
########################################################################
|
|
|
|
|
|
|
|
|
2019-03-12 15:10:30 +01:00
|
|
|
BASENAME=$(basename "$0")
|
2015-07-07 15:26:41 +02:00
|
|
|
|
2018-01-05 09:23:33 +01:00
|
|
|
# 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
|
|
|
|
|
2015-07-07 15:26:41 +02:00
|
|
|
if [ "$1" = "" ]; then
|
|
|
|
echo "Usage: $BASENAME <file-to-upload>"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Transform to full path
|
2019-03-12 15:10:30 +01:00
|
|
|
# file.pdf or /path/to/file.pdf
|
2015-07-07 15:26:41 +02:00
|
|
|
FILE="$1"
|
2019-03-12 15:10:30 +01:00
|
|
|
# -> file.pdf
|
|
|
|
FILE_NAME="$(basename "${FILE}")"
|
|
|
|
# -> file
|
|
|
|
FILE_PREFIX="${FILE_NAME%.*}"
|
|
|
|
# .pdf
|
|
|
|
FILE_SUFFIX="$([[ "$FILE_NAME" = *.* ]] && echo ".${FILE_NAME##*.}" || echo '')"
|
2015-07-07 15:26:41 +02:00
|
|
|
|
2019-03-12 15:10:30 +01:00
|
|
|
# Check whether file actually exists locally
|
|
|
|
if [ ! -e "${FILE}" ]; then
|
|
|
|
echo "File ${FILE} does not exist locally."
|
2015-07-07 15:26:41 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2019-03-12 15:10:30 +01:00
|
|
|
# If the file already exists on the host, append a number and iterate if necessary
|
|
|
|
i=0
|
|
|
|
FILE_PREFIX_tmp="${FILE_PREFIX}"
|
2018-01-05 09:23:33 +01:00
|
|
|
while [ "$ok" != "y" ]; do
|
2019-03-12 15:10:30 +01:00
|
|
|
# constuct file name, and replace spaces by underscores
|
|
|
|
DEST="$(echo "$FILE_PREFIX_tmp$FILE_SUFFIX" | sed 's/ /_/g')"
|
|
|
|
# Check if file is existent remotely
|
|
|
|
cmd="test -e \"${SSH_PATH}\"/\"${DEST}\""
|
|
|
|
# If file does already exist with this name
|
|
|
|
if ssh -q "${SSH_HOST}" "${cmd}"; then
|
|
|
|
# iterate number and append -<number> to file name in front of suffix
|
2018-01-05 09:23:33 +01:00
|
|
|
((i++))
|
2019-03-12 15:10:30 +01:00
|
|
|
FILE_PREFIX_tmp="$FILE_PREFIX"-${i}
|
2018-01-05 09:23:33 +01:00
|
|
|
ok=n
|
2019-03-12 15:10:30 +01:00
|
|
|
# If file does not exist yet
|
2018-01-05 09:23:33 +01:00
|
|
|
else
|
2019-03-12 15:10:30 +01:00
|
|
|
# Only output this info if a rename took place
|
|
|
|
if [[ $i -gt 0 ]]; then
|
|
|
|
echo "A file with an identical name already exists in the remote directory."
|
|
|
|
echo "Your file has been renamed to prevent overwriting."
|
|
|
|
echo
|
|
|
|
fi
|
2018-01-05 09:23:33 +01:00
|
|
|
ok=y
|
|
|
|
fi
|
|
|
|
done
|
2015-07-07 15:26:41 +02:00
|
|
|
|
2019-03-12 15:10:30 +01:00
|
|
|
# Upload file
|
|
|
|
scp "$FILE" "${SSH_HOST}":"${SSH_PATH}"/"${DEST}"
|
2015-07-07 15:26:41 +02:00
|
|
|
|
|
|
|
# Output download link
|
|
|
|
echo "File has been uploaded. Download link:"
|
|
|
|
echo "$URL/$DEST"
|
|
|
|
|
|
|
|
exit 0
|