BUGFIX: errors when post-processing 'data images'

This commit is contained in:
Régis Hanol 2014-07-18 17:54:18 +02:00
parent 8f36d3367f
commit c7330ed73f
7 changed files with 26 additions and 14 deletions

View File

@ -238,6 +238,8 @@ Discourse.Markdown.whiteListTag('a', 'class', 'mention');
Discourse.Markdown.whiteListTag('a', 'data-bbcode');
Discourse.Markdown.whiteListTag('img', 'src', /^data:image.*/i);
Discourse.Markdown.whiteListTag('div', 'class', 'title');
Discourse.Markdown.whiteListTag('div', 'class', 'quote-controls');

View File

@ -90,7 +90,7 @@ module Jobs
def extract_images_from(html)
doc = Nokogiri::HTML::fragment(html)
doc.css("img") - doc.css(".onebox-result img") - doc.css("img.avatar")
doc.css("img[src]") - doc.css(".onebox-result img") - doc.css("img.avatar")
end
def is_valid_image_url(src)

View File

@ -112,6 +112,7 @@ class Upload < ActiveRecord::Base
end
def self.get_from_url(url)
return if url.blank?
# we store relative urls, so we need to remove any host/cdn
url = url.gsub(/^#{Discourse.asset_host}/i, "") if Discourse.asset_host.present?
Upload.find_by(url: url) if Discourse.store.has_been_uploaded?(url)

View File

@ -27,14 +27,14 @@ class CookedPostProcessor
def keep_reverse_index_up_to_date
upload_ids = Set.new
@doc.search("a").each do |a|
@doc.css("a[href]").each do |a|
href = a["href"].to_s
if upload = Upload.get_from_url(href)
upload_ids << upload.id
end
end
@doc.search("img").each do |img|
@doc.css("img[src]").each do |img|
src = img["src"].to_s
if upload = Upload.get_from_url(src)
upload_ids << upload.id
@ -64,8 +64,14 @@ class CookedPostProcessor
end
def extract_images
# do not extract images inside oneboxes or quotes
@doc.css("img") - @doc.css(".onebox-result img, .onebox img") - @doc.css(".quote img")
# all image with a src attribute
@doc.css("img[src]") -
# minus, data images
@doc.css("img[src^='data']") -
# minus, image inside oneboxes
@doc.css(".onebox-result img, .onebox img") -
# minux, images inside quotes
@doc.css(".quote img")
end
def limit_size!(img)
@ -207,12 +213,12 @@ class CookedPostProcessor
end
def optimize_urls
@doc.search("a").each do |a|
@doc.css("a[href]").each do |a|
href = a["href"].to_s
a["href"] = schemaless absolute(href) if is_local(href)
end
@doc.search("img").each do |img|
@doc.css("img[src]").each do |img|
src = img["src"].to_s
img["src"] = schemaless absolute(src) if is_local(src)
end

View File

@ -28,7 +28,7 @@ module FileStore
end
def has_been_uploaded?(url)
is_relative?(url) || is_local?(url)
url.present? && (is_relative?(url) || is_local?(url))
end
def absolute_base_url
@ -120,10 +120,11 @@ module FileStore
end
def is_relative?(url)
url.start_with?(relative_base_url)
url.present? && url.start_with?(relative_base_url)
end
def is_local?(url)
return false if url.blank?
absolute_url = url.start_with?("//") ? SiteSetting.scheme + ":" + url : url
absolute_url.start_with?(absolute_base_url) || absolute_url.start_with?(absolute_base_cdn_url)
end

View File

@ -30,7 +30,7 @@ module FileStore
end
def has_been_uploaded?(url)
url.start_with?(absolute_base_url)
url.present? && url.start_with?(absolute_base_url)
end
def absolute_base_url

View File

@ -1,10 +1,12 @@
module UrlHelper
def is_local(url)
Discourse.store.has_been_uploaded?(url) ||
url =~ /^\/assets\// ||
url =~ /^\/plugins\// ||
url.start_with?(Discourse.asset_host || Discourse.base_url_no_prefix)
url.present? && (
Discourse.store.has_been_uploaded?(url) ||
url =~ /^\/assets\// ||
url =~ /^\/plugins\// ||
url.start_with?(Discourse.asset_host || Discourse.base_url_no_prefix)
)
end
def absolute(url, cdn = Discourse.asset_host)