-
-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Revert compressed file if larger #229
base: master
Are you sure you want to change the base?
Conversation
oxipng and jpegoptim already do this by default. cwebp doesn't have a skip option, so manually reverting it is necessary. Closes Huluti#172
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.
This should work in overwrite mode. I wish the solution was cleaner, but this was the best I could come up with. I should note something that I found on the Python docs, however:
This does make me a little hesitant to use |
This was done because the new code is cleaner and still produces the desired result, at least from my testing.
I forgot to remove this, I'd imported io in attempts to simplify the code and forgot it was imported.
Is there anything I could/should do to improve this? I'm still not super enthusiastic about the solution in overwrite mode, but ideally there would just be an option upstream for the WebP and SVG(?) compressors to skip files if larger anyway. |
I will try to review the PR in the next few days, I put it on my TODO :) |
Alright, thank you 👍 |
To summarize:
So shutil.copy2() will be useful for libwebp and scour. So only really useful for libwebp. Sorry for the long review... |
src/compressor.py
Outdated
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" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a '.' in front of the filename could be useful to "hide" the file in file managers? What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea. I added this.
I can, but this should only run if the output file is larger. It won't run for SVGs if SVGs aren't going to be larger. Up to you though. And don't worry about the long review, it's fine :) |
Adds a "." to the beginning of the temporary file to hide it from file managers.
Accidentally forgot to remove this. It was only needed for testing.
Some code that was only supposed to run in overwrite mode was running in safe mode.
Oxipng and jpegoptim already skip over files that'll be larger by default. pngquant has an option to skip them if larger, which I added. cwebp doesn't have any option for this as far as I know, so the file has to be reverted manually. I'm not sure if there's a cleaner way to do it. The attached image shows the result of a file being skipped/reverted.
I'm also not sure if scour has an option to skip files if they'll end up being larger, but if #227 gets fixed, that won't be a problem. The check I added should catch compressed SVG files that end up being larger anyway.
This currently doesn't properly handle reverting files while in overwrite mode. I'll implement that when I have the time.(Done).Closes #172