Skip already-processed ZIPs by sampling first JPEG comment
Extracts one JPEG from each ZIP to check for the processing comment before doing a full extract/resize/rezip cycle. Shared constants moved to config.sh as single source of truth. Trap and cleanup extended to cover the sample temp file.
This commit is contained in:
+27
-3
@@ -11,6 +11,7 @@ if [ $# -eq 0 ]; then
|
||||
fi
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
source "$SCRIPT_DIR/config.sh"
|
||||
|
||||
echo "Resizing large JPGs in ZIPs"
|
||||
|
||||
@@ -19,6 +20,9 @@ WORKDIR="/dev/shm/"
|
||||
|
||||
# Cleanup function
|
||||
cleanup_temp() {
|
||||
if [ -n "$SAMPLE" ] && [ -f "$SAMPLE" ]; then
|
||||
rm -f "$SAMPLE"
|
||||
fi
|
||||
if [ -n "$TEMPDIR" ] && [ -d "$TEMPDIR" ]; then
|
||||
rm -r "$TEMPDIR"
|
||||
fi
|
||||
@@ -35,6 +39,29 @@ for INFILE in "$@"; do
|
||||
INFILE="$(realpath "$INFILE")"
|
||||
echo "Processing file $CURRENT_FILE of $TOTAL_FILES: $INFILE"
|
||||
|
||||
SAMPLE=""
|
||||
TEMPDIR=""
|
||||
trap cleanup_temp EXIT INT TERM
|
||||
|
||||
# Check if already processed by sampling the first JPEG in the archive
|
||||
FIRST_JPEG=$(unzip -l "$INFILE" 2>/dev/null | grep -iE '\.(jpg|jpeg)$' | head -1 | awk '{print $NF}')
|
||||
if [ -n "$FIRST_JPEG" ]; then
|
||||
SAMPLE=$(mktemp -p "$WORKDIR" --suffix=.jpg)
|
||||
if unzip -p "$INFILE" "$FIRST_JPEG" > "$SAMPLE" 2>/dev/null; then
|
||||
COMMENT=$(~/Applications/magick identify -format "%c" "$SAMPLE" 2>/dev/null)
|
||||
rm -f "$SAMPLE"
|
||||
SAMPLE=""
|
||||
if [[ "$COMMENT" == "$MAGICK_COMMENT" ]]; then
|
||||
echo "Skipping $INFILE: already processed"
|
||||
trap - EXIT INT TERM
|
||||
continue
|
||||
fi
|
||||
else
|
||||
rm -f "$SAMPLE"
|
||||
SAMPLE=""
|
||||
fi
|
||||
fi
|
||||
|
||||
OUTFILE="$(realpath "${INFILE%.zip}-compress.zip")"
|
||||
|
||||
# Remove output file if it already exists
|
||||
@@ -45,9 +72,6 @@ for INFILE in "$@"; do
|
||||
# Set name for the temp dir. This directory will be created under WORKDIR
|
||||
TEMPDIR=$(mktemp -p "$WORKDIR" -d)
|
||||
|
||||
# Set up cleanup trap
|
||||
trap cleanup_temp EXIT INT TERM
|
||||
|
||||
# Create a temporary folder for unzipped files
|
||||
echo "Extracting $INFILE"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user