Improvements
This commit is contained in:
@@ -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
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
+2
-1
@@ -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" '{}' \;
|
||||
|
||||
@@ -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" '{}' \;
|
||||
|
||||
@@ -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
|
||||
|
||||
+11
-4
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user