Skip to content

Commit 5de340a

Browse files
fix: quote filenames as they can contain spaces ( see #2 )
1 parent 9ec03d6 commit 5de340a

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

save.sh

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ DEST_DIR='' : "${DEST_DIR:?DEST_DIR is not set}"
1717
IGNORE_REGEX="${IGNORE_REGEX:-.*(MV-PANO|COLLAGE|-ANIMATION|-EFFECTS)\..*)}"
1818

1919
function do_image () {
20-
local FILE=$1
21-
local PARENT_DIR=$(dirname $FILE)
20+
local FILE="$1"
21+
local PARENT_DIR=$(dirname "$FILE")
2222

2323
echo "PARENT_DIR: $PARENT_DIR"
2424

2525
local creation_time=$(exiftool -DateTimeOriginal -d "%Y-%m-%d %H:%M:%S" "$FILE" | awk -F ': ' '{print $2}')
2626

27-
local filename=$(basename $FILE)
27+
local filename=$(basename "$FILE")
2828
# If creation_time is not available, try getting it from filename
2929
if [[ -z "$creation_time" ]]; then
3030
# Use regex to match the encoded timestamp in the filename
31-
if [[ $filename =~ ^.*([0-9]{8})_([0-9]{6}).* ]]; then
31+
if [[ "$filename" =~ ^.*([0-9]{8})_([0-9]{6}).* ]]; then
3232
# Extract the encoded date and time
3333
encoded_date=${BASH_REMATCH[1]}
3434
encoded_time=${BASH_REMATCH[2]}
@@ -63,16 +63,16 @@ function do_image () {
6363
else
6464

6565
# check if downloaded file has GPS data
66-
local has_gps=$(exiftool $FILE | grep GPS)
66+
local has_gps=$(exiftool "$FILE" | grep GPS)
6767

6868
# set file time
69-
exiftool "-DateTimeOriginal>FileModifyDate" $FILE
69+
exiftool "-DateTimeOriginal>FileModifyDate" "$FILE"
7070

7171
# move file to proper subdir
72-
exiftool -d "${PARENT_DIR}/%Y/%Y-%m" '-directory<${DateTimeOriginal}' '-filename<${filename}' $FILE
72+
exiftool -d "${PARENT_DIR}/%Y/%Y-%m" '-directory<${DateTimeOriginal}' '-filename<${filename}' "$FILE"
7373

7474
# move to destination folder
75-
cd $PARENT_DIR
75+
cd "$PARENT_DIR"
7676

7777
local new_filepath=$(find -name "$filename")
7878
local local_path=${new_filepath#"./"}
@@ -85,7 +85,7 @@ function do_image () {
8585
elif [[ -z "$has_gps" ]]; then
8686
echo "Checking if the overwrite of $obsoleted_file would cause loss of GPS data"
8787
# check if existing file has GPS data
88-
local target_has_gps=$(exiftool $new_filepath | grep GPS)
88+
local target_has_gps=$(exiftool "$new_filepath" | grep GPS)
8989
if [[ -z "$target_has_gps" ]]; then
9090
prevent_sync="target GPS data would be lost"
9191
fi
@@ -98,28 +98,28 @@ function do_image () {
9898
-av \
9999
--remove-source-files \
100100
--include='20[0-9][0-9]/' --include='20[0-9][0-9]/20[0-9][0-9]-[0-1][0-9]/' \
101-
. $DEST_DIR
101+
. "$DEST_DIR"
102102
else
103103
echo "No rsync as $prevent_sync"
104104
fi
105105
fi
106106

107-
rm -rf $PARENT_DIR
107+
rm -rf "$PARENT_DIR"
108108
}
109109

110110
function do_video () {
111-
local FILE=$1
112-
local PARENT_DIR=$(dirname $FILE)
111+
local FILE="$1"
112+
local PARENT_DIR=$(dirname "$FILE")
113113

114114
echo "PARENT_DIR: $PARENT_DIR"
115115

116116
local creation_time=$(ffprobe -v quiet -print_format json -show_entries format_tags=creation_time "$FILE" | jq -r '.format.tags.creation_time')
117117

118-
local filename=$(basename $FILE)
118+
local filename=$(basename "$FILE")
119119
# If creation_time is not available, try getting it from filename
120120
if [[ "$creation_time" == "null" || -z "$creation_time" ]]; then
121121
# Use regex to match the encoded timestamp in the filename
122-
if [[ $filename =~ ^FILE([0-9]{6})-([0-9]{6}) ]]; then
122+
if [[ "$filename" =~ ^FILE([0-9]{6})-([0-9]{6}) ]]; then
123123
# Extract the encoded date and time
124124
encoded_date=${BASH_REMATCH[1]}
125125
encoded_time=${BASH_REMATCH[2]}
@@ -167,21 +167,21 @@ function do_video () {
167167
echo "Moved $FILE to $target_dir/"
168168

169169
# move to destination folder
170-
cd $PARENT_DIR
170+
cd "$PARENT_DIR"
171171
rsync \
172172
-av \
173173
--remove-source-files \
174174
--include='20[0-9][0-9]/' --include='20[0-9][0-9]/20[0-9][0-9]-[0-1][0-9]/' \
175-
. $DEST_DIR
175+
. "$DEST_DIR"
176176

177177
fi
178178

179-
rm -rf $PARENT_DIR
179+
rm -rf "$PARENT_DIR"
180180
}
181181

182182
# Check if the file matches the regex in IGNORE_REGEX
183-
base=$(basename $1)
184-
if [[ ! $base =~ $IGNORE_REGEX ]]; then
183+
base=$(basename "$1")
184+
if [[ ! "$base" =~ $IGNORE_REGEX ]]; then
185185
echo "Processing: $1"
186186
mimetype=$(file --mime-type --no-pad $1| awk '{print $2}')
187187

@@ -194,10 +194,10 @@ if [[ ! $base =~ $IGNORE_REGEX ]]; then
194194
;;
195195
*)
196196
echo "$1 is neither an image nor a video"
197-
rm -rf $(dirname $1)
197+
rm -rf $(dirname "$1")
198198
;;
199199
esac
200200
else
201201
echo "Discarding $1"
202-
rm -rf $(dirname $1)
202+
rm -rf $(dirname "$1")
203203
fi

0 commit comments

Comments
 (0)