feat: allow to provide subdirectory for upload, simplify

This commit is contained in:
2026-04-01 09:49:43 +02:00
parent 1786ca62ad
commit 6c71864186
2 changed files with 30 additions and 56 deletions

View File

@@ -2,6 +2,7 @@
SSH_HOST=user@host.tld
# Absolute path where files shall be stored
SSH_PATH=/home/user/uploads/share
SUBDIR_DEFAULT=dl
# URL to this directory
URL=http://uploads.host.tld/share

85
uas.sh
View File

@@ -1,28 +1,10 @@
#!/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.
#
########################################################################
#!/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")
@@ -33,8 +15,8 @@ if [ ! -e "$BINDIR"/config.cfg ]; then echo "Missing config.cfg file. Edit and r
source "$BINDIR"/config.cfg
if [ "$1" = "" ]; then
echo "Usage: $BASENAME <file-to-upload>"
exit 1
echo "Usage: $BASENAME <file-to-upload> [<subdirectory>]"
exit 1
fi
# Transform to full path
@@ -42,10 +24,14 @@ fi
FILE="$1"
# -> file.pdf
FILE_NAME="$(basename "${FILE}")"
# -> file
FILE_PREFIX="${FILE_NAME%.*}"
# .pdf
FILE_SUFFIX="$([[ "$FILE_NAME" = *.* ]] && echo ".${FILE_NAME##*.}" || echo '')"
# 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
@@ -53,34 +39,21 @@ if [ ! -e "${FILE}" ]; then
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
# 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
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
echo "File has been uploaded. Download link:"