63 lines
1.7 KiB
Bash
Executable File
63 lines
1.7 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
# This script enables users to easily upload files via a SSH connection and creates a link to
|
|
# download them.
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-only
|
|
# SPDX-FileCopyrightText: 2019 Max Mehl <https://mehl.mx>
|
|
|
|
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> [<subdirectory>]"
|
|
exit 1
|
|
fi
|
|
|
|
# Transform to full path
|
|
# file.pdf or /path/to/file.pdf
|
|
FILE="$1"
|
|
# -> file.pdf
|
|
FILE_NAME="$(basename "${FILE}")"
|
|
# Get subdirectory if given
|
|
if [ "$2" != "" ]; then
|
|
SSH_PATH="${SSH_PATH}/${2}"
|
|
URL="${URL}/${2}"
|
|
elif [ "$SUBDIR_DEFAULT" != "" ]; then
|
|
SSH_PATH="${SSH_PATH}/${SUBDIR_DEFAULT}"
|
|
URL="${URL}/${SUBDIR_DEFAULT}"
|
|
fi
|
|
|
|
# Check whether file actually exists locally
|
|
if [ ! -e "${FILE}" ]; then
|
|
echo "File ${FILE} does not exist locally."
|
|
exit 1
|
|
fi
|
|
|
|
# constuct file name, and replace spaces by underscores
|
|
DEST="${FILE_NAME// /_}"
|
|
# Check if file is existent remotely
|
|
cmd="test -e \"${SSH_PATH}\"/\"${DEST}\""
|
|
# Check if file does already exist with this name
|
|
if ssh -q "${SSH_HOST}" "${cmd}"; then
|
|
echo "File ${DEST} already exists on the host. Please rename the file or choose a different name."
|
|
exit 1
|
|
fi
|
|
|
|
# Upload file
|
|
if ! scp "$FILE" "${SSH_HOST}":"${SSH_PATH}"/"${DEST}"; then
|
|
echo "Error uploading file. Please check your connection and configuration."
|
|
exit 1
|
|
fi
|
|
|
|
# Output download link
|
|
echo "File has been uploaded. Download link:"
|
|
echo "$URL/$DEST"
|
|
|
|
exit 0
|