This script enables users to easily upload files via a SSH connection and creates a link to download them
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.8 KiB

#!/bin/bash
########################################################################
# Copyright (C) 2019 Max Mehl <mail@mehl.mx>
########################################################################
#
# 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.
#
########################################################################
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
# file.pdf or /path/to/file.pdf
FILE="$1"
# -> file.pdf
FILE_NAME="$(basename "${FILE}")"
# -> file
FILE_PREFIX="${FILE_NAME%.*}"
# .pdf
FILE_SUFFIX="$([[ "$FILE_NAME" = *.* ]] && echo ".${FILE_NAME##*.}" || echo '')"
# Check whether file actually exists locally
if [ ! -e "${FILE}" ]; then
echo "File ${FILE} does not exist locally."
exit 1
fi
# If the file already exists on the host, append a number and iterate if necessary
i=0
FILE_PREFIX_tmp="${FILE_PREFIX}"
while [ "$ok" != "y" ]; do
# 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
((i++))
FILE_PREFIX_tmp="$FILE_PREFIX"-${i}
ok=n
# If file does not exist yet
else
# 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
ok=y
fi
done
# Upload file
scp "$FILE" "${SSH_HOST}":"${SSH_PATH}"/"${DEST}"
# Output download link
echo "File has been uploaded. Download link:"
echo "$URL/$DEST"
exit 0