From e17f614771825a6573f1348067316099c0ca55cc Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 26 May 2015 13:08:31 +1000 Subject: [PATCH] FIX: fallback to local store when uploads are not on S3 --- lib/file_store/s3_store.rb | 8 ++++++++ spec/components/file_store/s3_store_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/lib/file_store/s3_store.rb b/lib/file_store/s3_store.rb index d2ccf80425e..4dd92d73953 100644 --- a/lib/file_store/s3_store.rb +++ b/lib/file_store/s3_store.rb @@ -1,6 +1,7 @@ require "file_store/base_store" require_dependency "s3_helper" require_dependency "file_helper" +require_dependency "file_store/local_store" module FileStore @@ -67,6 +68,13 @@ module FileStore @s3_helper.update_tombstone_lifecycle(grace_period) end + def path_for(upload) + url = upload.url + if url && url[0] == "/" && url[1] != "/" + FileStore::LocalStore.new.path_for(upload) + end + end + private def get_path_for_upload(file, upload) diff --git a/spec/components/file_store/s3_store_spec.rb b/spec/components/file_store/s3_store_spec.rb index 0d8ea1e2272..c0ce1dc160d 100644 --- a/spec/components/file_store/s3_store_spec.rb +++ b/spec/components/file_store/s3_store_spec.rb @@ -1,5 +1,6 @@ require 'spec_helper' require 'file_store/s3_store' +require 'file_store/local_store' describe FileStore::S3Store do @@ -105,4 +106,23 @@ describe FileStore::S3Store do end + describe ".path_for" do + + def assert_path(path, expected) + upload = Upload.new(url: path) + + path = store.path_for(upload) + expected = FileStore::LocalStore.new.path_for(upload) if expected + + expect(path).to eq(expected) + end + + it "correctly falls back to local" do + assert_path("/hello", "/hello") + assert_path("//hello", nil) + assert_path("http://hello", nil) + assert_path("https://hello", nil) + end + end + end