Fixed that cashed images are kept even when original does not exist anymore

This commit is contained in:
Hannes
2026-02-26 00:12:32 +01:00
parent bcaaad1b7c
commit 08c920c905
2 changed files with 39 additions and 2 deletions
+38 -2
View File
@@ -10,7 +10,9 @@ if [ ! -d $THUMB_DIR ]; then
mkdir -p $THUMB_DIR;
fi
shopt -s nullglob
files=( "$THUMB_DIR"/*.jpg "$THUMB_DIR"/*.jpeg "$THUMB_DIR"/*.png )
# printf ' %s\n' "${files[@]}"
# Make sure the thumb directory exists
mkdir -p "$THUMB_DIR"
@@ -19,6 +21,28 @@ MAX_DIM=300
num=0
remove_from_array() {
local -n arr=$1
local elem=$2
local result=()
# echo "=== Removing element: '$elem' ==="
# echo "Original array has ${#arr[@]} elements"
for item in "${arr[@]}"; do
if [[ "$item" != "$elem" ]]; then
result+=( "$item" )
# echo "Keeping: '$item'"
# else
# echo "Removing: '$item'"
fi
done
arr=( "${result[@]}" )
# echo "New array has ${#arr[@]} elements"
# echo "-----------------------------------"
}
# Process each image
for img in "$WALLPAPER_DIR"/*.{jpg,jpeg,png}; do
# Skip if no files match
@@ -34,16 +58,18 @@ for img in "$WALLPAPER_DIR"/*.{jpg,jpeg,png}; do
# Output path
thumb="$THUMB_DIR/$fname"
# If thumbnail already exists and is newer than the original, skip
if [ -f "$thumb" ] && [ "$thumb" -nt "$img" ]; then
remove_from_array files "$thumb"
continue
fi
num+=1
# Generate thumbnail using ImageMagick
if [ -x "/usr/bin/magick" ] then
if [ -x "/usr/bin/magick" ]; then
magick "$img" -resize "${MAX_DIM}x${MAX_DIM}" "$thumb"
rm $temp_image_name
else
@@ -52,6 +78,16 @@ for img in "$WALLPAPER_DIR"/*.{jpg,jpeg,png}; do
fi
cp $img $temp_image_name
fi
remove_from_array files "$thumb"
done
# echo ""
# printf ' %s\n' "${files[@]}"
for img in $files; do
echo "$img"
rm $img
done
echo "$num Thumbnails generated in $THUMB_DIR"