2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-11-06 02:04:47 +08:00
|
|
|
module Jobs
|
|
|
|
|
2019-10-02 12:01:53 +08:00
|
|
|
class PullHotlinkedImages < ::Jobs::Base
|
2016-04-07 10:56:43 +08:00
|
|
|
sidekiq_options queue: 'low'
|
|
|
|
|
2013-11-06 02:04:47 +08:00
|
|
|
def initialize
|
2013-11-14 00:30:48 +08:00
|
|
|
@max_size = SiteSetting.max_image_size_kb.kilobytes
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
|
|
|
|
2020-08-05 19:14:59 +08:00
|
|
|
def execute(args)
|
|
|
|
@post_id = args[:post_id]
|
|
|
|
raise Discourse::InvalidParameters.new(:post_id) if @post_id.blank?
|
|
|
|
|
|
|
|
post = Post.find_by(id: @post_id)
|
|
|
|
return if post.blank?
|
2021-02-03 13:45:07 +08:00
|
|
|
return if post.topic.blank?
|
2020-08-05 19:14:59 +08:00
|
|
|
|
|
|
|
raw = post.raw.dup
|
|
|
|
start_raw = raw.dup
|
|
|
|
|
|
|
|
large_image_urls = post.custom_fields[Post::LARGE_IMAGES] || []
|
|
|
|
broken_image_urls = post.custom_fields[Post::BROKEN_IMAGES] || []
|
|
|
|
downloaded_image_ids = post.custom_fields[Post::DOWNLOADED_IMAGES] || {}
|
|
|
|
|
|
|
|
upload_records = Upload.where(id: downloaded_image_ids.values)
|
|
|
|
upload_records = Hash[upload_records.map { |u| [u.id, u] }]
|
|
|
|
|
|
|
|
downloaded_images = {}
|
|
|
|
downloaded_image_ids.each { |url, id| downloaded_images[url] = upload_records[id] }
|
|
|
|
|
|
|
|
extract_images_from(post.cooked).each do |node|
|
|
|
|
download_src = original_src = node['src'] || node['href']
|
|
|
|
download_src = "#{SiteSetting.force_https ? "https" : "http"}:#{original_src}" if original_src.start_with?("//")
|
|
|
|
normalized_src = normalize_src(download_src)
|
|
|
|
|
|
|
|
next if !should_download_image?(download_src, post)
|
|
|
|
|
|
|
|
begin
|
|
|
|
already_attempted_download = downloaded_images.include?(normalized_src) || large_image_urls.include?(normalized_src) || broken_image_urls.include?(normalized_src)
|
|
|
|
if !already_attempted_download
|
|
|
|
downloaded_images[normalized_src] = attempt_download(download_src, post.user_id)
|
|
|
|
end
|
|
|
|
rescue ImageTooLargeError
|
|
|
|
large_image_urls << normalized_src
|
|
|
|
rescue ImageBrokenError
|
|
|
|
broken_image_urls << normalized_src
|
|
|
|
end
|
|
|
|
|
|
|
|
# have we successfully downloaded that file?
|
|
|
|
if upload = downloaded_images[normalized_src]
|
|
|
|
raw = replace_in_raw(original_src: original_src, upload: upload, raw: raw)
|
|
|
|
end
|
|
|
|
rescue => e
|
|
|
|
raise e if Rails.env.test?
|
|
|
|
log(:error, "Failed to pull hotlinked image (#{download_src}) post: #{@post_id}\n" + e.message + "\n" + e.backtrace.join("\n"))
|
|
|
|
end
|
|
|
|
|
|
|
|
large_image_urls.uniq!
|
|
|
|
broken_image_urls.uniq!
|
|
|
|
downloaded_images.compact!
|
|
|
|
|
|
|
|
post.custom_fields[Post::LARGE_IMAGES] = large_image_urls
|
|
|
|
post.custom_fields[Post::BROKEN_IMAGES] = broken_image_urls
|
|
|
|
|
|
|
|
downloaded_image_ids = {}
|
|
|
|
downloaded_images.each { |url, upload| downloaded_image_ids[url] = upload.id }
|
|
|
|
post.custom_fields[Post::DOWNLOADED_IMAGES] = downloaded_image_ids
|
|
|
|
|
|
|
|
[Post::LARGE_IMAGES, Post::BROKEN_IMAGES, Post::DOWNLOADED_IMAGES].each do |key|
|
|
|
|
post.custom_fields.delete(key) if !post.custom_fields[key].present?
|
|
|
|
end
|
|
|
|
|
|
|
|
custom_fields_updated = !post.custom_fields_clean?
|
|
|
|
|
|
|
|
# only save custom fields if they changed
|
|
|
|
post.save_custom_fields if custom_fields_updated
|
|
|
|
|
|
|
|
# If post changed while we were downloading images, never apply edits
|
|
|
|
post.reload
|
|
|
|
post_changed_elsewhere = (start_raw != post.raw)
|
|
|
|
raw_changed_here = (raw != post.raw)
|
|
|
|
|
|
|
|
if !post_changed_elsewhere && raw_changed_here
|
|
|
|
changes = { raw: raw, edit_reason: I18n.t("upload.edit_reason") }
|
|
|
|
post.revise(Discourse.system_user, changes, bypass_bump: true, skip_staff_log: true)
|
|
|
|
elsif custom_fields_updated
|
|
|
|
post.trigger_post_process(
|
|
|
|
bypass_bump: true,
|
|
|
|
skip_pull_hotlinked_images: true # Avoid an infinite loop of job scheduling
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-12 05:11:44 +08:00
|
|
|
def download(src)
|
|
|
|
downloaded = nil
|
|
|
|
|
|
|
|
begin
|
|
|
|
retries ||= 3
|
|
|
|
|
|
|
|
downloaded = FileHelper.download(
|
|
|
|
src,
|
|
|
|
max_file_size: @max_size,
|
2018-08-17 16:52:55 +08:00
|
|
|
retain_on_max_file_size_exceeded: true,
|
2017-10-12 05:11:44 +08:00
|
|
|
tmp_file_name: "discourse-hotlinked",
|
|
|
|
follow_redirect: true
|
|
|
|
)
|
|
|
|
rescue
|
2018-03-28 12:54:11 +08:00
|
|
|
if (retries -= 1) > 0 && !Rails.env.test?
|
2017-10-12 05:11:44 +08:00
|
|
|
sleep 1
|
|
|
|
retry
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
downloaded
|
|
|
|
end
|
|
|
|
|
2020-08-05 19:14:59 +08:00
|
|
|
class ImageTooLargeError < StandardError; end
|
|
|
|
class ImageBrokenError < StandardError; end
|
2013-11-06 02:04:47 +08:00
|
|
|
|
2020-08-05 19:14:59 +08:00
|
|
|
def attempt_download(src, user_id)
|
|
|
|
# secure-media-uploads endpoint prevents anonymous downloads, so we
|
|
|
|
# need the presigned S3 URL here
|
|
|
|
src = Upload.signed_url_from_secure_media_url(src) if Upload.secure_media_url?(src)
|
2013-11-06 02:04:47 +08:00
|
|
|
|
2020-08-05 19:14:59 +08:00
|
|
|
hotlinked = download(src)
|
|
|
|
raise ImageBrokenError if !hotlinked
|
|
|
|
raise ImageTooLargeError if File.size(hotlinked.path) > @max_size
|
2017-11-16 22:45:07 +08:00
|
|
|
|
2020-08-05 19:14:59 +08:00
|
|
|
filename = File.basename(URI.parse(src).path)
|
|
|
|
filename << File.extname(hotlinked.path) unless filename["."]
|
|
|
|
upload = UploadCreator.new(hotlinked, filename, origin: src).create_for(user_id)
|
2017-11-16 12:13:15 +08:00
|
|
|
|
2020-08-05 19:14:59 +08:00
|
|
|
if upload.persisted?
|
|
|
|
upload
|
|
|
|
else
|
|
|
|
log(:info, "Failed to persist downloaded hotlinked image for post: #{@post_id}: #{src} - #{upload.errors.full_messages.join("\n")}")
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
2013-11-06 02:04:47 +08:00
|
|
|
|
2020-08-05 19:14:59 +08:00
|
|
|
def replace_in_raw(original_src:, raw:, upload:)
|
|
|
|
raw = raw.dup
|
|
|
|
escaped_src = Regexp.escape(original_src)
|
|
|
|
|
|
|
|
replace_raw = ->(match, match_src, replacement, _index) {
|
|
|
|
if normalize_src(original_src) == normalize_src(match_src)
|
|
|
|
replacement =
|
|
|
|
if replacement.include?(InlineUploads::PLACEHOLDER)
|
|
|
|
replacement.sub(InlineUploads::PLACEHOLDER, upload.short_url)
|
|
|
|
elsif replacement.include?(InlineUploads::PATH_PLACEHOLDER)
|
|
|
|
replacement.sub(InlineUploads::PATH_PLACEHOLDER, upload.short_path)
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
2020-01-29 08:11:38 +08:00
|
|
|
|
2020-08-05 19:14:59 +08:00
|
|
|
raw = raw.gsub(
|
|
|
|
match,
|
|
|
|
replacement
|
|
|
|
)
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
2020-08-05 19:14:59 +08:00
|
|
|
}
|
2013-11-06 02:04:47 +08:00
|
|
|
|
2020-08-05 19:14:59 +08:00
|
|
|
# there are 6 ways to insert an image in a post
|
|
|
|
# HTML tag - <img src="http://...">
|
|
|
|
InlineUploads.match_img(raw, external_src: true, &replace_raw)
|
2017-11-16 22:45:07 +08:00
|
|
|
|
2020-08-05 19:14:59 +08:00
|
|
|
# BBCode tag - [img]http://...[/img]
|
|
|
|
InlineUploads.match_bbcode_img(raw, external_src: true, &replace_raw)
|
2017-11-16 12:13:15 +08:00
|
|
|
|
2020-08-05 19:14:59 +08:00
|
|
|
# Markdown linked image - [![alt](http://...)](http://...)
|
|
|
|
# Markdown inline - ![alt](http://...)
|
|
|
|
# Markdown inline - ![](http://... "image title")
|
|
|
|
# Markdown inline - ![alt](http://... "image title")
|
|
|
|
InlineUploads.match_md_inline_img(raw, external_src: true, &replace_raw)
|
2017-11-15 18:30:47 +08:00
|
|
|
|
2020-08-05 19:14:59 +08:00
|
|
|
# Direct link
|
|
|
|
raw.gsub!(/^#{escaped_src}(\s?)$/) { "![](#{upload.short_url})#{$1}" }
|
|
|
|
|
|
|
|
raw
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def extract_images_from(html)
|
2020-05-05 11:46:57 +08:00
|
|
|
doc = Nokogiri::HTML5::fragment(html)
|
2019-06-06 15:50:35 +08:00
|
|
|
|
2019-06-14 09:21:25 +08:00
|
|
|
doc.css("img[src], a.lightbox[href]") -
|
2019-06-06 15:50:35 +08:00
|
|
|
doc.css("img.avatar") -
|
|
|
|
doc.css(".lightbox img[src]")
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
|
|
|
|
2020-01-29 08:11:38 +08:00
|
|
|
def should_download_image?(src, post = nil)
|
2014-05-08 01:49:16 +08:00
|
|
|
# make sure we actually have a url
|
|
|
|
return false unless src.present?
|
2019-03-28 04:31:12 +08:00
|
|
|
|
2020-06-20 03:21:05 +08:00
|
|
|
local_bases = [
|
|
|
|
Discourse.base_url,
|
|
|
|
Discourse.asset_host,
|
2021-03-03 03:04:16 +08:00
|
|
|
SiteSetting.external_emoji_url.presence
|
2020-06-20 03:21:05 +08:00
|
|
|
].compact.map { |s| normalize_src(s) }
|
|
|
|
|
|
|
|
if Discourse.store.has_been_uploaded?(src) || normalize_src(src).start_with?(*local_bases) || src =~ /\A\/[^\/]/i
|
|
|
|
return false if !(src =~ /\/uploads\// || Upload.secure_media_url?(src))
|
2019-06-07 20:00:52 +08:00
|
|
|
|
|
|
|
# Someone could hotlink a file from a different site on the same CDN,
|
|
|
|
# so check whether we have it in this database
|
2020-01-29 08:11:38 +08:00
|
|
|
#
|
|
|
|
# if the upload already exists and is attached to a different post,
|
|
|
|
# or the original_sha1 is missing meaning it was created before secure
|
|
|
|
# media was enabled, then we definitely want to redownload again otherwise
|
|
|
|
# we end up reusing existing uploads which may be linked to many posts
|
|
|
|
# already.
|
|
|
|
upload = Upload.consider_for_reuse(Upload.get_from_url(src), post)
|
|
|
|
|
|
|
|
return !upload.present?
|
2019-03-28 04:31:12 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Don't download non-local images unless site setting enabled
|
|
|
|
return false unless SiteSetting.download_remote_images_to_local?
|
2017-07-06 16:55:28 +08:00
|
|
|
|
2014-05-08 01:49:16 +08:00
|
|
|
# parse the src
|
|
|
|
begin
|
|
|
|
uri = URI.parse(src)
|
2018-08-14 18:23:32 +08:00
|
|
|
rescue URI::Error
|
2014-05-08 01:49:16 +08:00
|
|
|
return false
|
|
|
|
end
|
2017-07-06 16:55:28 +08:00
|
|
|
|
|
|
|
hostname = uri.hostname
|
|
|
|
return false unless hostname
|
|
|
|
|
2020-07-27 08:23:54 +08:00
|
|
|
# check the domains blocklist
|
2014-04-22 04:59:53 +08:00
|
|
|
SiteSetting.should_download_images?(src)
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
|
|
|
|
2017-07-05 09:34:24 +08:00
|
|
|
def log(log_level, message)
|
|
|
|
Rails.logger.public_send(
|
|
|
|
log_level,
|
|
|
|
"#{RailsMultisite::ConnectionManagement.current_db}: #{message}"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-03-28 14:44:42 +08:00
|
|
|
private
|
|
|
|
|
2020-05-30 00:47:05 +08:00
|
|
|
def normalize_src(src)
|
|
|
|
uri = Addressable::URI.heuristic_parse(src)
|
|
|
|
uri.normalize!
|
|
|
|
uri.scheme = nil
|
|
|
|
uri.to_s
|
2020-06-19 19:52:51 +08:00
|
|
|
rescue URI::Error, Addressable::URI::InvalidURIError
|
2020-05-30 00:47:05 +08:00
|
|
|
src
|
2018-03-28 14:44:42 +08:00
|
|
|
end
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|