From cd2c6a888e7ca12ac07f793abda36371ee2928f1 Mon Sep 17 00:00:00 2001
From: Dmitry Verkhoturov <paskal.07@gmail.com>
Date: Sat, 10 Aug 2024 11:29:32 +0000
Subject: [PATCH] add webp support to optimise-images script

---
 scripts/optimise-images.sh | 51 +++++++++++++++++++++++++-------------
 1 file changed, 34 insertions(+), 17 deletions(-)

diff --git a/scripts/optimise-images.sh b/scripts/optimise-images.sh
index 2f49400..4f88a00 100755
--- a/scripts/optimise-images.sh
+++ b/scripts/optimise-images.sh
@@ -1,14 +1,14 @@
 #!/usr/bin/env sh
 set -e -u
 
-# This script optimises png and jpeg images on the site and marks them as optimised, so that they are not processed again.
-# It uses optipng and advancecomp for PNGs and jpegoptim for JPEGs.
+# This script optimises png, jpeg, and webp images on the site and marks them as optimised, so that they are not processed again.
+# It uses optipng and advancecomp for PNGs, jpegoptim for JPEGs, and cwebp for WebP images.
 # The script is designed to be run as a cron job.
 # It only processes files that have been modified since the last optimisation.
 # It also cleans up orphaned .optimised files.
 
 # Install missing packages
-sudo apt -y install optipng advancecomp jpegoptim
+sudo apt -y install optipng advancecomp jpegoptim webp
 
 # Function to optimise PNGs
 optimise_png() {
@@ -36,6 +36,20 @@ optimise_jpeg() {
     fi
 }
 
+# Function to optimise WebP images
+optimise_webp() {
+    local file="$1"
+    local tmpfile="${file}.tmp.webp"
+    if nice -n 10 ionice -c2 -n7 cwebp -q 80 "$file" -o "$tmpfile"; then
+        mv "$tmpfile" "$file"
+        touch -r "$file" "$file.optimised"  # Set .optimised file's modification time to the original file's time
+        chmod 600 "$file.optimised"  # Restrict permissions to owner only
+    else
+        echo "Error: Failed to process $file with cwebp. Skipping."
+        rm -f "$tmpfile"  # Clean up temporary file if optimization fails
+    fi
+}
+
 # Clean up orphaned .optimised files
 cleanup_optimised_flags() {
     find web/prod/images web/prod/upload -type f -iname "*.optimised" | while read -r flag; do
@@ -46,19 +60,23 @@ cleanup_optimised_flags() {
     done
 }
 
-# Optimising PNG files
-echo "Optimising PNGs..."
-find web/prod/images web/prod/upload -type f -iname "*.png" | while read -r png; do
-    if [ ! -f "${png}.optimised" ] || [ "$png" -nt "${png}.optimised" ]; then
-        optimise_png "$png"
-    fi
-done
-
-# Optimising JPEG files
-echo "Optimising JPEGs..."
-find web/prod/images web/prod/upload -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) | while read -r jpg; do
-    if [ ! -f "${jpg}.optimised" ] || [ "$jpg" -nt "${jpg}.optimised" ]; then
-        optimise_jpeg "$jpg"
+# Single find command to locate PNG, JPEG, and WebP files, case insensitive
+echo "Optimising images..."
+find web/prod/images web/prod/upload -type f \( -iname "*.png" -o -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.webp" \) | while read -r file; do
+    if [ ! -f "${file}.optimised" ] || [ "$file" -nt "${file}.optimised" ]; then
+        # Convert file extension to lowercase
+        extension=$(echo "${file##*.}" | tr '[:upper:]' '[:lower:]')
+        case "$extension" in
+            png)
+                optimise_png "$file"
+                ;;
+            jpg|jpeg)
+                optimise_jpeg "$file"
+                ;;
+            webp)
+                optimise_webp "$file"
+                ;;
+        esac
     fi
 done
 
@@ -67,4 +85,3 @@ echo "Cleaning up orphaned .optimised files..."
 cleanup_optimised_flags
 
 echo "Images optimisation and cleanup complete"
-