Skip to content

Commit

Permalink
Fixed permissions and modify time for optimized images
Browse files Browse the repository at this point in the history
  • Loading branch information
zevilz committed Sep 27, 2020
1 parent 72bff77 commit 79d94ab
Showing 1 changed file with 45 additions and 2 deletions.
47 changes: 45 additions & 2 deletions zImageOptimizer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# URL: https://github.com/zevilz/zImageOptimizer
# Author: Alexandr "zEvilz" Emshanov
# License: MIT
# Version: 0.10.3
# Version: 0.10.4

sayWait()
{
Expand Down Expand Up @@ -637,6 +637,13 @@ fixTimeMarker()
fi
}

updateModifyTime()
{
if [ $NEW_ONLY -eq 1 ]; then
touch "$IMAGE" -r "$TIME_MARKER_FULL_PATH" > /dev/null
fi
}

optimJpegoptim()
{
jpegoptim --strip-all "$1" > /dev/null
Expand Down Expand Up @@ -900,6 +907,23 @@ checkDirLock()
fi
}

savePerms()
{
if [[ "$OSTYPE" == "linux-gnu" ]]; then
CUR_OWNER=$(stat -c "%U:%G" "$IMAGE")
CUR_PERMS=$(stat -c "%a" "$IMAGE")
else
CUR_OWNER=$(ls -l "$IMAGE" | awk '{print $3":"$4}')
CUR_PERMS=$(stat -f "%Lp" "$IMAGE")
fi
}

restorePerms()
{
chown $CUR_OWNER "$IMAGE"
chmod $CUR_PERMS "$IMAGE"
}

usage()
{
echo
Expand Down Expand Up @@ -1518,14 +1542,18 @@ if ! [ -z "$IMAGES" ]; then
echo "$IMAGES" | ( \
while read IMAGE ; do

# Define additional vars for using hooks
# Additional vars for using hooks
OPTIMIZE=1
OPTIMIZE_JPG=1
OPTIMIZE_PNG=1
OPTIMIZE_GIF=1
RESTORE_IMAGE_CHECK=1
BACKUP=1

# Internal vars
RESTORE_IMAGE_PERMS=1
UPDATE_IMAGE_MODIFY_TIME=1

# Process counter
if [ $LESS -eq 0 ]; then
# if [ $SHOW_PROGRESS -eq 1 ]; then
Expand Down Expand Up @@ -1557,6 +1585,9 @@ if ! [ -z "$IMAGES" ]; then

if [ $BACKUP -eq 1 ]; then

# Save permissions
savePerms

# Backup original file
cp -fp "$IMAGE" "$TMP_PATH/$(basename "$IMAGE").bkp"

Expand Down Expand Up @@ -1626,6 +1657,8 @@ if ! [ -z "$IMAGES" ]; then

if [ $SIZE_BEFORE -le $SIZE_AFTER ]; then
cp -fp "$TMP_PATH/$(basename "$IMAGE").bkp" "$IMAGE"
RESTORE_IMAGE_PERMS=0
UPDATE_IMAGE_MODIFY_TIME=0
fi

fi
Expand All @@ -1637,6 +1670,16 @@ if ! [ -z "$IMAGES" ]; then

fi

# Restore image permissions
if [ $RESTORE_IMAGE_PERMS -eq 1 ]; then
restorePerms
fi

# Update modify time from time marker
if [ $UPDATE_IMAGE_MODIFY_TIME -eq 1 ]; then
updateModifyTime
fi

# Calculate stats
if [ $SIZE_BEFORE -le $SIZE_AFTER ]; then
OUTPUT=$(echo "$OUTPUT+$SIZE_BEFORE" | bc)
Expand Down

0 comments on commit 79d94ab

Please sign in to comment.