From 2c3f75795152b0a7f7781a3e215a446e09382a59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Sun, 16 Jun 2013 10:21:01 +0200 Subject: [PATCH] moved `has_been_uploaded` and `uploaded_regex` to the `Upload` model --- app/models/upload.rb | 18 ++++++++++++++++- lib/cooked_post_processor.rb | 20 +++++-------------- lib/tasks/images.rake | 18 +---------------- spec/components/cooked_post_processor_spec.rb | 18 ----------------- spec/models/upload_spec.rb | 19 ++++++++++++++++++ 5 files changed, 42 insertions(+), 51 deletions(-) diff --git a/app/models/upload.rb b/app/models/upload.rb index 41c6f536a..362318202 100644 --- a/app/models/upload.rb +++ b/app/models/upload.rb @@ -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}\/(?\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 diff --git a/lib/cooked_post_processor.rb b/lib/cooked_post_processor.rb index 9039e738a..f46c49d83 100644 --- a/lib/cooked_post_processor.rb +++ b/lib/cooked_post_processor.rb @@ -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 diff --git a/lib/tasks/images.rake b/lib/tasks/images.rake index 662ff9e61..d1376bd3e 100644 --- a/lib/tasks/images.rake +++ b/lib/tasks/images.rake @@ -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}\/(?\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 diff --git a/spec/components/cooked_post_processor_spec.rb b/spec/components/cooked_post_processor_spec.rb index 1e38644fc..de1b44606 100644 --- a/spec/components/cooked_post_processor_spec.rb +++ b/spec/components/cooked_post_processor_spec.rb @@ -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 diff --git a/spec/models/upload_spec.rb b/spec/models/upload_spec.rb index 870a1e4ec..52b523d66 100644 --- a/spec/models/upload_spec.rb +++ b/spec/models/upload_spec.rb @@ -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