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.

uas.sh 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/bin/bash
  2. ########################################################################
  3. # Copyright (C) 2019 Max Mehl <mail@mehl.mx>
  4. ########################################################################
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. ########################################################################
  20. #
  21. # This script enables users to easily upload files via a SSH connection
  22. # and creates a link to download them.
  23. #
  24. ########################################################################
  25. BASENAME=$(basename "$0")
  26. # Load config
  27. BINDIR="$(dirname "$(readlink -f "$0")")"
  28. # Test if config.cfg exists and set needed variables
  29. if [ ! -e "$BINDIR"/config.cfg ]; then echo "Missing config.cfg file. Edit and rename config.cfg.sample"; exit 1; fi
  30. source "$BINDIR"/config.cfg
  31. if [ "$1" = "" ]; then
  32. echo "Usage: $BASENAME <file-to-upload>"
  33. exit 1
  34. fi
  35. # Transform to full path
  36. # file.pdf or /path/to/file.pdf
  37. FILE="$1"
  38. # -> file.pdf
  39. FILE_NAME="$(basename "${FILE}")"
  40. # -> file
  41. FILE_PREFIX="${FILE_NAME%.*}"
  42. # .pdf
  43. FILE_SUFFIX="$([[ "$FILE_NAME" = *.* ]] && echo ".${FILE_NAME##*.}" || echo '')"
  44. # Check whether file actually exists locally
  45. if [ ! -e "${FILE}" ]; then
  46. echo "File ${FILE} does not exist locally."
  47. exit 1
  48. fi
  49. # If the file already exists on the host, append a number and iterate if necessary
  50. i=0
  51. FILE_PREFIX_tmp="${FILE_PREFIX}"
  52. while [ "$ok" != "y" ]; do
  53. # constuct file name, and replace spaces by underscores
  54. DEST="$(echo "$FILE_PREFIX_tmp$FILE_SUFFIX" | sed 's/ /_/g')"
  55. # Check if file is existent remotely
  56. cmd="test -e \"${SSH_PATH}\"/\"${DEST}\""
  57. # If file does already exist with this name
  58. if ssh -q "${SSH_HOST}" "${cmd}"; then
  59. # iterate number and append -<number> to file name in front of suffix
  60. ((i++))
  61. FILE_PREFIX_tmp="$FILE_PREFIX"-${i}
  62. ok=n
  63. # If file does not exist yet
  64. else
  65. # Only output this info if a rename took place
  66. if [[ $i -gt 0 ]]; then
  67. echo "A file with an identical name already exists in the remote directory."
  68. echo "Your file has been renamed to prevent overwriting."
  69. echo
  70. fi
  71. ok=y
  72. fi
  73. done
  74. # Upload file
  75. scp "$FILE" "${SSH_HOST}":"${SSH_PATH}"/"${DEST}"
  76. # Output download link
  77. echo "File has been uploaded. Download link:"
  78. echo "$URL/$DEST"
  79. exit 0