Skip to content

Commit

Permalink
Fix clean_cache! breaking with old format of cache id
Browse files Browse the repository at this point in the history
  • Loading branch information
mshibuya committed May 23, 2019
1 parent f94b061 commit aab402f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/carrierwave/storage/file.rb
Expand Up @@ -110,8 +110,8 @@ def delete_dir!(path)

def clean_cache!(seconds)
Dir.glob(::File.expand_path(::File.join(uploader.cache_dir, '*'), CarrierWave.root)).each do |dir|
# generate_cache_id returns key formated TIMEINT-PID-COUNTER-RND
time = dir.scan(/(\d+)-\d+-\d+-\d+/).first.map(&:to_i)
# generate_cache_id returns key formated TIMEINT-PID(-COUNTER)-RND
time = dir.scan(/(\d+)-\d+-\d+(?:-\d+)?/).first.map(&:to_i)
time = Time.at(*time)
if time < (Time.now.utc - seconds)
FileUtils.rm_rf(dir)
Expand Down
4 changes: 2 additions & 2 deletions lib/carrierwave/storage/fog.rb
Expand Up @@ -138,8 +138,8 @@ def clean_cache!(seconds)
:key => uploader.fog_directory,
:public => uploader.fog_public
).files.all(:prefix => uploader.cache_dir).each do |file|
# generate_cache_id returns key formated TIMEINT-PID-COUNTER-RND
time = file.key.scan(/(\d+)-\d+-\d+-\d+/).first.map { |t| t.to_i }
# generate_cache_id returns key formated TIMEINT-PID(-COUNTER)-RND
time = file.key.scan(/(\d+)-\d+-\d+(?:-\d+)?/).first.map { |t| t.to_i }
time = Time.at(*time)
file.destroy if time < (Time.now.utc - seconds)
end
Expand Down
7 changes: 7 additions & 0 deletions spec/storage/file_spec.rb
Expand Up @@ -94,5 +94,12 @@

expect(Dir.glob("#{cache_dir}/*").size).to eq(2)
end

it "cleans a directory named using old format of cache id" do
FileUtils.mkdir_p File.expand_path("#{yesterday.utc.to_i}-100-1234", cache_dir)
Timecop.freeze(today) { uploader_class.clean_cached_files!(0) }

expect(Dir.glob("#{cache_dir}/*").size).to eq(0)
end
end
end
8 changes: 8 additions & 0 deletions spec/storage/fog_helper.rb
Expand Up @@ -397,6 +397,14 @@ class FogSpec#{fog_credentials[:provider]}Uploader < CarrierWave::Uploader::Base
end
expect(@directory.files.all(:prefix => 'uploads/tmp').size).to eq(2)
end

it "cleans a directory named using old format of cache id" do
@directory.files.create(:key => "uploads/tmp/#{yesterday.utc.to_i}-100-1234/test.jpg", :body => 'A test, 1234', :public => true)
Timecop.freeze(today) do
@uploader.clean_cached_files!(0)
end
expect(@directory.files.all(:prefix => 'uploads/tmp').size).to eq(0)
end
end

describe 'fog_public' do
Expand Down

0 comments on commit aab402f

Please sign in to comment.