From 5c27dd175a9181845055e9333368aacceb9e05af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Wed, 17 Jul 2013 00:32:09 +0200 Subject: [PATCH] make sure we handle both s3 url formats --- app/models/upload.rb | 2 +- lib/s3_store.rb | 4 ++++ spec/models/upload_spec.rb | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/app/models/upload.rb b/app/models/upload.rb index d7fd2f7ae2b..828eacee54a 100644 --- a/app/models/upload.rb +++ b/app/models/upload.rb @@ -99,7 +99,7 @@ class Upload < ActiveRecord::Base end def self.is_on_s3?(url) - SiteSetting.enable_s3_uploads? && url.start_with?(S3Store.base_url) + SiteSetting.enable_s3_uploads? && (url.start_with?(S3Store.base_url) || url.start_with?(S3Store.base_url_old)) end def self.get_from_url(url) diff --git a/lib/s3_store.rb b/lib/s3_store.rb index 753f8045974..a61d311e2ca 100644 --- a/lib/s3_store.rb +++ b/lib/s3_store.rb @@ -16,6 +16,10 @@ module S3Store "//s3.amazonaws.com/#{SiteSetting.s3_upload_bucket}" end + def self.base_url_old + "//#{SiteSetting.s3_upload_bucket.downcase}.s3.amazonaws.com" + end + def self.remove_file(url) S3Store.check_missing_site_settings diff --git a/spec/models/upload_spec.rb b/spec/models/upload_spec.rb index 34df67d3a04..268a52b01c7 100644 --- a/spec/models/upload_spec.rb +++ b/spec/models/upload_spec.rb @@ -174,6 +174,24 @@ describe Upload do end + context ".is_on_s3?" do + + before do + SiteSetting.stubs(:enable_s3_uploads).returns(true) + SiteSetting.stubs(:s3_upload_bucket).returns("BuCkEt") + end + + it "case-insensitively matches the old subdomain format" do + Upload.is_on_s3?("//bucket.s3.amazonaws.com/1337.png").should == true + end + + it "case-sensitively matches the new folder format" do + Upload.is_on_s3?("//s3.amazonaws.com/BuCkEt/1337.png").should == true + Upload.is_on_s3?("//s3.amazonaws.com/bucket/1337.png").should == false + end + + end + context ".get_from_url" do it "works only when the file has been uploaded" do