From 97ba5eac6ad79fc6db91594018242d4ba82d575b Mon Sep 17 00:00:00 2001 From: poprhythm Date: Thu, 27 Nov 2025 04:22:46 +0000 Subject: [PATCH] Improvements --- CLAUDE.md | 110 +++++++++++++++++++++++++++++++++++++ images_resize_recursive.sh | 11 +++- rar2zip.sh | 5 ++ rar2zip_images.sh | 3 +- rar2zip_incomplete.sh | 3 +- zip_images_recursive.sh | 4 +- zip_images_resize.sh | 15 +++-- 7 files changed, 141 insertions(+), 10 deletions(-) create mode 100644 CLAUDE.md diff --git a/CLAUDE.md b/CLAUDE.md new file mode 100644 index 0000000..164553e --- /dev/null +++ b/CLAUDE.md @@ -0,0 +1,110 @@ +# CLAUDE.md + +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. + +## Repository Overview + +This repository contains bash scripts for image processing operations, specifically for: +- Resizing large JPEG images using ImageMagick +- Converting RAR archives to ZIP format +- Processing images inside ZIP archives +- Recursive directory operations + +## Architecture + +### Script Organization + +The scripts follow a modular pattern with three tiers: + +1. **Core scripts** - Perform operations in current directory only: + - `images_resize.sh` - Resizes JPEGs in current directory + - `rar2zip.sh` - Converts RAR to ZIP (takes file arguments) + - `zip_images_resize.sh` - Resizes JPEGs inside ZIP files (takes file arguments) + +2. **Recursive scripts** - Traverse directories and call core scripts: + - `images_resize_recursive.sh` - Calls `~/images_resize.sh` in each subdirectory + - `zip_images_resize_recursive.sh` - Finds all ZIPs and calls `zip_images_resize.sh` + - `zip_images_recursive.sh` - Recursively creates ZIPs from JPEGs in directory trees + +3. **Wrapper scripts** - Execute core scripts on specific paths: + - `rar2zip_images.sh` - Runs `rar2zip.sh` on `/media/rewt/media/images/` + - `rar2zip_incomplete.sh` - Runs `rar2zip.sh` on `/media/rewt/media/incomplete/` + +### Key Design Patterns + +**Script Path Resolution**: Recursive and wrapper scripts reference scripts using relative paths with `SCRIPT_DIR`: +```bash +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +"$SCRIPT_DIR/images_resize.sh" +``` + +**ImageMagick Path**: Uses custom ImageMagick installation at `~/Applications/magick` + +**Temporary Storage**: Uses RAM disk (`/dev/shm/`) for temporary file operations to improve performance + +**Cleanup Handling**: Scripts implement trap-based cleanup functions to remove temp directories on exit/error + +**File Safety Checks**: Before deleting originals, scripts verify output files exist and are non-empty + +## Common Commands + +### Resize Images + +In current directory: +```bash +./images_resize.sh +``` + +Recursively through subdirectories: +```bash +./images_resize_recursive.sh +``` + +Image resize parameters: `-resize 8000000@@\>` (max 8MP), `-quality 85%`, `-sampling-factor 4:2:0` + +### Convert RAR to ZIP + +Single/multiple files: +```bash +./rar2zip.sh file1.rar file2.rar +``` + +All RARs in specific paths: +```bash +./rar2zip_images.sh # /media/rewt/media/images/ +./rar2zip_incomplete.sh # /media/rewt/media/incomplete/ +``` + +Compression: Uses 7z with `-mx=9` (maximum compression) + +### Process ZIP Archives + +Resize images inside ZIP files: +```bash +./zip_images_resize.sh archive1.zip archive2.zip +``` + +Recursively process all ZIPs: +```bash +./zip_images_resize_recursive.sh +``` + +Create ZIPs from directory structure (preserves hierarchy in filename): +```bash +./zip_images_recursive.sh +``` + +## Dependencies + +- ImageMagick (`~/Applications/magick`) - For image resizing +- `7z` - For ZIP compression +- `unrar` or `rar` - For RAR extraction +- `numfmt` - For human-readable file size output +- `/dev/shm/` - RAM disk for temporary storage + +## Important Notes + +- Scripts modify files in-place (originals are deleted after successful processing) +- File modification times are preserved using `touch -r` +- Scripts use absolute paths via `realpath` for file handling +- Recursive scripts use `find` with `-print0` and null-delimited reading for safe filename handling diff --git a/images_resize_recursive.sh b/images_resize_recursive.sh index 8240490..0b8a0b9 100755 --- a/images_resize_recursive.sh +++ b/images_resize_recursive.sh @@ -1,9 +1,14 @@ #!/bin/bash +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + find . -mindepth 1 -type d -print0 | while IFS= read -r -d '' f do - pushd "$f" - ~/images_resize.sh - popd + if pushd "$f" > /dev/null; then + "$SCRIPT_DIR/images_resize.sh" + popd > /dev/null + else + echo "Error: Failed to change to directory $f" + fi done diff --git a/rar2zip.sh b/rar2zip.sh index bc866a8..d22ee69 100755 --- a/rar2zip.sh +++ b/rar2zip.sh @@ -5,6 +5,11 @@ # # Usage: rar2zip.sh file [file ...] +if [ $# -eq 0 ]; then + echo "Usage: $0 file.rar [file2.rar ...]" + exit 1 +fi + echo "Converting RARs to ZIPs" # Use RAM disk for temporary files. diff --git a/rar2zip_images.sh b/rar2zip_images.sh index 1bb755a..5116dfa 100755 --- a/rar2zip_images.sh +++ b/rar2zip_images.sh @@ -1,2 +1,3 @@ #!/bin/bash -find /media/rewt/media/images/ -name '*.rar' -exec ~/rar2zip.sh '{}' \; +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +find /media/rewt/media/images/ -name '*.rar' -exec "$SCRIPT_DIR/rar2zip.sh" '{}' \; diff --git a/rar2zip_incomplete.sh b/rar2zip_incomplete.sh index 115b951..d5c48db 100755 --- a/rar2zip_incomplete.sh +++ b/rar2zip_incomplete.sh @@ -1,2 +1,3 @@ #!/bin/bash -find /media/rewt/media/incomplete/ -name '*.rar' -exec ~/rar2zip.sh '{}' \; +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +find /media/rewt/media/incomplete/ -name '*.rar' -exec "$SCRIPT_DIR/rar2zip.sh" '{}' \; diff --git a/zip_images_recursive.sh b/zip_images_recursive.sh index dd8bd1c..960861e 100755 --- a/zip_images_recursive.sh +++ b/zip_images_recursive.sh @@ -28,8 +28,10 @@ if [ -n "$jpgs" ]; then fi +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + find . -mindepth 1 -maxdepth 1 -type d -print0 | while IFS= read -r -d '' subd; do pushd "${subd}" - ~/zip_images_recursive.sh "${directoryTree}" + "$SCRIPT_DIR/zip_images_recursive.sh" "${directoryTree}" popd done diff --git a/zip_images_resize.sh b/zip_images_resize.sh index 88d545b..5bd829a 100755 --- a/zip_images_resize.sh +++ b/zip_images_resize.sh @@ -5,6 +5,11 @@ # # Usage: xxx.sh file [file ...] +if [ $# -eq 0 ]; then + echo "Usage: $0 file.zip [file2.zip ...]" + exit 1 +fi + echo "Resizing large JPGs in ZIPs" # Use RAM disk for temporary files. @@ -23,10 +28,12 @@ CURRENT_FILE=0 for INFILE in "$@"; do CURRENT_FILE=$((CURRENT_FILE + 1)) + + # Convert to absolute path + INFILE="$(realpath "$INFILE")" echo "Processing file $CURRENT_FILE of $TOTAL_FILES: $INFILE" - # Absolute path to old file - #OLDFILE=$(realpath "${INFILE}") - OUTFILE="${INFILE%.zip}-compress.zip" + + OUTFILE="$(realpath "${INFILE%.zip}-compress.zip")" # Remove output file if it already exists if [ -e "${OUTFILE}" ]; then @@ -81,7 +88,7 @@ for INFILE in "$@"; do if [ ! -s "$OUTFILE" ]; then echo "Error: Output file is empty or does not exist. Keeping original file." cleanup_temp - continue + exit 1 fi rm "$INFILE"