Skip to content

Commit

Permalink
Fix filename suffix being removed due to unnecessary deduplication
Browse files Browse the repository at this point in the history
Fixes #2672
  • Loading branch information
mshibuya committed Jul 13, 2023
1 parent 3a24616 commit d68a111
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
5 changes: 3 additions & 2 deletions lib/carrierwave/uploader/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ def filename
#
def deduplicated_filename
return unless filename
return filename unless @deduplication_index

parts = filename.split('.')
basename = parts.shift
basename.sub!(/ ?\(\d+\)\z/, '')
([basename.to_s + (@deduplication_index ? "(#{@deduplication_index})" : '')] + parts).join('.')
([basename.to_s + (@deduplication_index > 1 ? "(#{@deduplication_index})" : '')] + parts).join('.')
end

##
Expand Down Expand Up @@ -120,7 +121,7 @@ def deduplicate(current_paths)
@deduplication_index = nil
return unless current_paths.include?(store_path)

(2..current_paths.size + 1).each do |i|
(1..current_paths.size + 1).each do |i|
@deduplication_index = i
break unless current_paths.include?(store_path)
end
Expand Down
28 changes: 25 additions & 3 deletions spec/uploader/store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,8 @@ def filename
let(:file) { stub_file('test.jpg') }

before do
allow(CarrierWave::SanitizedFile).to receive(:sanitize_regexp).and_return(/[^A-z0-9\.\(\)]/)

@uploader.cache!(file)
end

Expand All @@ -432,20 +434,40 @@ def filename
@uploader.deduplicate(['uploads/test.png'])
expect(@uploader.deduplicated_filename).to eq('test.jpg')
end

context "when deduplication is unnecessary" do
let(:file) { stub_tempfile('test.jpg', nil, 'test(2).jpg') }

it "does not change the suffix" do
@uploader.deduplicate([])
expect(@uploader.deduplicated_filename).to eq('test(2).jpg')
end
end
end

describe "#deduplicated_filename" do
subject { @uploader.deduplicated_filename }

it "returns the filename as it is when deduplication index is not set" do
it "returns the filename when deduplication index is not set" do
allow(@uploader).to receive(:filename).and_return('filename.jpg')
is_expected.to eq('filename.jpg')
end

it "returns the filename with its suffix unchanged when deduplication index is not set" do
allow(@uploader).to receive(:filename).and_return('filename(2).jpg')
is_expected.to eq('filename(2).jpg')
end

it "returns the filename without appending the suffix when deduplication index is 1" do
allow(@uploader).to receive(:filename).and_return('filename(2).jpg')
@uploader.instance_variable_set :@deduplication_index, 1
is_expected.to eq('filename.jpg')
end

it "appends the deduplication index as suffix" do
allow(@uploader).to receive(:filename).and_return('filename.jpg')
@uploader.instance_variable_set :@deduplication_index, 1
is_expected.to eq('filename(1).jpg')
@uploader.instance_variable_set :@deduplication_index, 5
is_expected.to eq('filename(5).jpg')
end

it "reuses the parentheses" do
Expand Down

0 comments on commit d68a111

Please sign in to comment.