FIX: handle link tracking correctly for cdn based urls

(usually attachments)
This commit is contained in:
Sam 2015-08-05 12:15:08 +10:00
parent 54b780439d
commit a3c6cd7b38
2 changed files with 28 additions and 8 deletions

View File

@ -29,6 +29,15 @@ class TopicLinkClick < ActiveRecord::Base
urls << uri.path if uri.try(:host) == Discourse.current_hostname
urls << url.sub(/\?.*$/, '') if url.include?('?')
# add a cdn link
if uri && Discourse.asset_host.present?
cdn_uri = URI.parse(Discourse.asset_host) rescue nil
if cdn_uri && cdn_uri.hostname == uri.hostname && uri.path.starts_with?(cdn_uri.path)
is_cdn_link = true
urls << uri.path[(cdn_uri.path.length)..-1]
end
end
link = TopicLink.select([:id, :user_id])
# test for all possible URLs
@ -54,14 +63,7 @@ class TopicLinkClick < ActiveRecord::Base
return nil unless uri
# Only redirect to whitelisted hostnames
return url if WHITELISTED_REDIRECT_HOSTNAMES.include?(uri.hostname)
if Discourse.asset_host.present?
cdn_uri = URI.parse(Discourse.asset_host) rescue nil
if cdn_uri
return url if cdn_uri.hostname == uri.hostname && uri.path.starts_with?(cdn_uri.path)
end
end
return url if WHITELISTED_REDIRECT_HOSTNAMES.include?(uri.hostname) || is_cdn_link
return nil
end

View File

@ -122,6 +122,24 @@ describe TopicLinkClick do
ip: '127.0.0.3')
expect(url).to eq(nil)
# cdn better link track
path = "/uploads/site/29/5b585f848d8761d5.xls"
post = Fabricate(:post, topic: @topic, raw: "[test](#{path})")
TopicLink.extract_from(post)
url = TopicLinkClick.create_from(
url: "https://cdn.discourse.org/stuff#{path}",
topic_id: post.topic_id,
post_id: post.id,
ip: '127.0.0.3')
expect(url).to eq("https://cdn.discourse.org/stuff#{path}")
click = TopicLinkClick.order('id desc').first
expect(click.topic_link_id).to eq(TopicLink.order('id desc').first.id)
end
end