From 8cc80105649771be24e6b02352cc8c99ed92eaf1 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Thu, 3 Aug 2017 11:56:55 +0900 Subject: [PATCH] Maintain backwards compatibility before `Jobs::MigrateUploadExtensions` runs. --- lib/file_store/base_store.rb | 11 +++++++++- spec/components/file_store/base_store_spec.rb | 21 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 spec/components/file_store/base_store_spec.rb diff --git a/lib/file_store/base_store.rb b/lib/file_store/base_store.rb index 0a7ae8ba5aa..f30882d4d6e 100644 --- a/lib/file_store/base_store.rb +++ b/lib/file_store/base_store.rb @@ -95,7 +95,16 @@ module FileStore end def get_path_for_upload(upload) - get_path_for("original".freeze, upload.id, upload.sha1, "." + upload.extension) + extension = + if upload.extension + ".#{upload.extension}" + else + # Maintain backward compatibility before Jobs::MigrateUploadExtensions + # runs + File.extname(upload.original_filename) + end + + get_path_for("original".freeze, upload.id, upload.sha1, extension) end def get_path_for_optimized_image(optimized_image) diff --git a/spec/components/file_store/base_store_spec.rb b/spec/components/file_store/base_store_spec.rb new file mode 100644 index 00000000000..89f0a105da2 --- /dev/null +++ b/spec/components/file_store/base_store_spec.rb @@ -0,0 +1,21 @@ +require 'rails_helper' + +RSpec.describe FileStore::BaseStore do + let(:upload) { Fabricate(:upload, id: 9999, sha1: Digest::SHA1.hexdigest('9999')) } + + describe '#get_path_for_upload' do + it 'should return the right path' do + expect(FileStore::BaseStore.new.get_path_for_upload(upload)) + .to eq('original/2X/4/4170ac2a2782a1516fe9e13d7322ae482c1bd594.png') + end + + describe 'when Upload#extension has not been set' do + it 'should return the right path' do + upload.update!(extension: nil) + + expect(FileStore::BaseStore.new.get_path_for_upload(upload)) + .to eq('original/2X/4/4170ac2a2782a1516fe9e13d7322ae482c1bd594.png') + end + end + end +end