Browse Source

check if file already exists remotely, and rename it accordingly to prevent overwriting

mxmehl 1 year ago
parent
commit
4c75729fa5
Signed by: mxmehl <mail@mehl.mx> GPG Key ID: 2704E4AB371E2E92
2 changed files with 32 additions and 16 deletions
  1. 4
    2
      config.cfg.sample
  2. 28
    14
      uas.sh

+ 4
- 2
config.cfg.sample View File

@@ -1,5 +1,7 @@
1
-# SSH host and directory to upload file
2
-SSH=user@host.tld:\~/uploads/share
1
+# SSH user@host or HostName
2
+SSH_HOST=user@host.tld
3
+# Absolute path where files shall be stored
4
+SSH_PATH=/home/user/uploads/share
3 5
 
4 6
 # URL to this directory
5 7
 URL=http://uploads.host.tld/share

+ 28
- 14
uas.sh View File

@@ -1,6 +1,6 @@
1 1
 #!/bin/bash
2 2
 ########################################################################
3
-#  Copyright (C) 2015 Max Mehl <mail@mehl.mx>
3
+#  Copyright (C) 2018 Max Mehl <mail@mehl.mx>
4 4
 ########################################################################
5 5
 #  
6 6
 #  This program is free software: you can redistribute it and/or modify
@@ -26,32 +26,46 @@
26 26
 
27 27
 BASENAME=$(basename $0)
28 28
 
29
+# Load config
30
+BINDIR="$(dirname "$(readlink -f "$0")")"
31
+# Test if config.cfg exists and set needed variables
32
+if [ ! -e "$BINDIR"/config.cfg ]; then echo "Missing config.cfg file. Edit and rename config.cfg.sample"; exit 1; fi
33
+source "$BINDIR"/config.cfg
34
+
29 35
 if [ "$1" = "" ]; then
30 36
 	echo "Usage: $BASENAME <file-to-upload>"
31 37
 	exit 1
32 38
 fi
33 39
 
34 40
 # Transform to full path
35
-PWD=$(pwd)
36 41
 FILE="$1"
37
-FILENAME=$(basename $FILE)
42
+FILE_NAME=$(basename $FILE)
43
+FILE_PREFIX=${FILE_NAME%.*}
44
+FILE_SUFFIX=$([[ "$FILE_NAME" = *.* ]] && echo ".${FILE_NAME##*.}" || echo '')
38 45
 
39 46
 if [ ! -e "$FILE" ]; then
40
-    echo "File $FILE does not exist."
47
+    echo "File $FILE does not exist locally."
41 48
     exit 1
42 49
 fi
43 50
 
44
-# Jump to dir of where the executable relies to load config
45
-BINDIR="$(dirname "$(readlink -f "$0")")"
46
-# Test if config.cfg exists and set needed variables
47
-if [ ! -e "$BINDIR"/config.cfg ]; then echo "Missing config.cfg file. Edit and rename config.cfg.sample"; exit 1; fi
48
-source "$BINDIR"/config.cfg
49
-
50
-
51
-# Replace spaces by underscores for better downloading
52
-DEST=$(echo "$FILENAME" | sed 's/ /_/g')
51
+i=1
52
+FILE_PREFIX_tmp="$FILE_PREFIX"
53
+while [ "$ok" != "y" ]; do
54
+  DEST=$(echo "$FILE_PREFIX_tmp$FILE_SUFFIX" | sed 's/ /_/g')
55
+  ssh ${SSH_HOST} "test -e ${SSH_PATH}/$DEST"
56
+  if [ $? == 0 ]; then
57
+    FILE_PREFIX_tmp="$FILE_PREFIX"-${i}
58
+    ((i++))
59
+    ok=n
60
+  else
61
+    echo "A file with an identical name already exists in the remote directory."
62
+    echo "Your file will be renamed to prevent overwriting."
63
+    echo
64
+    ok=y
65
+  fi
66
+done
53 67
 
54
-scp "$FILE" "$SSH"/"$DEST"
68
+scp "$FILE" ${SSH_HOST}:${SSH_PATH}/"$DEST"
55 69
 
56 70
 # Output download link
57 71
 echo "File has been uploaded. Download link:"

Loading…
Cancel
Save