2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-11 06:16:57 +08:00
|
|
|
require "fastimage"
|
|
|
|
|
|
|
|
class UploadCreator
|
|
|
|
|
2017-05-18 18:13:13 +08:00
|
|
|
TYPES_TO_CROP ||= %w{avatar card_background custom_emoji profile_background}.each(&:freeze)
|
|
|
|
|
2020-09-28 11:52:05 +08:00
|
|
|
ALLOWED_SVG_ELEMENTS ||= %w{
|
2020-12-08 08:16:41 +08:00
|
|
|
circle clipPath defs ellipse feGaussianBlur filter g line linearGradient
|
2020-09-19 02:13:07 +08:00
|
|
|
marker path polygon polyline radialGradient rect stop style svg text
|
2020-12-08 08:16:41 +08:00
|
|
|
textPath tref tspan use
|
2017-05-18 18:13:13 +08:00
|
|
|
}.each(&:freeze)
|
2017-05-11 06:16:57 +08:00
|
|
|
|
2020-09-28 11:52:05 +08:00
|
|
|
include ActiveSupport::Deprecation::DeprecatedConstantAccessor
|
|
|
|
deprecate_constant 'WHITELISTED_SVG_ELEMENTS', 'UploadCreator::ALLOWED_SVG_ELEMENTS'
|
|
|
|
|
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)
|
2019-07-31 11:16:03 +08:00
|
|
|
# - for_gravatar (boolean)
|
2017-05-11 06:16:57 +08:00
|
|
|
def initialize(file, filename, opts = {})
|
|
|
|
@file = file
|
2018-08-22 00:11:01 +08:00
|
|
|
@filename = (filename || "").gsub(/[^[:print:]]/, "")
|
|
|
|
@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
|
|
|
|
|
2020-06-11 22:06:48 +08:00
|
|
|
@image_info = FastImage.new(@file) rescue nil
|
|
|
|
is_image = FileHelper.is_supported_image?(@filename)
|
|
|
|
is_image ||= @image_info && FileHelper.is_supported_image?("test.#{@image_info.type}")
|
|
|
|
is_image = false if @opts[:for_theme]
|
2020-06-11 03:30:53 +08:00
|
|
|
|
2020-06-11 22:06:48 +08:00
|
|
|
DistributedMutex.synchronize("upload_#{user_id}_#{@filename}") do
|
2020-07-23 09:40:09 +08:00
|
|
|
# We need to convert HEIFs early because FastImage does not consider them as images
|
|
|
|
if convert_heif_to_jpeg?
|
|
|
|
convert_heif!
|
|
|
|
is_image = FileHelper.is_supported_image?("test.#{@image_info.type}")
|
|
|
|
end
|
|
|
|
|
2018-08-20 10:18:49 +08:00
|
|
|
if is_image
|
2017-05-11 06:16:57 +08:00
|
|
|
extract_image_info!
|
|
|
|
return @upload if @upload.errors.present?
|
|
|
|
|
2018-11-07 12:29:14 +08:00
|
|
|
if @image_info.type.to_s == "svg"
|
2020-07-27 08:23:54 +08:00
|
|
|
clean_svg!
|
2018-08-17 11:41:30 +08:00
|
|
|
elsif !Rails.env.test? || @opts[:force_optimize]
|
2020-10-24 00:38:28 +08:00
|
|
|
convert_to_jpeg! if convert_png_to_jpeg? || should_alter_quality?
|
2017-05-11 06:16:57 +08:00
|
|
|
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
|
2018-08-17 11:41:30 +08:00
|
|
|
|
2018-08-20 10:18:49 +08:00
|
|
|
# conversion may have switched the type
|
2018-08-17 11:41:30 +08:00
|
|
|
image_type = @image_info.type.to_s
|
2017-05-11 06:16:57 +08:00
|
|
|
end
|
|
|
|
|
2020-01-16 11:50:27 +08:00
|
|
|
# compute the sha of the file and generate a unique hash
|
|
|
|
# which is only used for secure uploads
|
2017-05-11 06:16:57 +08:00
|
|
|
sha1 = Upload.generate_digest(@file)
|
2020-01-16 11:50:27 +08:00
|
|
|
unique_hash = SecureRandom.hex(20) if SiteSetting.secure_media
|
2017-05-11 06:16:57 +08:00
|
|
|
|
2020-01-16 11:50:27 +08:00
|
|
|
# we do not check for duplicate uploads if secure media is
|
|
|
|
# enabled because we use a unique access hash to differentiate
|
|
|
|
# between uploads instead of the sha1, and to get around various
|
|
|
|
# access/permission issues for uploads
|
|
|
|
if !SiteSetting.secure_media
|
|
|
|
# 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
|
2017-05-11 06:16:57 +08:00
|
|
|
|
2020-01-16 11:50:27 +08:00
|
|
|
# return the previous upload if any
|
|
|
|
if @upload
|
2020-07-04 01:16:54 +08:00
|
|
|
add_metadata!
|
2020-01-16 11:50:27 +08:00
|
|
|
UserUpload.find_or_create_by!(user_id: user_id, upload_id: @upload.id) if user_id
|
|
|
|
return @upload
|
|
|
|
end
|
2018-09-20 13:33:10 +08:00
|
|
|
end
|
2017-05-11 06:16:57 +08:00
|
|
|
|
2018-08-20 10:18:49 +08:00
|
|
|
fixed_original_filename = nil
|
2018-08-20 14:08:05 +08:00
|
|
|
|
2019-01-04 22:30:17 +08:00
|
|
|
if is_image
|
2018-08-20 14:08:05 +08:00
|
|
|
current_extension = File.extname(@filename).downcase.sub("jpeg", "jpg")
|
|
|
|
expected_extension = ".#{image_type}".downcase.sub("jpeg", "jpg")
|
|
|
|
|
2018-08-20 10:18:49 +08:00
|
|
|
# we have to correct original filename here, no choice
|
|
|
|
# otherwise validation will fail and we can not save
|
|
|
|
# TODO decide if we only run the validation on the extension
|
2018-08-20 14:08:05 +08:00
|
|
|
if current_extension != expected_extension
|
2019-01-04 22:30:17 +08:00
|
|
|
basename = File.basename(@filename, current_extension).presence || "image"
|
2018-08-20 14:08:05 +08:00
|
|
|
fixed_original_filename = "#{basename}#{expected_extension}"
|
2018-08-20 10:18:49 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-11 06:16:57 +08:00
|
|
|
# create the upload otherwise
|
|
|
|
@upload = Upload.new
|
|
|
|
@upload.user_id = user_id
|
2018-08-20 10:18:49 +08:00
|
|
|
@upload.original_filename = fixed_original_filename || @filename
|
2017-05-11 06:16:57 +08:00
|
|
|
@upload.filesize = filesize
|
2020-01-16 11:50:27 +08:00
|
|
|
@upload.sha1 = SiteSetting.secure_media? ? unique_hash : sha1
|
|
|
|
@upload.original_sha1 = SiteSetting.secure_media? ? sha1 : nil
|
2017-05-11 06:16:57 +08:00
|
|
|
@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
|
|
|
|
2018-08-20 10:18:49 +08:00
|
|
|
if is_image
|
2021-03-02 00:44:00 +08:00
|
|
|
if @image_info.type.to_s == 'svg'
|
|
|
|
w, h = Discourse::Utils.execute_command("identify", "-format", "%w %h", @file.path).split(' ') rescue [0, 0]
|
|
|
|
else
|
|
|
|
w, h = @image_info.size
|
|
|
|
end
|
|
|
|
|
|
|
|
@upload.thumbnail_width, @upload.thumbnail_height = ImageSizer.resize(w, h)
|
|
|
|
@upload.width, @upload.height = w, h
|
2021-01-14 01:01:30 +08:00
|
|
|
@upload.animated = animated?
|
2017-05-11 06:16:57 +08:00
|
|
|
end
|
|
|
|
|
2020-07-04 01:16:54 +08:00
|
|
|
add_metadata!
|
2021-01-29 07:03:44 +08:00
|
|
|
|
|
|
|
if SiteSetting.secure_media
|
|
|
|
secure, reason = UploadSecurity.new(@upload, @opts.merge(creating: true)).should_be_secure_with_reason
|
|
|
|
attrs = @upload.secure_params(secure, reason, "upload creator")
|
|
|
|
@upload.assign_attributes(attrs)
|
|
|
|
end
|
|
|
|
|
2017-05-11 06:16:57 +08:00
|
|
|
return @upload unless @upload.save
|
|
|
|
|
2020-09-05 04:24:40 +08:00
|
|
|
DiscourseEvent.trigger(:before_upload_creation, @file, is_image, @opts[:for_export])
|
2020-07-21 04:59:37 +08:00
|
|
|
|
2017-05-11 06:16:57 +08:00
|
|
|
# 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?
|
2019-01-04 14:16:22 +08:00
|
|
|
@upload.url = url
|
|
|
|
@upload.save!
|
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
|
|
|
|
|
2018-11-07 12:29:14 +08:00
|
|
|
if @upload.errors.empty? && is_image && @opts[:type] == "avatar" && @upload.extension != "svg"
|
2019-05-02 16:08:12 +08:00
|
|
|
Jobs.enqueue(:create_avatar_thumbnails, upload_id: @upload.id)
|
2017-05-11 06:16:57 +08:00
|
|
|
end
|
|
|
|
|
2018-09-20 13:33:10 +08:00
|
|
|
if @upload.errors.empty?
|
|
|
|
UserUpload.find_or_create_by!(user_id: user_id, upload_id: @upload.id) if user_id
|
|
|
|
end
|
|
|
|
|
2017-05-11 06:16:57 +08:00
|
|
|
@upload
|
|
|
|
end
|
|
|
|
ensure
|
2019-10-11 17:13:10 +08:00
|
|
|
if @file
|
|
|
|
@file.respond_to?(:close!) ? @file.close! : @file.close
|
|
|
|
end
|
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"))
|
2020-09-14 08:10:55 +08:00
|
|
|
elsif max_image_pixels > 0 && pixels >= max_image_pixels * 2
|
|
|
|
@upload.errors.add(:base, I18n.t("upload.images.larger_than_x_megapixels", max_image_megapixels: SiteSetting.max_image_megapixels * 2))
|
2017-05-11 06:16:57 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-02 05:12:37 +08:00
|
|
|
MIN_PIXELS_TO_CONVERT_TO_JPEG ||= 1280 * 720
|
|
|
|
|
2019-02-11 13:28:43 +08:00
|
|
|
def convert_png_to_jpeg?
|
|
|
|
return false unless @image_info.type == :png
|
2017-06-23 18:13:48 +08:00
|
|
|
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
|
|
|
|
|
2018-11-21 08:00:52 +08:00
|
|
|
MIN_CONVERT_TO_JPEG_BYTES_SAVED = 75_000
|
|
|
|
MIN_CONVERT_TO_JPEG_SAVING_RATIO = 0.70
|
|
|
|
|
2017-05-11 06:16:57 +08:00
|
|
|
def convert_to_jpeg!
|
2021-03-16 22:44:41 +08:00
|
|
|
return if @opts[:for_site_setting]
|
2019-01-04 22:30:17 +08:00
|
|
|
return if filesize < MIN_CONVERT_TO_JPEG_BYTES_SAVED
|
2018-11-21 08:00:52 +08:00
|
|
|
|
2017-05-11 06:16:57 +08:00
|
|
|
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-08-20 10:18:49 +08:00
|
|
|
from = OptimizedImage.prepend_decoder!(from, nil, filename: "image.#{@image_info.type}")
|
2018-07-26 05:55:06 +08:00
|
|
|
to = OptimizedImage.prepend_decoder!(to)
|
2017-12-19 18:31:18 +08:00
|
|
|
|
2020-10-24 00:38:28 +08:00
|
|
|
opts = {}
|
|
|
|
desired_quality = [SiteSetting.png_to_jpg_quality, SiteSetting.recompress_original_jpg_quality].compact.min
|
|
|
|
target_quality = @upload.target_image_quality(from, desired_quality)
|
|
|
|
opts = { quality: target_quality } if target_quality
|
|
|
|
|
2017-12-19 18:31:18 +08:00
|
|
|
begin
|
2020-10-24 00:38:28 +08:00
|
|
|
execute_convert(from, to, opts)
|
2017-12-19 18:31:18 +08:00
|
|
|
rescue
|
|
|
|
# retry with debugging enabled
|
2020-10-24 00:38:28 +08:00
|
|
|
execute_convert(from, to, opts.merge(debug: true))
|
2017-12-19 18:31:18 +08:00
|
|
|
end
|
2017-05-11 06:16:57 +08:00
|
|
|
|
2018-11-21 08:00:52 +08:00
|
|
|
new_size = File.size(jpeg_tempfile.path)
|
|
|
|
|
|
|
|
keep_jpeg = new_size < filesize * MIN_CONVERT_TO_JPEG_SAVING_RATIO
|
|
|
|
keep_jpeg &&= (filesize - new_size) > MIN_CONVERT_TO_JPEG_BYTES_SAVED
|
|
|
|
|
|
|
|
if keep_jpeg
|
2019-10-11 17:13:10 +08:00
|
|
|
@file.respond_to?(:close!) ? @file.close! : @file.close
|
2017-05-11 06:16:57 +08:00
|
|
|
@file = jpeg_tempfile
|
2017-06-22 22:53:49 +08:00
|
|
|
extract_image_info!
|
2017-05-11 06:16:57 +08:00
|
|
|
else
|
2019-10-11 17:13:10 +08:00
|
|
|
jpeg_tempfile.close!
|
2017-05-11 06:16:57 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-23 09:40:09 +08:00
|
|
|
def convert_heif_to_jpeg?
|
2020-08-07 23:17:50 +08:00
|
|
|
File.extname(@filename).downcase.match?(/\.hei(f|c)$/)
|
2020-07-23 09:40:09 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def convert_heif!
|
|
|
|
jpeg_tempfile = Tempfile.new(["image", ".jpg"])
|
|
|
|
from = @file.path
|
|
|
|
to = jpeg_tempfile.path
|
|
|
|
OptimizedImage.ensure_safe_paths!(from, to)
|
|
|
|
|
|
|
|
begin
|
|
|
|
execute_convert(from, to)
|
|
|
|
rescue
|
|
|
|
# retry with debugging enabled
|
2020-10-24 00:38:28 +08:00
|
|
|
execute_convert(from, to, { debug: true })
|
2020-07-23 09:40:09 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
@file.respond_to?(:close!) ? @file.close! : @file.close
|
|
|
|
@file = jpeg_tempfile
|
|
|
|
extract_image_info!
|
|
|
|
end
|
|
|
|
|
2020-10-24 00:38:28 +08:00
|
|
|
def execute_convert(from, to, opts = {})
|
2018-07-26 04:00:04 +08:00
|
|
|
command = [
|
|
|
|
"convert",
|
|
|
|
from,
|
|
|
|
"-auto-orient",
|
2018-07-26 05:08:02 +08:00
|
|
|
"-background", "white",
|
|
|
|
"-interlace", "none",
|
2020-10-24 00:38:28 +08:00
|
|
|
"-flatten"
|
2018-07-26 04:00:04 +08:00
|
|
|
]
|
2020-10-24 00:38:28 +08:00
|
|
|
command << "-debug" << "all" if opts[:debug]
|
|
|
|
command << "-quality" << opts[:quality].to_s if opts[:quality]
|
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
|
|
|
|
|
2020-10-24 00:38:28 +08:00
|
|
|
def should_alter_quality?
|
2021-01-14 01:01:30 +08:00
|
|
|
return false if animated?
|
2020-10-27 03:10:19 +08:00
|
|
|
|
2021-02-13 02:37:35 +08:00
|
|
|
desired_quality = @image_info.type == :png ? SiteSetting.png_to_jpg_quality : SiteSetting.recompress_original_jpg_quality
|
|
|
|
@upload.target_image_quality(@file.path, desired_quality).present?
|
2020-10-24 00:38:28 +08:00
|
|
|
end
|
|
|
|
|
2017-05-11 06:16:57 +08:00
|
|
|
def should_downsize?
|
2021-01-14 01:01:30 +08:00
|
|
|
max_image_size > 0 && filesize >= max_image_size && !animated?
|
2017-05-11 06:16:57 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def downsize!
|
|
|
|
3.times do
|
|
|
|
original_size = filesize
|
2019-10-04 22:22:57 +08:00
|
|
|
down_tempfile = Tempfile.new(["down", ".#{@image_info.type}"])
|
2018-08-17 01:20:07 +08:00
|
|
|
|
2019-10-11 17:13:10 +08:00
|
|
|
from = @file.path
|
|
|
|
to = down_tempfile.path
|
|
|
|
|
|
|
|
OptimizedImage.ensure_safe_paths!(from, to)
|
|
|
|
|
2018-08-17 01:20:07 +08:00
|
|
|
OptimizedImage.downsize(
|
2019-10-11 17:13:10 +08:00
|
|
|
from,
|
|
|
|
to,
|
2021-01-12 23:07:07 +08:00
|
|
|
"50%",
|
2020-04-10 20:46:47 +08:00
|
|
|
scale_image: true,
|
2018-08-17 01:20:07 +08:00
|
|
|
raise_on_error: true
|
|
|
|
)
|
|
|
|
|
2019-10-11 17:13:10 +08:00
|
|
|
@file.respond_to?(:close!) ? @file.close! : @file.close
|
2019-10-04 22:22:57 +08:00
|
|
|
@file = down_tempfile
|
|
|
|
|
2017-05-11 06:16:57 +08:00
|
|
|
extract_image_info!
|
2018-08-17 01:20:07 +08:00
|
|
|
|
2017-05-11 06:16:57 +08:00
|
|
|
return if filesize >= original_size || pixels == 0 || !should_downsize?
|
|
|
|
end
|
2020-03-25 18:59:16 +08:00
|
|
|
rescue
|
|
|
|
@upload.errors.add(:base, I18n.t("upload.optimize_failure_message"))
|
2017-05-11 06:16:57 +08:00
|
|
|
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
|
|
|
|
|
2020-07-27 08:23:54 +08:00
|
|
|
def clean_svg!
|
2017-05-11 06:16:57 +08:00
|
|
|
doc = Nokogiri::XML(@file)
|
2020-07-27 08:23:54 +08:00
|
|
|
doc.xpath(svg_allowlist_xpath).remove
|
2019-12-11 22:28:35 +08:00
|
|
|
doc.xpath("//@*[starts-with(name(), 'on')]").remove
|
2020-07-09 11:31:48 +08:00
|
|
|
doc.css('use').each do |use_el|
|
|
|
|
if use_el.attr('href')
|
|
|
|
use_el.remove_attribute('href') unless use_el.attr('href').starts_with?('#')
|
|
|
|
end
|
|
|
|
if use_el.attr('xlink:href')
|
|
|
|
use_el.remove_attribute('xlink:href') unless use_el.attr('xlink:href').starts_with?('#')
|
|
|
|
end
|
|
|
|
end
|
2017-05-11 06:16:57 +08:00
|
|
|
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-08-20 10:18:49 +08:00
|
|
|
path = OptimizedImage.prepend_decoder!(path, nil, filename: "image.#{@image_info.type}")
|
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?
|
2021-01-14 01:01:30 +08:00
|
|
|
return false if ['profile_background', 'card_background', 'custom_emoji'].include?(@opts[:type]) && animated?
|
2020-11-12 08:22:38 +08:00
|
|
|
|
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
|
2018-08-20 10:18:49 +08:00
|
|
|
filename_with_correct_ext = "image.#{@image_info.type}"
|
|
|
|
|
2017-05-11 06:16:57 +08:00
|
|
|
case @opts[:type]
|
|
|
|
when "avatar"
|
|
|
|
width = height = Discourse.avatar_sizes.max
|
2020-10-16 18:41:27 +08:00
|
|
|
OptimizedImage.resize(@file.path, @file.path, width, height, filename: filename_with_correct_ext)
|
2017-05-11 06:16:57 +08:00
|
|
|
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)
|
2020-10-16 18:41:27 +08:00
|
|
|
OptimizedImage.downsize(@file.path, @file.path, "#{width}x#{height}\>", filename: filename_with_correct_ext)
|
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)
|
2020-10-16 18:41:27 +08:00
|
|
|
OptimizedImage.downsize(@file.path, @file.path, "#{width}x#{height}\>", filename: filename_with_correct_ext)
|
2017-05-11 06:16:57 +08:00
|
|
|
when "custom_emoji"
|
2020-10-16 18:41:27 +08:00
|
|
|
OptimizedImage.downsize(@file.path, @file.path, "100x100\>", filename: filename_with_correct_ext)
|
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
|
|
|
|
|
2020-07-27 08:23:54 +08:00
|
|
|
def svg_allowlist_xpath
|
2020-09-28 11:52:05 +08:00
|
|
|
@@svg_allowlist_xpath ||= "//*[#{ALLOWED_SVG_ELEMENTS.map { |e| "name()!='#{e}'" }.join(" and ") }]"
|
2017-05-11 06:16:57 +08:00
|
|
|
end
|
|
|
|
|
2020-07-04 01:16:54 +08:00
|
|
|
def add_metadata!
|
|
|
|
@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]
|
|
|
|
@upload.for_export = true if @opts[:for_export]
|
|
|
|
@upload.for_site_setting = true if @opts[:for_site_setting]
|
|
|
|
@upload.for_gravatar = true if @opts[:for_gravatar]
|
|
|
|
end
|
|
|
|
|
2021-01-14 01:01:30 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def animated?
|
|
|
|
return @animated if @animated != nil
|
|
|
|
|
|
|
|
@animated ||= begin
|
|
|
|
is_animated = FastImage.animated?(@file)
|
|
|
|
type = @image_info.type.to_s
|
|
|
|
|
|
|
|
if is_animated != nil
|
|
|
|
# FastImage will return nil if it cannot determine if animated
|
|
|
|
is_animated
|
|
|
|
elsif type == "gif" || type == "webp"
|
|
|
|
# Only GIFs, WEBPs and a few other unsupported image types can be animated
|
|
|
|
OptimizedImage.ensure_safe_paths!(@file.path)
|
|
|
|
|
|
|
|
command = ["identify", "-format", "%n\\n", @file.path]
|
|
|
|
frames = Discourse::Utils.execute_command(*command).to_i rescue 1
|
|
|
|
|
|
|
|
frames > 1
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-11 06:16:57 +08:00
|
|
|
end
|