Refactor
This commit is contained in:
+38
-16
@@ -10,38 +10,59 @@ echo "Resizing large JPGs in ZIPs"
|
||||
# Use RAM disk for temporary files.
|
||||
WORKDIR="/dev/shm/"
|
||||
|
||||
# Cleanup function
|
||||
cleanup_temp() {
|
||||
if [ -n "$TEMPDIR" ] && [ -d "$TEMPDIR" ]; then
|
||||
rm -r "$TEMPDIR"
|
||||
fi
|
||||
}
|
||||
|
||||
for INFILE in "$@"; do
|
||||
# Absolute path to old file
|
||||
#OLDFILE=`realpath "${INFILE}"`
|
||||
#OLDFILE=$(realpath "${INFILE}")
|
||||
OUTFILE="${INFILE%.zip}-compress.zip"
|
||||
|
||||
if [ ! -e "${OUTFILE}" ]; then
|
||||
# Remove output file if it already exists
|
||||
if [ -e "${OUTFILE}" ]; then
|
||||
rm "${OUTFILE}"
|
||||
fi
|
||||
# Set name for the temp dir. This directory will be created under WORKDIR
|
||||
TEMPDIR=`mktemp -p "$WORKDIR" -d`
|
||||
|
||||
# Create a temporary folder for unRARed files
|
||||
# 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"
|
||||
|
||||
|
||||
# extract files from zip
|
||||
unzip "$INFILE" -d "$TEMPDIR" # -j to junk directories
|
||||
if ! unzip "$INFILE" -d "$TEMPDIR"; then
|
||||
echo "Error: Failed to extract $INFILE"
|
||||
cleanup_temp
|
||||
continue
|
||||
fi
|
||||
|
||||
# resize
|
||||
#convert '${TEMPDIR}/*.jpg' -set filename:fn '%[basename]' -resize 2800000@@\> -sampling-factor 4:2:0 -strip -quality 85% -colorspace RGB '%[filename:fn].jpg'
|
||||
|
||||
pushd "$TEMPDIR"
|
||||
if ! pushd "$TEMPDIR" > /dev/null; then
|
||||
echo "Error: Failed to change to temp directory"
|
||||
cleanup_temp
|
||||
continue
|
||||
fi
|
||||
~/images_resize.sh
|
||||
popd
|
||||
popd > /dev/null
|
||||
|
||||
# Zip the files with no compression
|
||||
7z a -tzip -mx=0 "$OUTFILE" "${TEMPDIR}/*"
|
||||
|
||||
if [ $? -ne 0 ]; then
|
||||
echo Exiting
|
||||
exit 1
|
||||
pushd "$TEMPDIR" > /dev/null
|
||||
if ! 7z a -tzip -mx=0 "$OUTFILE" ./*; then
|
||||
echo "Error: Failed to create ZIP"
|
||||
popd > /dev/null
|
||||
cleanup_temp
|
||||
continue
|
||||
fi
|
||||
|
||||
popd > /dev/null
|
||||
|
||||
# Alternative. MUCH SLOWER, but better compression
|
||||
# 7z a -mm=Deflate -mfb=258 -mpass=15 -r "$NEWNAME" *
|
||||
@@ -52,7 +73,8 @@ for INFILE in "$@"; do
|
||||
mv "$OUTFILE" "$INFILE"
|
||||
|
||||
# Delete the temporary directory
|
||||
rm -r "$TEMPDIR"
|
||||
cleanup_temp
|
||||
trap - EXIT INT TERM
|
||||
|
||||
# OPTIONAL. Safe-remove the old file
|
||||
#gio trash "$OLDFILE"
|
||||
|
||||
Reference in New Issue
Block a user