Merge branch 'master' into search_posts_by_filetype

This commit is contained in:
Neil Lalonde
2017-07-25 14:41:20 -04:00
committed by GitHub
18042 changed files with 120300 additions and 33700 deletions

View File

@ -16,8 +16,10 @@ class UploadCreator
# - type (string)
# - content_type (string)
# - origin (string)
# - is_attachment_for_group_message (boolean)
# - for_group_message (boolean)
# - for_theme (boolean)
# - for_private_message (boolean)
# - pasted (boolean)
def initialize(file, filename, opts = {})
@upload = Upload.new
@file = file
@ -38,7 +40,7 @@ class UploadCreator
if @filename[/\.svg$/i]
whitelist_svg!
else
elsif !Rails.env.test?
convert_to_jpeg! if should_convert_to_jpeg?
downsize! if should_downsize?
@ -79,13 +81,9 @@ class UploadCreator
@upload.width, @upload.height = ImageSizer.resize(*@image_info.size)
end
if @opts[:is_attachment_for_group_message]
@upload.is_attachment_for_group_message = true
end
if @opts[:for_theme]
@upload.for_theme = true
end
@upload.for_private_message = true if @opts[:for_private_message]
@upload.for_group_message = true if @opts[:for_group_message]
@upload.for_theme = true if @opts[:for_theme]
return @upload unless @upload.save
@ -126,9 +124,10 @@ class UploadCreator
MIN_PIXELS_TO_CONVERT_TO_JPEG ||= 1280 * 720
def should_convert_to_jpeg?
TYPES_CONVERTED_TO_JPEG.include?(@image_info.type) &&
pixels > MIN_PIXELS_TO_CONVERT_TO_JPEG &&
SiteSetting.png_to_jpg_quality < 100
return false if !TYPES_CONVERTED_TO_JPEG.include?(@image_info.type)
return true if @opts[:pasted]
return false if SiteSetting.png_to_jpg_quality == 100
pixels > MIN_PIXELS_TO_CONVERT_TO_JPEG
end
def convert_to_jpeg!
@ -137,7 +136,9 @@ class UploadCreator
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
@ -145,10 +146,10 @@ class UploadCreator
# keep the JPEG if it's at least 15% smaller
if File.size(jpeg_tempfile.path) < filesize * 0.85
@image_info = FastImage.new(jpeg_tempfile)
@file = jpeg_tempfile
@filename = (File.basename(@filename, ".*").presence || I18n.t("image").presence || "image") + ".jpg"
@opts[:content_type] = "image/jpeg"
extract_image_info!
else
jpeg_tempfile.close! rescue nil
end
@ -187,6 +188,18 @@ class UploadCreator
@file.rewind
end
def should_fix_orientation?
# orientation is between 1 and 8, 1 being the default
# cf. http://www.daveperrett.com/articles/2012/07/28/exif-orientation-handling-is-a-ghetto/
@image_info.orientation.to_i > 1
end
def fix_orientation!
OptimizedImage.ensure_safe_paths!(@file.path)
Discourse::Utils.execute_command('convert', @file.path, '-auto-orient', @file.path)
extract_image_info!
end
def should_crop?
TYPES_TO_CROP.include?(@opts[:type])
end
@ -209,17 +222,8 @@ class UploadCreator
when "custom_emoji"
OptimizedImage.downsize(@file.path, @file.path, "100x100\\>", filename: @filename, allow_animation: allow_animation)
end
end
def should_fix_orientation?
# orientation is between 1 and 8, 1 being the default
# cf. http://www.daveperrett.com/articles/2012/07/28/exif-orientation-handling-is-a-ghetto/
@image_info.orientation.to_i > 1
end
def fix_orientation!
OptimizedImage.ensure_safe_paths!(@file.path)
Discourse::Utils.execute_command('convert', @file.path, '-auto-orient', @file.path)
extract_image_info!
end
def should_optimize?
@ -234,7 +238,8 @@ class UploadCreator
def optimize!
OptimizedImage.ensure_safe_paths!(@file.path)
ImageOptim.new.optimize_image!(@file.path)
FileHelper.optimize_image!(@file.path)
extract_image_info!
rescue ImageOptim::Worker::TimeoutExceeded
Rails.logger.warn("ImageOptim timed out while optimizing #{@filename}")
end