This commit is contained in:
Arak 2024-05-16 21:34:03 +00:00 committed by GitHub
commit d1dd450bda
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -18,6 +18,7 @@
import threading import threading
import subprocess import subprocess
import logging import logging
import shutil
from gi.repository import GLib, Gio from gi.repository import GLib, Gio
from pathlib import Path from pathlib import Path
@ -76,6 +77,12 @@ class Compressor():
GLib.idle_add(self.c_enable_compression, True) GLib.idle_add(self.c_enable_compression, True)
def run_command(self, command, result_item): 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
temp_filename = result_item.filename + ".temp"
shutil.copy2(result_item.filename, temp_filename)
error = False error = False
error_message = '' error_message = ''
try: try:
@ -97,6 +104,17 @@ class Compressor():
new_file_data = Path(result_item.new_filename) new_file_data = Path(result_item.new_filename)
if new_file_data.is_file(): if new_file_data.is_file():
result_item.new_size = new_file_data.stat().st_size result_item.new_size = new_file_data.stat().st_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)
else:
shutil.copy2(temp_filename, result_item.new_filename)
result_item.new_size = new_file_data.stat().st_size
Path(temp_filename).unlink(True)
else: else:
logging.error(str(output)) logging.error(str(output))
error_message = _("Can't find the compressed file") error_message = _("Can't find the compressed file")
@ -105,7 +123,7 @@ class Compressor():
GLib.idle_add(self.c_update_result_item, result_item, error, error_message) GLib.idle_add(self.c_update_result_item, result_item, error, error_message)
def build_png_command(self, result_item): 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 "{}"' oxipng = 'oxipng -o {} -i 1 "{}" --out "{}"'
if not self.metadata: if not self.metadata: