From 6c718641867170ad499d874e6baeeb513638db2d Mon Sep 17 00:00:00 2001 From: Max Mehl Date: Wed, 1 Apr 2026 09:49:43 +0200 Subject: [PATCH] feat: allow to provide subdirectory for upload, simplify --- config.cfg.sample | 1 + uas.sh | 85 ++++++++++++++++------------------------------- 2 files changed, 30 insertions(+), 56 deletions(-) diff --git a/config.cfg.sample b/config.cfg.sample index 2dbe439..b605dbc 100644 --- a/config.cfg.sample +++ b/config.cfg.sample @@ -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 diff --git a/uas.sh b/uas.sh index a2d6ad0..237b8b7 100755 --- a/uas.sh +++ b/uas.sh @@ -1,28 +1,10 @@ -#!/bin/bash -######################################################################## -# Copyright (C) 2019 Max Mehl -######################################################################## -# -# 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 . -# -######################################################################## -# -# 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 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 " - exit 1 + echo "Usage: $BASENAME []" + 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 - 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:"