2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
module Jobs
|
|
|
|
|
2019-10-02 00:01:53 -04:00
|
|
|
class PullHotlinkedImages < ::Jobs::Base
|
2016-04-06 22:56:43 -04:00
|
|
|
sidekiq_options queue: 'low'
|
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def initialize
|
2013-11-13 11:30:48 -05:00
|
|
|
@max_size = SiteSetting.max_image_size_kb.kilobytes
|
2013-11-05 13:04:47 -05:00
|
|
|
end
|
|
|
|
|
2017-10-11 17:11:44 -04:00
|
|
|
def download(src)
|
|
|
|
downloaded = nil
|
|
|
|
|
|
|
|
begin
|
|
|
|
retries ||= 3
|
|
|
|
|
|
|
|
downloaded = FileHelper.download(
|
|
|
|
src,
|
|
|
|
max_file_size: @max_size,
|
2018-08-17 04:52:55 -04:00
|
|
|
retain_on_max_file_size_exceeded: true,
|
2017-10-11 17:11:44 -04:00
|
|
|
tmp_file_name: "discourse-hotlinked",
|
|
|
|
follow_redirect: true
|
|
|
|
)
|
|
|
|
rescue
|
2018-03-28 00:54:11 -04:00
|
|
|
if (retries -= 1) > 0 && !Rails.env.test?
|
2017-10-11 17:11:44 -04:00
|
|
|
sleep 1
|
|
|
|
retry
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
downloaded
|
|
|
|
end
|
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
def execute(args)
|
|
|
|
post_id = args[:post_id]
|
|
|
|
raise Discourse::InvalidParameters.new(:post_id) unless post_id.present?
|
|
|
|
|
2014-05-06 09:41:59 -04:00
|
|
|
post = Post.find_by(id: post_id)
|
2013-11-05 13:04:47 -05:00
|
|
|
return unless post.present?
|
|
|
|
|
|
|
|
raw = post.raw.dup
|
2014-04-21 17:08:17 -04:00
|
|
|
start_raw = raw.dup
|
2017-11-16 09:45:07 -05:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
downloaded_urls = {}
|
2017-11-15 23:13:15 -05:00
|
|
|
|
2018-03-28 04:20:08 -04:00
|
|
|
large_images = JSON.parse(post.custom_fields[Post::LARGE_IMAGES].presence || "[]")
|
|
|
|
broken_images = JSON.parse(post.custom_fields[Post::BROKEN_IMAGES].presence || "[]")
|
|
|
|
downloaded_images = JSON.parse(post.custom_fields[Post::DOWNLOADED_IMAGES].presence || "{}")
|
2017-11-15 23:13:15 -05:00
|
|
|
|
2017-11-16 09:45:07 -05:00
|
|
|
has_new_large_image = false
|
|
|
|
has_new_broken_image = false
|
|
|
|
has_downloaded_image = false
|
2013-11-05 13:04:47 -05:00
|
|
|
|
2019-05-23 03:43:25 -04:00
|
|
|
extract_images_from(post.cooked).each do |node|
|
|
|
|
src = original_src = node['src'] || node['href']
|
2017-11-16 09:45:07 -05:00
|
|
|
src = "#{SiteSetting.force_https ? "https" : "http"}:#{src}" if src.start_with?("//")
|
2013-11-05 13:04:47 -05:00
|
|
|
|
2020-01-28 19:11:38 -05:00
|
|
|
if should_download_image?(src, post)
|
2013-11-05 13:04:47 -05:00
|
|
|
begin
|
|
|
|
# have we already downloaded that file?
|
2020-05-29 12:47:05 -04:00
|
|
|
schemeless_src = normalize_src(original_src)
|
2018-03-28 02:44:42 -04:00
|
|
|
|
|
|
|
unless downloaded_images.include?(schemeless_src) || large_images.include?(schemeless_src) || broken_images.include?(schemeless_src)
|
2020-01-23 20:59:30 -05:00
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
2017-10-11 17:11:44 -04:00
|
|
|
if hotlinked = download(src)
|
2015-08-17 12:57:28 -04:00
|
|
|
if File.size(hotlinked.path) <= @max_size
|
2014-09-26 12:27:10 -04:00
|
|
|
filename = File.basename(URI.parse(src).path)
|
2017-06-13 07:27:05 -04:00
|
|
|
filename << File.extname(hotlinked.path) unless filename["."]
|
2017-05-10 18:16:57 -04:00
|
|
|
upload = UploadCreator.new(hotlinked, filename, origin: src).create_for(post.user_id)
|
2019-06-13 21:21:25 -04:00
|
|
|
|
2017-06-13 07:27:05 -04:00
|
|
|
if upload.persisted?
|
|
|
|
downloaded_urls[src] = upload.url
|
2020-05-29 12:47:05 -04:00
|
|
|
downloaded_images[normalize_src(src)] = upload.id
|
2017-11-16 09:45:07 -05:00
|
|
|
has_downloaded_image = true
|
2017-06-13 07:27:05 -04:00
|
|
|
else
|
2017-10-18 17:14:13 -04:00
|
|
|
log(:info, "Failed to pull hotlinked image for post: #{post_id}: #{src} - #{upload.errors.full_messages.join("\n")}")
|
2017-06-13 07:27:05 -04:00
|
|
|
end
|
2014-09-26 12:27:10 -04:00
|
|
|
else
|
2020-05-29 12:47:05 -04:00
|
|
|
large_images << normalize_src(original_src)
|
2017-11-16 09:45:07 -05:00
|
|
|
has_new_large_image = true
|
2014-09-26 12:27:10 -04:00
|
|
|
end
|
2013-11-05 13:04:47 -05:00
|
|
|
else
|
2020-05-29 12:47:05 -04:00
|
|
|
broken_images << normalize_src(original_src)
|
2017-11-16 09:45:07 -05:00
|
|
|
has_new_broken_image = true
|
2013-11-05 13:04:47 -05:00
|
|
|
end
|
|
|
|
end
|
2020-01-28 19:11:38 -05:00
|
|
|
|
2013-12-21 02:19:22 -05:00
|
|
|
# have we successfully downloaded that file?
|
2013-11-05 13:04:47 -05:00
|
|
|
if downloaded_urls[src].present?
|
2017-01-16 05:50:07 -05:00
|
|
|
escaped_src = Regexp.escape(original_src)
|
2019-05-23 03:43:25 -04:00
|
|
|
|
2019-06-06 03:50:35 -04:00
|
|
|
replace_raw = ->(match, match_src, replacement, _index) {
|
2019-06-21 00:32:02 -04:00
|
|
|
|
2020-05-29 12:47:05 -04:00
|
|
|
if normalize_src(src) == normalize_src(match_src)
|
2019-06-21 00:32:02 -04:00
|
|
|
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)
|
|
|
|
end
|
|
|
|
|
2019-06-06 03:50:35 -04:00
|
|
|
raw = raw.gsub(
|
|
|
|
match,
|
2019-06-21 00:32:02 -04:00
|
|
|
replacement
|
2019-06-06 03:50:35 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2013-11-20 07:10:08 -05:00
|
|
|
# there are 6 ways to insert an image in a post
|
2013-11-05 13:04:47 -05:00
|
|
|
# HTML tag - <img src="http://...">
|
2019-06-06 03:50:35 -04:00
|
|
|
InlineUploads.match_img(raw, external_src: true, &replace_raw)
|
2019-05-23 03:43:25 -04:00
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
# BBCode tag - [img]http://...[/img]
|
2019-06-13 01:53:43 -04:00
|
|
|
InlineUploads.match_bbcode_img(raw, external_src: true, &replace_raw)
|
2019-06-06 03:50:35 -04:00
|
|
|
|
2013-11-20 07:10:08 -05:00
|
|
|
# Markdown linked image - [![alt](http://...)](http://...)
|
2013-11-05 13:04:47 -05:00
|
|
|
# Markdown inline - ![alt](http://...)
|
2016-09-01 02:25:40 -04:00
|
|
|
# Markdown inline - ![](http://... "image title")
|
2016-09-01 08:26:39 -04:00
|
|
|
# Markdown inline - ![alt](http://... "image title")
|
2019-06-06 03:50:35 -04:00
|
|
|
InlineUploads.match_md_inline_img(raw, external_src: true, &replace_raw)
|
|
|
|
|
2013-11-05 13:04:47 -05:00
|
|
|
# Direct link
|
2019-06-06 03:50:35 -04:00
|
|
|
raw.gsub!(/^#{escaped_src}(\s?)$/) { "![](#{upload.short_url})#{$1}" }
|
2013-11-05 13:04:47 -05:00
|
|
|
end
|
|
|
|
rescue => e
|
2019-06-06 03:50:35 -04:00
|
|
|
if Rails.env.test?
|
|
|
|
raise e
|
|
|
|
else
|
|
|
|
log(:error, "Failed to pull hotlinked image (#{src}) post: #{post_id}\n" + e.message + "\n" + e.backtrace.join("\n"))
|
|
|
|
end
|
2013-11-05 13:04:47 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-16 09:45:07 -05:00
|
|
|
large_images.uniq!
|
|
|
|
broken_images.uniq!
|
|
|
|
|
|
|
|
post.custom_fields[Post::LARGE_IMAGES] = large_images.to_json if large_images.present?
|
|
|
|
post.custom_fields[Post::BROKEN_IMAGES] = broken_images.to_json if broken_images.present?
|
|
|
|
post.custom_fields[Post::DOWNLOADED_IMAGES] = downloaded_images.to_json if downloaded_images.present?
|
|
|
|
# only save custom fields if there are any
|
|
|
|
post.save_custom_fields if large_images.present? || broken_images.present? || downloaded_images.present?
|
2017-11-15 23:13:15 -05:00
|
|
|
|
2014-04-21 17:08:17 -04:00
|
|
|
post.reload
|
2017-11-15 05:30:47 -05:00
|
|
|
|
2015-10-30 17:46:46 -04:00
|
|
|
if start_raw == post.raw && raw != post.raw
|
2014-10-27 17:06:43 -04:00
|
|
|
changes = { raw: raw, edit_reason: I18n.t("upload.edit_reason") }
|
2019-09-12 10:55:45 -04:00
|
|
|
post.revise(Discourse.system_user, changes, bypass_bump: true, skip_staff_log: true)
|
2017-11-16 09:45:07 -05:00
|
|
|
elsif has_downloaded_image || has_new_large_image || has_new_broken_image
|
2020-05-29 08:07:47 -04:00
|
|
|
post.trigger_post_process(
|
|
|
|
bypass_bump: true,
|
|
|
|
skip_pull_hotlinked_images: true # Avoid an infinite loop of job scheduling
|
|
|
|
)
|
2013-11-05 13:04:47 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract_images_from(html)
|
2020-05-04 23:46:57 -04:00
|
|
|
doc = Nokogiri::HTML5::fragment(html)
|
2019-06-06 03:50:35 -04:00
|
|
|
|
2019-06-13 21:21:25 -04:00
|
|
|
doc.css("img[src], a.lightbox[href]") -
|
2019-06-06 03:50:35 -04:00
|
|
|
doc.css("img.avatar") -
|
|
|
|
doc.css(".lightbox img[src]")
|
2013-11-05 13:04:47 -05:00
|
|
|
end
|
|
|
|
|
2020-01-28 19:11:38 -05:00
|
|
|
def should_download_image?(src, post = nil)
|
2014-05-07 13:49:16 -04:00
|
|
|
# make sure we actually have a url
|
|
|
|
return false unless src.present?
|
2019-03-27 16:31:12 -04:00
|
|
|
|
2020-06-19 15:21:05 -04:00
|
|
|
local_bases = [
|
|
|
|
Discourse.base_url,
|
|
|
|
Discourse.asset_host,
|
|
|
|
].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 08:00:52 -04: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-28 19:11:38 -05: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-27 16:31:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Don't download non-local images unless site setting enabled
|
|
|
|
return false unless SiteSetting.download_remote_images_to_local?
|
2017-07-06 04:55:28 -04:00
|
|
|
|
2014-05-07 13:49:16 -04:00
|
|
|
# parse the src
|
|
|
|
begin
|
|
|
|
uri = URI.parse(src)
|
2018-08-14 06:23:32 -04:00
|
|
|
rescue URI::Error
|
2014-05-07 13:49:16 -04:00
|
|
|
return false
|
|
|
|
end
|
2017-07-06 04:55:28 -04:00
|
|
|
|
|
|
|
hostname = uri.hostname
|
|
|
|
return false unless hostname
|
|
|
|
|
2014-05-07 13:49:16 -04:00
|
|
|
# check the domains blacklist
|
2014-04-21 16:59:53 -04:00
|
|
|
SiteSetting.should_download_images?(src)
|
2013-11-05 13:04:47 -05:00
|
|
|
end
|
|
|
|
|
2017-07-04 21:34:24 -04:00
|
|
|
def log(log_level, message)
|
|
|
|
Rails.logger.public_send(
|
|
|
|
log_level,
|
|
|
|
"#{RailsMultisite::ConnectionManagement.current_db}: #{message}"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-03-28 02:44:42 -04:00
|
|
|
private
|
|
|
|
|
2020-05-29 12:47:05 -04:00
|
|
|
def normalize_src(src)
|
|
|
|
uri = Addressable::URI.heuristic_parse(src)
|
|
|
|
uri.normalize!
|
|
|
|
uri.scheme = nil
|
|
|
|
uri.to_s
|
2020-06-19 07:52:51 -04:00
|
|
|
rescue URI::Error, Addressable::URI::InvalidURIError
|
2020-05-29 12:47:05 -04:00
|
|
|
src
|
2018-03-28 02:44:42 -04:00
|
|
|
end
|
2013-11-05 13:04:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|