2017-05-11 06:16:57 +08:00
|
|
|
require "fastimage"
|
|
|
|
require_dependency "image_sizer"
|
|
|
|
|
|
|
|
class UploadCreator
|
|
|
|
|
|
|
|
TYPES_CONVERTED_TO_JPEG ||= %i{bmp png}
|
|
|
|
|
2017-05-18 18:13:13 +08:00
|
|
|
TYPES_TO_CROP ||= %w{avatar card_background custom_emoji profile_background}.each(&:freeze)
|
|
|
|
|
2017-05-11 06:16:57 +08:00
|
|
|
WHITELISTED_SVG_ELEMENTS ||= %w{
|
|
|
|
circle clippath defs ellipse g line linearGradient path polygon polyline
|
|
|
|
radialGradient rect stop svg text textpath tref tspan use
|
2017-05-18 18:13:13 +08:00
|
|
|
}.each(&:freeze)
|
2017-05-11 06:16:57 +08:00
|
|
|
|
|
|
|
# Available options
|
|
|
|
# - type (string)
|
|
|
|
# - origin (string)
|
2017-06-13 04:41:29 +08:00
|
|
|
# - for_group_message (boolean)
|
2017-05-11 06:16:57 +08:00
|
|
|
# - for_theme (boolean)
|
2017-06-13 04:41:29 +08:00
|
|
|
# - for_private_message (boolean)
|
2017-06-23 18:13:48 +08:00
|
|
|
# - pasted (boolean)
|
2018-04-19 19:30:31 +08:00
|
|
|
# - for_export (boolean)
|
2017-05-11 06:16:57 +08:00
|
|
|
def initialize(file, filename, opts = {})
|
|
|
|
@file = file
|
2017-12-14 04:51:09 +08:00
|
|
|
@filename = filename || ''
|
|
|
|
@upload = Upload.new(original_filename: filename, filesize: 0)
|
2017-05-11 06:16:57 +08:00
|
|
|
@opts = opts
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_for(user_id)
|
|
|
|
if filesize <= 0
|
|
|
|
@upload.errors.add(:base, I18n.t("upload.empty"))
|
|
|
|
return @upload
|
|
|
|
end
|
|
|
|
|
|
|
|
DistributedMutex.synchronize("upload_#{user_id}_#{@filename}") do
|
|
|
|
if FileHelper.is_image?(@filename)
|
|
|
|
extract_image_info!
|
|
|
|
return @upload if @upload.errors.present?
|
|
|
|
|
2018-08-07 13:15:00 +08:00
|
|
|
image_type = @image_info.type.to_s
|
|
|
|
basename = File.basename(@filename, File.extname(@filename))
|
|
|
|
@filename = "#{basename}.#{image_type}"
|
|
|
|
|
2017-05-11 06:16:57 +08:00
|
|
|
if @filename[/\.svg$/i]
|
|
|
|
whitelist_svg!
|
2017-06-22 10:23:31 +08:00
|
|
|
elsif !Rails.env.test?
|
2017-05-11 06:16:57 +08:00
|
|
|
convert_to_jpeg! if should_convert_to_jpeg?
|
|
|
|
downsize! if should_downsize?
|
|
|
|
|
|
|
|
return @upload if is_still_too_big?
|
|
|
|
|
|
|
|
fix_orientation! if should_fix_orientation?
|
|
|
|
crop! if should_crop?
|
|
|
|
optimize! if should_optimize?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# compute the sha of the file
|
|
|
|
sha1 = Upload.generate_digest(@file)
|
|
|
|
|
|
|
|
# do we already have that upload?
|
|
|
|
@upload = Upload.find_by(sha1: sha1)
|
|
|
|
|
|
|
|
# make sure the previous upload has not failed
|
|
|
|
if @upload && @upload.url.blank?
|
|
|
|
@upload.destroy
|
|
|
|
@upload = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
# return the previous upload if any
|
|
|
|
return @upload unless @upload.nil?
|
|
|
|
|
|
|
|
# create the upload otherwise
|
|
|
|
@upload = Upload.new
|
|
|
|
@upload.user_id = user_id
|
|
|
|
@upload.original_filename = @filename
|
|
|
|
@upload.filesize = filesize
|
|
|
|
@upload.sha1 = sha1
|
|
|
|
@upload.url = ""
|
|
|
|
@upload.origin = @opts[:origin][0...1000] if @opts[:origin]
|
2018-08-13 10:55:06 +08:00
|
|
|
@upload.extension = image_type || File.extname(@filename)[1..10]
|
2017-05-11 06:16:57 +08:00
|
|
|
|
|
|
|
if FileHelper.is_image?(@filename)
|
|
|
|
@upload.width, @upload.height = ImageSizer.resize(*@image_info.size)
|
|
|
|
end
|
|
|
|
|
2017-06-13 04:41:29 +08:00
|
|
|
@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]
|
2018-04-19 19:30:31 +08:00
|
|
|
@upload.for_export = true if @opts[:for_export]
|
2017-05-11 06:16:57 +08:00
|
|
|
|
|
|
|
return @upload unless @upload.save
|
|
|
|
|
|
|
|
# store the file and update its url
|
|
|
|
File.open(@file.path) do |f|
|
2018-08-07 13:15:00 +08:00
|
|
|
url = Discourse.store.store_upload(f, @upload)
|
|
|
|
|
2017-05-11 06:16:57 +08:00
|
|
|
if url.present?
|
2018-08-07 13:15:00 +08:00
|
|
|
@upload.update!(url: url)
|
2017-05-11 06:16:57 +08:00
|
|
|
else
|
|
|
|
@upload.errors.add(:url, I18n.t("upload.store_failure", upload_id: @upload.id, user_id: user_id))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if @upload.errors.empty? && FileHelper.is_image?(@filename) && @opts[:type] == "avatar"
|
|
|
|
Jobs.enqueue(:create_avatar_thumbnails, upload_id: @upload.id, user_id: user_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
@upload
|
|
|
|
end
|
|
|
|
ensure
|
2018-03-28 16:20:08 +08:00
|
|
|
@file&.close
|
2017-05-11 06:16:57 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def extract_image_info!
|
|
|
|
@image_info = FastImage.new(@file) rescue nil
|
|
|
|
@file.rewind
|
|
|
|
|
|
|
|
if @image_info.nil?
|
|
|
|
@upload.errors.add(:base, I18n.t("upload.images.not_supported_or_corrupted"))
|
|
|
|
elsif filesize <= 0
|
|
|
|
@upload.errors.add(:base, I18n.t("upload.empty"))
|
|
|
|
elsif pixels == 0
|
|
|
|
@upload.errors.add(:base, I18n.t("upload.images.size_not_found"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-02 05:12:37 +08:00
|
|
|
MIN_PIXELS_TO_CONVERT_TO_JPEG ||= 1280 * 720
|
|
|
|
|
2017-05-11 06:16:57 +08:00
|
|
|
def should_convert_to_jpeg?
|
2017-06-23 18:13:48 +08:00
|
|
|
return false if !TYPES_CONVERTED_TO_JPEG.include?(@image_info.type)
|
|
|
|
return true if @opts[:pasted]
|
2017-06-26 20:21:47 +08:00
|
|
|
return false if SiteSetting.png_to_jpg_quality == 100
|
2017-06-23 18:13:48 +08:00
|
|
|
pixels > MIN_PIXELS_TO_CONVERT_TO_JPEG
|
2017-05-11 06:16:57 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def convert_to_jpeg!
|
|
|
|
jpeg_tempfile = Tempfile.new(["image", ".jpg"])
|
|
|
|
|
2018-07-26 04:00:04 +08:00
|
|
|
from = @file.path
|
|
|
|
to = jpeg_tempfile.path
|
|
|
|
|
|
|
|
OptimizedImage.ensure_safe_paths!(from, to)
|
|
|
|
|
2018-07-26 05:55:06 +08:00
|
|
|
from = OptimizedImage.prepend_decoder!(from)
|
|
|
|
to = OptimizedImage.prepend_decoder!(to)
|
2017-12-19 18:31:18 +08:00
|
|
|
|
|
|
|
begin
|
2018-07-26 04:00:04 +08:00
|
|
|
execute_convert(from, to)
|
2017-12-19 18:31:18 +08:00
|
|
|
rescue
|
|
|
|
# retry with debugging enabled
|
2018-07-26 04:00:04 +08:00
|
|
|
execute_convert(from, to, true)
|
2017-12-19 18:31:18 +08:00
|
|
|
end
|
2017-05-11 06:16:57 +08:00
|
|
|
|
|
|
|
# keep the JPEG if it's at least 15% smaller
|
|
|
|
if File.size(jpeg_tempfile.path) < filesize * 0.85
|
|
|
|
@file = jpeg_tempfile
|
|
|
|
@filename = (File.basename(@filename, ".*").presence || I18n.t("image").presence || "image") + ".jpg"
|
2017-06-22 22:53:49 +08:00
|
|
|
extract_image_info!
|
2017-05-11 06:16:57 +08:00
|
|
|
else
|
2018-03-28 16:20:08 +08:00
|
|
|
jpeg_tempfile&.close
|
2017-05-11 06:16:57 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-26 04:00:04 +08:00
|
|
|
def execute_convert(from, to, debug = false)
|
|
|
|
command = [
|
|
|
|
"convert",
|
|
|
|
from,
|
|
|
|
"-auto-orient",
|
2018-07-26 05:08:02 +08:00
|
|
|
"-background", "white",
|
|
|
|
"-interlace", "none",
|
2018-07-26 04:00:04 +08:00
|
|
|
"-flatten",
|
2018-07-26 05:08:02 +08:00
|
|
|
"-quality", SiteSetting.png_to_jpg_quality.to_s
|
2018-07-26 04:00:04 +08:00
|
|
|
]
|
2018-07-26 05:08:02 +08:00
|
|
|
command << "-debug" << "all" if debug
|
2018-07-26 04:00:04 +08:00
|
|
|
command << to
|
2017-12-19 18:31:18 +08:00
|
|
|
|
2017-12-27 23:33:25 +08:00
|
|
|
Discourse::Utils.execute_command(*command, failure_message: I18n.t("upload.png_to_jpg_conversion_failure_message"))
|
2017-12-19 18:31:18 +08:00
|
|
|
end
|
|
|
|
|
2017-05-11 06:16:57 +08:00
|
|
|
def should_downsize?
|
|
|
|
max_image_size > 0 && filesize >= max_image_size
|
|
|
|
end
|
|
|
|
|
|
|
|
def downsize!
|
|
|
|
3.times do
|
|
|
|
original_size = filesize
|
|
|
|
downsized_pixels = [pixels, max_image_pixels].min / 2
|
|
|
|
OptimizedImage.downsize(@file.path, @file.path, "#{downsized_pixels}@", filename: @filename, allow_animation: allow_animation)
|
|
|
|
extract_image_info!
|
|
|
|
return if filesize >= original_size || pixels == 0 || !should_downsize?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def is_still_too_big?
|
|
|
|
if max_image_pixels > 0 && pixels >= max_image_pixels
|
|
|
|
@upload.errors.add(:base, I18n.t("upload.images.larger_than_x_megapixels", max_image_megapixels: SiteSetting.max_image_megapixels))
|
|
|
|
true
|
|
|
|
elsif max_image_size > 0 && filesize >= max_image_size
|
|
|
|
@upload.errors.add(:base, I18n.t("upload.images.too_large", max_size_kb: SiteSetting.max_image_size_kb))
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def whitelist_svg!
|
|
|
|
doc = Nokogiri::XML(@file)
|
|
|
|
doc.xpath(svg_whitelist_xpath).remove
|
|
|
|
File.write(@file.path, doc.to_s)
|
|
|
|
@file.rewind
|
|
|
|
end
|
|
|
|
|
2017-07-10 22:35:23 +08:00
|
|
|
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!
|
2018-07-26 04:00:04 +08:00
|
|
|
path = @file.path
|
|
|
|
|
|
|
|
OptimizedImage.ensure_safe_paths!(path)
|
2018-07-26 05:55:06 +08:00
|
|
|
path = OptimizedImage.prepend_decoder!(path)
|
2018-07-26 04:00:04 +08:00
|
|
|
|
|
|
|
Discourse::Utils.execute_command('convert', path, '-auto-orient', path)
|
|
|
|
|
2017-07-10 22:35:23 +08:00
|
|
|
extract_image_info!
|
|
|
|
end
|
|
|
|
|
2017-05-11 06:16:57 +08:00
|
|
|
def should_crop?
|
2017-05-18 18:13:13 +08:00
|
|
|
TYPES_TO_CROP.include?(@opts[:type])
|
2017-05-11 06:16:57 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def crop!
|
|
|
|
max_pixel_ratio = Discourse::PIXEL_RATIOS.max
|
|
|
|
|
|
|
|
case @opts[:type]
|
|
|
|
when "avatar"
|
|
|
|
width = height = Discourse.avatar_sizes.max
|
|
|
|
OptimizedImage.resize(@file.path, @file.path, width, height, filename: @filename, allow_animation: allow_animation)
|
|
|
|
when "profile_background"
|
|
|
|
max_width = 850 * max_pixel_ratio
|
|
|
|
width, height = ImageSizer.resize(@image_info.size[0], @image_info.size[1], max_width: max_width, max_height: max_width)
|
2018-07-17 15:48:59 +08:00
|
|
|
OptimizedImage.downsize(@file.path, @file.path, "#{width}x#{height}\>", filename: @filename, allow_animation: allow_animation)
|
2017-05-11 06:16:57 +08:00
|
|
|
when "card_background"
|
|
|
|
max_width = 590 * max_pixel_ratio
|
|
|
|
width, height = ImageSizer.resize(@image_info.size[0], @image_info.size[1], max_width: max_width, max_height: max_width)
|
2018-07-17 15:48:59 +08:00
|
|
|
OptimizedImage.downsize(@file.path, @file.path, "#{width}x#{height}\>", filename: @filename, allow_animation: allow_animation)
|
2017-05-11 06:16:57 +08:00
|
|
|
when "custom_emoji"
|
2018-07-17 15:48:59 +08:00
|
|
|
OptimizedImage.downsize(@file.path, @file.path, "100x100\>", filename: @filename, allow_animation: allow_animation)
|
2017-05-11 06:16:57 +08:00
|
|
|
end
|
|
|
|
|
2017-06-22 22:53:49 +08:00
|
|
|
extract_image_info!
|
2017-05-11 06:16:57 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def should_optimize?
|
|
|
|
# GIF is too slow (plus, we'll soon be converting them to MP4)
|
|
|
|
# Optimizing SVG is useless
|
|
|
|
return false if @file.path =~ /\.(gif|svg)$/i
|
|
|
|
# Safeguard for large PNGs
|
|
|
|
return pixels < 2_000_000 if @file.path =~ /\.png/i
|
|
|
|
# Everything else is fine!
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def optimize!
|
|
|
|
OptimizedImage.ensure_safe_paths!(@file.path)
|
2017-07-25 17:48:39 +08:00
|
|
|
FileHelper.optimize_image!(@file.path)
|
2017-06-22 22:53:49 +08:00
|
|
|
extract_image_info!
|
2018-07-09 10:30:18 +08:00
|
|
|
rescue ImageOptim::TimeoutExceeded
|
2017-05-11 06:16:57 +08:00
|
|
|
Rails.logger.warn("ImageOptim timed out while optimizing #{@filename}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def filesize
|
|
|
|
File.size?(@file.path).to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
def max_image_size
|
2017-05-11 16:03:28 +08:00
|
|
|
@max_image_size ||= SiteSetting.max_image_size_kb.kilobytes
|
2017-05-11 06:16:57 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def max_image_pixels
|
2017-05-11 16:03:28 +08:00
|
|
|
@max_image_pixels ||= SiteSetting.max_image_megapixels * 1_000_000
|
2017-05-11 06:16:57 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def pixels
|
|
|
|
@image_info.size&.reduce(:*).to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
def allow_animation
|
2017-05-11 16:03:28 +08:00
|
|
|
@allow_animation ||= @opts[:type] == "avatar" ? SiteSetting.allow_animated_avatars : SiteSetting.allow_animated_thumbnails
|
2017-05-11 06:16:57 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def svg_whitelist_xpath
|
|
|
|
@@svg_whitelist_xpath ||= "//*[#{WHITELISTED_SVG_ELEMENTS.map { |e| "name()!='#{e}'" }.join(" and ") }]"
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|