2013-11-20 20:10:08 +08:00
|
|
|
require_dependency 'url_helper'
|
2014-04-15 04:55:57 +08:00
|
|
|
require_dependency 'file_helper'
|
2017-05-11 06:16:57 +08:00
|
|
|
require_dependency 'upload_creator'
|
2013-11-20 20:10:08 +08:00
|
|
|
|
2013-11-06 02:04:47 +08:00
|
|
|
module Jobs
|
|
|
|
|
|
|
|
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
|
|
|
|
# maximum size of the file in bytes
|
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
|
|
|
|
|
|
|
|
def execute(args)
|
2013-11-15 22:22:18 +08:00
|
|
|
return unless SiteSetting.download_remote_images_to_local?
|
2013-11-06 02:04:47 +08:00
|
|
|
|
|
|
|
post_id = args[:post_id]
|
|
|
|
raise Discourse::InvalidParameters.new(:post_id) unless post_id.present?
|
|
|
|
|
2014-05-06 21:41:59 +08:00
|
|
|
post = Post.find_by(id: post_id)
|
2013-11-06 02:04:47 +08:00
|
|
|
return unless post.present?
|
|
|
|
|
|
|
|
raw = post.raw.dup
|
2014-04-22 05:08:17 +08:00
|
|
|
start_raw = raw.dup
|
2013-11-06 02:04:47 +08:00
|
|
|
downloaded_urls = {}
|
2017-09-01 22:26:13 +08:00
|
|
|
broken_images, large_images = [], []
|
2013-11-06 02:04:47 +08:00
|
|
|
|
|
|
|
extract_images_from(post.cooked).each do |image|
|
2017-01-16 18:50:07 +08:00
|
|
|
src = original_src = image['src']
|
2017-07-06 16:55:28 +08:00
|
|
|
src = "http:#{src}" if src.start_with?("//")
|
2013-11-06 02:04:47 +08:00
|
|
|
|
|
|
|
if is_valid_image_url(src)
|
2014-04-22 05:08:17 +08:00
|
|
|
hotlinked = nil
|
2013-11-06 02:04:47 +08:00
|
|
|
begin
|
|
|
|
# have we already downloaded that file?
|
2014-04-22 05:08:17 +08:00
|
|
|
unless downloaded_urls.include?(src)
|
2014-04-22 21:32:48 +08:00
|
|
|
begin
|
2017-05-25 01:42:52 +08:00
|
|
|
hotlinked = FileHelper.download(
|
|
|
|
src,
|
|
|
|
max_file_size: @max_size,
|
|
|
|
tmp_file_name: "discourse-hotlinked",
|
|
|
|
follow_redirect: true
|
|
|
|
)
|
2014-04-22 21:32:48 +08:00
|
|
|
rescue Discourse::InvalidParameters
|
2017-09-28 07:00:13 +08:00
|
|
|
log(:error, "InvalidParameters while downloading hotlinked image (#{src}) for post: #{post_id}")
|
2017-09-28 14:35:27 +08:00
|
|
|
rescue => e
|
|
|
|
log(:error, "Failed to download image #{e}")
|
2014-04-22 21:32:48 +08:00
|
|
|
end
|
2014-09-27 00:27:10 +08:00
|
|
|
if hotlinked
|
2015-08-18 00:57:28 +08:00
|
|
|
if File.size(hotlinked.path) <= @max_size
|
2014-09-27 00:27:10 +08:00
|
|
|
filename = File.basename(URI.parse(src).path)
|
2017-06-13 19:27:05 +08:00
|
|
|
filename << File.extname(hotlinked.path) unless filename["."]
|
2017-05-11 06:16:57 +08:00
|
|
|
upload = UploadCreator.new(hotlinked, filename, origin: src).create_for(post.user_id)
|
2017-06-13 19:27:05 +08:00
|
|
|
if upload.persisted?
|
|
|
|
downloaded_urls[src] = upload.url
|
|
|
|
else
|
2017-09-28 05:26:07 +08:00
|
|
|
log(:error, "Failed to pull hotlinked image for post: #{post_id}: #{src} - #{upload.errors.join("\n")}")
|
2017-06-13 19:27:05 +08:00
|
|
|
end
|
2014-09-27 00:27:10 +08:00
|
|
|
else
|
2017-09-28 05:26:07 +08:00
|
|
|
log(:error, "Failed to pull hotlinked image for post: #{post_id}: #{src} - Image is bigger than #{@max_size}")
|
2017-09-01 22:26:13 +08:00
|
|
|
large_images << original_src
|
2014-09-27 00:27:10 +08:00
|
|
|
end
|
2013-11-06 02:04:47 +08:00
|
|
|
else
|
2017-09-28 05:26:07 +08:00
|
|
|
log(:error, "There was an error while downloading '#{src}' locally for post: #{post_id}")
|
2017-09-01 22:26:13 +08:00
|
|
|
broken_images << original_src
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
|
|
|
end
|
2013-12-21 15:19:22 +08:00
|
|
|
# have we successfully downloaded that file?
|
2013-11-06 02:04:47 +08:00
|
|
|
if downloaded_urls[src].present?
|
|
|
|
url = downloaded_urls[src]
|
2017-01-16 18:50:07 +08:00
|
|
|
escaped_src = Regexp.escape(original_src)
|
2013-11-20 20:10:08 +08:00
|
|
|
# there are 6 ways to insert an image in a post
|
2013-11-06 02:04:47 +08:00
|
|
|
# HTML tag - <img src="http://...">
|
|
|
|
raw.gsub!(/src=["']#{escaped_src}["']/i, "src='#{url}'")
|
|
|
|
# BBCode tag - [img]http://...[/img]
|
|
|
|
raw.gsub!(/\[img\]#{escaped_src}\[\/img\]/i, "[img]#{url}[/img]")
|
2013-11-20 20:10:08 +08:00
|
|
|
# Markdown linked image - [![alt](http://...)](http://...)
|
|
|
|
raw.gsub!(/\[!\[([^\]]*)\]\(#{escaped_src}\)\]/) { "[<img src='#{url}' alt='#{$1}'>]" }
|
2013-11-06 02:04:47 +08:00
|
|
|
# Markdown inline - ![alt](http://...)
|
|
|
|
raw.gsub!(/!\[([^\]]*)\]\(#{escaped_src}\)/) { "![#{$1}](#{url})" }
|
2016-09-01 14:25:40 +08:00
|
|
|
# Markdown inline - ![](http://... "image title")
|
|
|
|
raw.gsub!(/!\[\]\(#{escaped_src} "([^\]]*)"\)/) { "![](#{url})" }
|
2016-09-01 20:26:39 +08:00
|
|
|
# Markdown inline - ![alt](http://... "image title")
|
|
|
|
raw.gsub!(/!\[([^\]]*)\]\(#{escaped_src} "([^\]]*)"\)/) { "![](#{url})" }
|
2013-11-06 02:04:47 +08:00
|
|
|
# Markdown reference - [x]: http://
|
2015-11-09 23:37:51 +08:00
|
|
|
raw.gsub!(/\[([^\]]+)\]:\s?#{escaped_src}/) { "[#{$1}]: #{url}" }
|
2013-11-06 02:04:47 +08:00
|
|
|
# Direct link
|
2016-02-15 19:34:45 +08:00
|
|
|
raw.gsub!(/^#{escaped_src}(\s?)$/) { "<img src='#{url}'>#{$1}" }
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
|
|
|
rescue => e
|
2017-09-28 07:00:13 +08:00
|
|
|
log(:error, "Failed to pull hotlinked image (#{src}) post: #{post_id}\n" + e.message + "\n" + e.backtrace.join("\n"))
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-04-22 05:08:17 +08:00
|
|
|
post.reload
|
2015-10-31 05:46:46 +08:00
|
|
|
if start_raw == post.raw && raw != post.raw
|
2014-10-28 05:06:43 +08:00
|
|
|
changes = { raw: raw, edit_reason: I18n.t("upload.edit_reason") }
|
|
|
|
# we never want that job to bump the topic
|
|
|
|
options = { bypass_bump: true }
|
|
|
|
post.revise(Discourse.system_user, changes, options)
|
2017-06-02 17:39:06 +08:00
|
|
|
elsif downloaded_urls.present?
|
|
|
|
post.trigger_post_process(true)
|
2017-09-01 22:26:13 +08:00
|
|
|
elsif broken_images.present? || large_images.present?
|
|
|
|
start_html = post.cooked
|
|
|
|
doc = Nokogiri::HTML::fragment(start_html)
|
|
|
|
images = doc.css("img[src]") - doc.css("img.avatar")
|
|
|
|
images.each do |tag|
|
|
|
|
src = tag['src']
|
|
|
|
if broken_images.include?(src)
|
|
|
|
tag.name = 'span'
|
|
|
|
tag.set_attribute('class', 'broken-image fa fa-chain-broken')
|
2017-09-13 17:46:38 +08:00
|
|
|
tag.set_attribute('title', I18n.t('post.image_placeholder.broken'))
|
2017-09-01 22:26:13 +08:00
|
|
|
tag.remove_attribute('src')
|
2017-09-13 17:46:38 +08:00
|
|
|
tag.remove_attribute('width')
|
|
|
|
tag.remove_attribute('height')
|
2017-09-01 22:26:13 +08:00
|
|
|
elsif large_images.include?(src)
|
|
|
|
tag.name = 'a'
|
|
|
|
tag.set_attribute('href', src)
|
|
|
|
tag.set_attribute('target', '_blank')
|
2017-09-13 17:46:38 +08:00
|
|
|
tag.set_attribute('title', I18n.t('post.image_placeholder.large'))
|
2017-09-01 22:26:13 +08:00
|
|
|
tag.remove_attribute('src')
|
2017-09-13 17:46:38 +08:00
|
|
|
tag.remove_attribute('width')
|
|
|
|
tag.remove_attribute('height')
|
2017-09-01 22:26:13 +08:00
|
|
|
tag.inner_html = '<span class="large-image fa fa-picture-o"></span>'
|
2017-09-13 17:46:38 +08:00
|
|
|
parent = tag.parent
|
|
|
|
if parent.name == 'a'
|
|
|
|
parent.add_next_sibling(tag)
|
|
|
|
parent.add_next_sibling('<br>')
|
|
|
|
parent.content = parent["href"]
|
|
|
|
end
|
2017-09-01 22:26:13 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
if start_html == post.cooked && doc.to_html != post.cooked
|
|
|
|
post.update_column(:cooked, doc.to_html)
|
|
|
|
post.publish_change_to_clients! :revised
|
|
|
|
end
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract_images_from(html)
|
|
|
|
doc = Nokogiri::HTML::fragment(html)
|
2017-06-02 17:39:06 +08:00
|
|
|
doc.css("img[src]") - doc.css("img.avatar")
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def is_valid_image_url(src)
|
2014-05-08 01:49:16 +08:00
|
|
|
# make sure we actually have a url
|
|
|
|
return false unless src.present?
|
|
|
|
# we don't want to pull uploaded images
|
|
|
|
return false if Discourse.store.has_been_uploaded?(src)
|
2014-09-27 00:27:10 +08:00
|
|
|
# we don't want to pull relative images
|
|
|
|
return false if src =~ /\A\/[^\/]/i
|
2017-07-06 16:55:28 +08:00
|
|
|
|
2014-05-08 01:49:16 +08:00
|
|
|
# parse the src
|
|
|
|
begin
|
|
|
|
uri = URI.parse(src)
|
|
|
|
rescue URI::InvalidURIError
|
|
|
|
return false
|
|
|
|
end
|
2017-07-06 16:55:28 +08:00
|
|
|
|
|
|
|
hostname = uri.hostname
|
|
|
|
return false unless hostname
|
|
|
|
|
2014-05-08 01:49:16 +08:00
|
|
|
# we don't want to pull images hosted on the CDN (if we use one)
|
2017-07-06 16:55:28 +08:00
|
|
|
return false if Discourse.asset_host.present? && URI.parse(Discourse.asset_host).hostname == hostname
|
|
|
|
return false if SiteSetting.s3_cdn_url.present? && URI.parse(SiteSetting.s3_cdn_url).hostname == hostname
|
2014-05-08 01:49:16 +08:00
|
|
|
# we don't want to pull images hosted on the main domain
|
2017-07-06 16:55:28 +08:00
|
|
|
return false if URI.parse(Discourse.base_url_no_prefix).hostname == hostname
|
2014-05-08 01:49:16 +08:00
|
|
|
# check the domains blacklist
|
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
|
|
|
|
|
2013-11-06 02:04:47 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|