feat: allow to provide subdirectory for upload, simplify
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
SSH_HOST=user@host.tld
|
SSH_HOST=user@host.tld
|
||||||
# Absolute path where files shall be stored
|
# Absolute path where files shall be stored
|
||||||
SSH_PATH=/home/user/uploads/share
|
SSH_PATH=/home/user/uploads/share
|
||||||
|
SUBDIR_DEFAULT=dl
|
||||||
|
|
||||||
# URL to this directory
|
# URL to this directory
|
||||||
URL=http://uploads.host.tld/share
|
URL=http://uploads.host.tld/share
|
||||||
|
|||||||
85
uas.sh
85
uas.sh
@@ -1,28 +1,10 @@
|
|||||||
#!/bin/bash
|
#!/usr/bin/env 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.
|
|
||||||
#
|
|
||||||
########################################################################
|
|
||||||
|
|
||||||
|
# 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")
|
BASENAME=$(basename "$0")
|
||||||
|
|
||||||
@@ -33,8 +15,8 @@ if [ ! -e "$BINDIR"/config.cfg ]; then echo "Missing config.cfg file. Edit and r
|
|||||||
source "$BINDIR"/config.cfg
|
source "$BINDIR"/config.cfg
|
||||||
|
|
||||||
if [ "$1" = "" ]; then
|
if [ "$1" = "" ]; then
|
||||||
echo "Usage: $BASENAME <file-to-upload>"
|
echo "Usage: $BASENAME <file-to-upload> [<subdirectory>]"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Transform to full path
|
# Transform to full path
|
||||||
@@ -42,10 +24,14 @@ fi
|
|||||||
FILE="$1"
|
FILE="$1"
|
||||||
# -> file.pdf
|
# -> file.pdf
|
||||||
FILE_NAME="$(basename "${FILE}")"
|
FILE_NAME="$(basename "${FILE}")"
|
||||||
# -> file
|
# Get subdirectory if given
|
||||||
FILE_PREFIX="${FILE_NAME%.*}"
|
if [ "$2" != "" ]; then
|
||||||
# .pdf
|
SSH_PATH="${SSH_PATH}/${2}"
|
||||||
FILE_SUFFIX="$([[ "$FILE_NAME" = *.* ]] && echo ".${FILE_NAME##*.}" || echo '')"
|
URL="${URL}/${2}"
|
||||||
|
elif [ "$SUBDIR_DEFAULT" != "" ]; then
|
||||||
|
SSH_PATH="${SSH_PATH}/${SUBDIR_DEFAULT}"
|
||||||
|
URL="${URL}/${SUBDIR_DEFAULT}"
|
||||||
|
fi
|
||||||
|
|
||||||
# Check whether file actually exists locally
|
# Check whether file actually exists locally
|
||||||
if [ ! -e "${FILE}" ]; then
|
if [ ! -e "${FILE}" ]; then
|
||||||
@@ -53,34 +39,21 @@ if [ ! -e "${FILE}" ]; then
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# If the file already exists on the host, append a number and iterate if necessary
|
# constuct file name, and replace spaces by underscores
|
||||||
i=0
|
DEST="${FILE_NAME// /_}"
|
||||||
FILE_PREFIX_tmp="${FILE_PREFIX}"
|
# Check if file is existent remotely
|
||||||
while [ "$ok" != "y" ]; do
|
cmd="test -e \"${SSH_PATH}\"/\"${DEST}\""
|
||||||
# constuct file name, and replace spaces by underscores
|
# Check if file does already exist with this name
|
||||||
DEST="$(echo "$FILE_PREFIX_tmp$FILE_SUFFIX" | sed 's/ /_/g')"
|
if ssh -q "${SSH_HOST}" "${cmd}"; then
|
||||||
# Check if file is existent remotely
|
echo "File ${DEST} already exists on the host. Please rename the file or choose a different name."
|
||||||
cmd="test -e \"${SSH_PATH}\"/\"${DEST}\""
|
exit 1
|
||||||
# If file does already exist with this name
|
fi
|
||||||
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
|
# Upload file
|
||||||
scp "$FILE" "${SSH_HOST}":"${SSH_PATH}"/"${DEST}"
|
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
|
# Output download link
|
||||||
echo "File has been uploaded. Download link:"
|
echo "File has been uploaded. Download link:"
|
||||||
|
|||||||
Reference in New Issue
Block a user