moved `has_been_uploaded` and `uploaded_regex` to the `Upload` model

This commit is contained in:
Régis Hanol 2013-06-16 10:21:01 +02:00
parent c11f4456ae
commit 2c3f757951
5 changed files with 42 additions and 51 deletions

View File

@ -46,10 +46,26 @@ class Upload < ActiveRecord::Base
end
def self.store_file(file, sha, image_info, upload_id)
return S3.store_file(file, sha, image_info, upload_id) if SiteSetting.enable_s3_uploads?
return S3.store_file(file, sha, image_info, upload_id) if SiteSetting.enable_s3_uploads?
return LocalStore.store_file(file, sha, image_info, upload_id)
end
def self.uploaded_regex
/\/uploads\/#{RailsMultisite::ConnectionManagement.current_db}\/(?<upload_id>\d+)\/[0-9a-f]{16}\.(png|jpg|jpeg|gif|tif|tiff|bmp)/
end
def self.has_been_uploaded?(url)
(url =~ /^\/[^\/]/) == 0 || url.start_with?(base_url)
end
def self.base_url
asset_host.present? ? asset_host : Discourse.base_url_no_prefix
end
def self.asset_host
ActionController::Base.asset_host
end
end
# == Schema Information

View File

@ -26,13 +26,13 @@ class CookedPostProcessor
return unless images.present?
images.each do |img|
# keep track of the src
# keep track of the original src
src = img['src']
# make sure the src is absolute (when working with locally uploaded files)
img['src'] = Discourse.base_url_no_prefix + img['src'] if img['src'] =~ /^\/[^\/]/
if src.present?
# update img dimensions if at least one is missing
# make sure the img has both width and height attributes
update_dimensions!(img)
# optimize image
img['src'] = optimize_image(img)
@ -76,6 +76,8 @@ class CookedPostProcessor
def optimize_image(img)
return img["src"]
# 1) optimize using image_optim
# 2) .png vs. .jpg
# TODO: needs some <3
end
@ -124,7 +126,7 @@ class CookedPostProcessor
def get_size(url)
# we can always crawl our own images
return unless SiteSetting.crawl_images? || has_been_uploaded?(url)
return unless SiteSetting.crawl_images? || Upload.has_been_uploaded?(url)
@size_cache[url] ||= FastImage.size(url)
rescue Zlib::BufError # FastImage.size raises BufError for some gifs
end
@ -134,18 +136,6 @@ class CookedPostProcessor
uri if %w(http https).include?(uri.scheme)
end
def has_been_uploaded?(url)
@has_been_uploaded_cache[url] ||= url.start_with?(base_url)
end
def base_url
asset_host.present? ? asset_host : Discourse.base_url_no_prefix
end
def asset_host
ActionController::Base.asset_host
end
def dirty?
@dirty
end

View File

@ -18,7 +18,7 @@ task "images:reindex" => :environment do
doc = Nokogiri::HTML::fragment(p.cooked)
doc.search("img").each do |img|
src = img['src']
if src.present? && has_been_uploaded?(src) && m = uploaded_regex.match(src)
if src.present? && Upload.has_been_uploaded?(src) && m = Upload.uploaded_regex.match(src)
begin
PostUpload.create({ post_id: p.id, upload_id: m[:upload_id] })
rescue ActiveRecord::RecordNotUnique
@ -30,19 +30,3 @@ task "images:reindex" => :environment do
end
puts "\ndone."
end
def uploaded_regex
/\/uploads\/#{RailsMultisite::ConnectionManagement.current_db}\/(?<upload_id>\d+)\/[0-9a-f]{16}\.(png|jpg|jpeg|gif|tif|tiff|bmp)/
end
def has_been_uploaded?(url)
url =~ /^\/[^\/]/ || url.start_with?(base_url) || (asset_host.present? && url.start_with?(asset_host))
end
def base_url
asset_host.present? ? asset_host : Discourse.base_url_no_prefix
end
def asset_host
ActionController::Base.asset_host
end

View File

@ -161,22 +161,4 @@ describe CookedPostProcessor do
end
context 'has_been_uploaded?' do
it "identifies internal urls" do
Discourse.expects(:base_url_no_prefix).returns("http://my.discourse.com")
cpp.has_been_uploaded?("http://my.discourse.com").should == true
end
it "identifies internal urls when using a CDN" do
ActionController::Base.expects(:asset_host).returns("http://my.cdn.com").twice
cpp.has_been_uploaded?("http://my.cdn.com").should == true
end
it "identifies external urls" do
cpp.has_been_uploaded?("http://domain.com").should == false
end
end
end

View File

@ -56,4 +56,23 @@ describe Upload do
end
context 'has_been_uploaded?' 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/upload/1234/42/ABCD.jpg").should == true
Upload.has_been_uploaded?("/upload/42/ABCD.jpg").should == true
end
it "identifies internal urls when using a CDN" do
ActionController::Base.expects(:asset_host).returns("http://my.cdn.com").twice
Upload.has_been_uploaded?("http://my.cdn.com/upload/1234/42/ABCD.jpg").should == true
end
it "identifies external urls" do
Upload.has_been_uploaded?("http://domain.com/upload/1234/42/ABCD.jpg").should == false
end
end
end