From 1a3a6ec01ec3eb881154120d7bfdd6c4f94daefc Mon Sep 17 00:00:00 2001 From: ARAKHNID Date: Mon, 13 May 2024 19:18:03 -0500 Subject: [PATCH 1/4] Revert file if larger oxipng and jpegoptim already do this by default. cwebp doesn't have a skip option, so manually reverting it is necessary. Closes #172 --- src/compressor.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/compressor.py b/src/compressor.py index ca816a3..eb5e813 100644 --- a/src/compressor.py +++ b/src/compressor.py @@ -18,6 +18,7 @@ import threading import subprocess import logging +import shutil from gi.repository import GLib, Gio from pathlib import Path @@ -97,6 +98,15 @@ class Compressor(): new_file_data = Path(result_item.new_filename) if new_file_data.is_file(): result_item.new_size = new_file_data.stat().st_size + + # This check is mainly for WebP compression + try: + if result_item.new_size > result_item.size: + shutil.copy2(result_item.filename, result_item.new_filename) + result_item.new_size = new_file_data.stat().st_size + except shutil.SameFileError as err: + # This will happen with overwrite mode on + pass else: logging.error(str(output)) error_message = _("Can't find the compressed file") @@ -105,7 +115,7 @@ class Compressor(): GLib.idle_add(self.c_update_result_item, result_item, error, error_message) def build_png_command(self, result_item): - pngquant = 'pngquant --quality=0-{} -f "{}" --output "{}"' + pngquant = 'pngquant --quality=0-{} -f "{}" --output "{}" --skip-if-larger' oxipng = 'oxipng -o {} -i 1 "{}" --out "{}"' if not self.metadata: From e3149d39bd5d28624a1c40bc63cb174bdec22096 Mon Sep 17 00:00:00 2001 From: ARAKHNID Date: Tue, 14 May 2024 18:53:59 -0500 Subject: [PATCH 2/4] Add overwrite mode compatibility This is done by creating a copy of the input file that is then used to copy over onto the output file, effectively reverting it. This was the best solution I could find. --- src/compressor.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/compressor.py b/src/compressor.py index eb5e813..2ee9884 100644 --- a/src/compressor.py +++ b/src/compressor.py @@ -77,6 +77,13 @@ class Compressor(): GLib.idle_add(self.c_enable_compression, True) def run_command(self, command, result_item): + if not self.do_new_file: + # Creates a copy of the input file + # This is done in case the output file is larger than the input file + result_item_path = Path(result_item.filename) + original_filename = Path(result_item.filename).with_stem(f"{result_item_path.stem}-og") + shutil.copy2(result_item.filename, original_filename) + error = False error_message = '' try: @@ -99,14 +106,16 @@ class Compressor(): if new_file_data.is_file(): result_item.new_size = new_file_data.stat().st_size - # This check is mainly for WebP compression - try: - if result_item.new_size > result_item.size: + # This check is mainly for compressors that don't have a way + # to automatically detect and skip files + if result_item.new_size > result_item.size: + if self.do_new_file: shutil.copy2(result_item.filename, result_item.new_filename) - result_item.new_size = new_file_data.stat().st_size - except shutil.SameFileError as err: - # This will happen with overwrite mode on - pass + else: + shutil.copy2(original_filename, result_item.new_filename) + result_item.new_size = new_file_data.stat().st_size + original_filename.unlink(True) + else: logging.error(str(output)) error_message = _("Can't find the compressed file") From 3e0944d778ade6d86a4dd7ad0d915b616a105928 Mon Sep 17 00:00:00 2001 From: ARAKHNID Date: Thu, 16 May 2024 16:30:52 -0500 Subject: [PATCH 3/4] Simplify overwrite mode revert code This was done because the new code is cleaner and still produces the desired result, at least from my testing. --- src/compressor.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/compressor.py b/src/compressor.py index 2ee9884..8f6b0aa 100644 --- a/src/compressor.py +++ b/src/compressor.py @@ -18,6 +18,7 @@ import threading import subprocess import logging +import io import shutil from gi.repository import GLib, Gio from pathlib import Path @@ -80,9 +81,8 @@ class Compressor(): if not self.do_new_file: # Creates a copy of the input file # This is done in case the output file is larger than the input file - result_item_path = Path(result_item.filename) - original_filename = Path(result_item.filename).with_stem(f"{result_item_path.stem}-og") - shutil.copy2(result_item.filename, original_filename) + temp_filename = result_item.filename + ".temp" + shutil.copy2(result_item.filename, temp_filename) error = False error_message = '' @@ -112,9 +112,9 @@ class Compressor(): if self.do_new_file: shutil.copy2(result_item.filename, result_item.new_filename) else: - shutil.copy2(original_filename, result_item.new_filename) + shutil.copy2(temp_filename, result_item.new_filename) result_item.new_size = new_file_data.stat().st_size - original_filename.unlink(True) + Path(temp_filename).unlink(True) else: logging.error(str(output)) From 92c5dbf4f2abdd73f547b12147aa229acc3a8bbb Mon Sep 17 00:00:00 2001 From: ARAKHNID Date: Thu, 16 May 2024 16:33:13 -0500 Subject: [PATCH 4/4] Remove io import I forgot to remove this, I'd imported io in attempts to simplify the code and forgot it was imported. --- src/compressor.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/compressor.py b/src/compressor.py index 8f6b0aa..0c78f4a 100644 --- a/src/compressor.py +++ b/src/compressor.py @@ -18,7 +18,6 @@ import threading import subprocess import logging -import io import shutil from gi.repository import GLib, Gio from pathlib import Path