Skip to content

Commit 42c620a

Browse files
committed
Fog storage's #clean_cache! breaks when non-cache objects exist in cache_dir
Closes #2532
1 parent 5404e5c commit 42c620a

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lib/carrierwave/storage/fog.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,9 @@ def clean_cache!(seconds)
147147
:public => uploader.fog_public
148148
).files.all(:prefix => uploader.cache_dir).each do |file|
149149
# generate_cache_id returns key formated TIMEINT-PID(-COUNTER)-RND
150-
time = file.key.scan(/(\d+)-\d+-\d+(?:-\d+)?/).first.map { |t| t.to_i }
151-
time = Time.at(*time)
150+
matched = file.key.match(/(\d+)-\d+-\d+(?:-\d+)?/)
151+
next unless matched
152+
time = Time.at(matched[1].to_i)
152153
file.destroy if time < (Time.now.utc - seconds)
153154
end
154155
end

spec/storage/fog_helper.rb

+13
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,19 @@ def check_file
338338
end
339339
expect(@directory.files.all(:prefix => 'uploads/tmp').size).to eq(0)
340340
end
341+
342+
context "when a file which does not conform to the cache_id format exists" do
343+
before do
344+
@directory.files.create(:key => "uploads/tmp/invalid", :body => 'A test, 1234', :public => true)
345+
end
346+
347+
it "should just ignore that" do
348+
Timecop.freeze(today) do
349+
@uploader.clean_cached_files!(0)
350+
end
351+
expect(@directory.files.all(:prefix => 'uploads/tmp').size).to eq(1)
352+
end
353+
end
341354
end
342355

343356
describe "CarrierWave::Storage::Fog::File" do

0 commit comments

Comments
 (0)