mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 09:12:45 +08:00
Retry PNG to JPG conversion with debug enabled on failure
This commit is contained in:
parent
9472821331
commit
b47b378cb6
|
@ -134,15 +134,13 @@ class UploadCreator
|
|||
jpeg_tempfile = Tempfile.new(["image", ".jpg"])
|
||||
|
||||
OptimizedImage.ensure_safe_paths!(@file.path, jpeg_tempfile.path)
|
||||
Discourse::Utils.execute_command(
|
||||
'convert', @file.path,
|
||||
'-auto-orient',
|
||||
'-background', 'white',
|
||||
'-interlace', 'none',
|
||||
'-flatten',
|
||||
'-quality', SiteSetting.png_to_jpg_quality.to_s,
|
||||
jpeg_tempfile.path
|
||||
)
|
||||
|
||||
begin
|
||||
execute_convert(@file, jpeg_tempfile)
|
||||
rescue
|
||||
# retry with debugging enabled
|
||||
execute_convert(@file, jpeg_tempfile, true)
|
||||
end
|
||||
|
||||
# keep the JPEG if it's at least 15% smaller
|
||||
if File.size(jpeg_tempfile.path) < filesize * 0.85
|
||||
|
@ -155,6 +153,19 @@ class UploadCreator
|
|||
end
|
||||
end
|
||||
|
||||
def execute_convert(input_file, output_file, debug = false)
|
||||
command = ['convert', input_file.path,
|
||||
'-auto-orient',
|
||||
'-background', 'white',
|
||||
'-interlace', 'none',
|
||||
'-flatten',
|
||||
'-quality', SiteSetting.png_to_jpg_quality.to_s]
|
||||
command << '-debug' << 'all' if debug
|
||||
command << output_file.path
|
||||
|
||||
Discourse::Utils.execute_command(*command, failure_message: "failed to convert png to jpg")
|
||||
end
|
||||
|
||||
def should_downsize?
|
||||
max_image_size > 0 && filesize >= max_image_size
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue
Block a user