Merge pull request #1228 from ZogStriP/fix-thumbnailing-with-cdn-enabled

FIX: thumbnailing isn't working with CDN enabled
This commit is contained in:
Robin Ward 2013-07-22 07:22:59 -07:00
commit 80a0943f54
5 changed files with 25 additions and 16 deletions

View File

@ -91,7 +91,7 @@ class Upload < ActiveRecord::Base
end
def self.is_relative?(url)
url.start_with?("/uploads/")
url.start_with?(LocalStore.directory)
end
def self.is_local?(url)
@ -103,6 +103,8 @@ class Upload < ActiveRecord::Base
end
def self.get_from_url(url)
# we store relative urls, so we need to remove any host/cdn
url = url.gsub(/^#{LocalStore.asset_host}/i, "") if LocalStore.asset_host.present?
Upload.where(url: url).first if has_been_uploaded?(url)
end

View File

@ -4,7 +4,7 @@ module LocalStore
unique_sha1 = Digest::SHA1.hexdigest("#{Time.now.to_s}#{file.original_filename}")[0,16]
extension = File.extname(file.original_filename)
clean_name = "#{unique_sha1}#{extension}"
url_root = "/uploads/#{RailsMultisite::ConnectionManagement.current_db}/#{upload_id}"
url_root = "#{directory}/#{upload_id}"
path = "#{Rails.root}/public#{url_root}"
FileUtils.mkdir_p path
@ -41,7 +41,7 @@ module LocalStore
end
def self.asset_host
ActionController::Base.asset_host
Rails.configuration.action_controller.asset_host
end
end

View File

@ -65,13 +65,13 @@ describe CookedPostProcessor do
context "with locally uploaded images" do
let(:upload) { Fabricate(:upload) }
let(:post) { Fabricate(:post_with_uploaded_images) }
let(:post) { Fabricate(:post_with_uploaded_image) }
let(:cpp) { CookedPostProcessor.new(post) }
before { FastImage.stubs(:size) }
before { FastImage.stubs(:size).returns([200, 400]) }
# all in one test to speed things up
it "works" do
Upload.expects(:get_from_url).returns(upload).twice
Upload.expects(:get_from_url).returns(upload)
cpp.post_process_images
# ensures absolute urls on uploaded images
cpp.html.should =~ /#{LocalStore.base_url}/
@ -148,7 +148,7 @@ describe CookedPostProcessor do
context "topic image" do
let(:topic) { build(:topic, id: 1) }
let(:post) { Fabricate(:post_with_uploaded_images, topic: topic) }
let(:post) { Fabricate(:post_with_uploaded_image, topic: topic) }
let(:cpp) { CookedPostProcessor.new(post) }
it "adds a topic image if there's one in the post" do

View File

@ -43,11 +43,8 @@ Fabricator(:post_with_images_in_quote_and_onebox, from: :post) do
'
end
Fabricator(:post_with_uploaded_images, from: :post) do
cooked '
<img src="/uploads/default/2/3456789012345678.png" width="1500" height="2000">
<img src="/uploads/default/1/1234567890123456.jpg">
'
Fabricator(:post_with_uploaded_image, from: :post) do
cooked '<img src="/uploads/default/2/3456789012345678.png" width="1500" height="2000">'
end
Fabricator(:post_with_an_attachment, from: :post) do

View File

@ -153,11 +153,11 @@ describe Upload do
it "identifies internal or relatives urls" do
Discourse.expects(:base_url_no_prefix).returns("http://discuss.site.com")
Upload.has_been_uploaded?("http://discuss.site.com/uploads/default/42/0123456789ABCDEF.jpg").should == true
Upload.has_been_uploaded?("/uploads/42/0123456789ABCDEF.jpg").should == true
Upload.has_been_uploaded?("/uploads/default/42/0123456789ABCDEF.jpg").should == true
end
it "identifies internal urls when using a CDN" do
ActionController::Base.expects(:asset_host).returns("http://my.cdn.com").twice
Rails.configuration.action_controller.expects(:asset_host).returns("http://my.cdn.com").twice
Upload.has_been_uploaded?("http://my.cdn.com/uploads/default/42/0123456789ABCDEF.jpg").should == true
end
@ -194,10 +194,20 @@ describe Upload do
context ".get_from_url" do
it "works when the file has been uploaded" do
Upload.expects(:where).returns([]).once
Upload.get_from_url("/uploads/default/1/10387531.jpg")
end
it "works when using a cdn" do
Rails.configuration.action_controller.stubs(:asset_host).returns("http://my.cdn.com")
Upload.expects(:where).with(url: "/uploads/default/1/02395732905.jpg").returns([]).once
Upload.get_from_url("http://my.cdn.com/uploads/default/1/02395732905.jpg")
end
it "works only when the file has been uploaded" do
Upload.expects(:has_been_uploaded?).returns(false)
Upload.expects(:where).never
Upload.get_from_url("discourse.org")
Upload.get_from_url("http://domain.com/my/file.txt")
end
end