Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed bug with the difference of key format between generate_cache_id and clean_cache! #2036

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/carrierwave/storage/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ def delete_dir!(path)

def clean_cache!(seconds)
Dir.glob(::File.expand_path(::File.join(uploader.cache_dir, '*'), CarrierWave.root)).each do |dir|
time = dir.scan(/(\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
3 changes: 2 additions & 1 deletion lib/carrierwave/storage/fog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ def clean_cache!(seconds)
:key => uploader.fog_directory,
:public => uploader.fog_public
).files.all(:prefix => uploader.cache_dir).each do |file|
time = file.key.scan(/(\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
33 changes: 21 additions & 12 deletions spec/storage/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,35 +48,44 @@
end

describe '#clean_cache!' do
let(:five_days_ago_int) { 1369894322 }
let(:three_days_ago_int) { 1370067122 }
let(:yesterday_int) { 1370239922 }
let(:today) { '2016/10/09 10:00:00'.to_time }
let(:five_days_ago) { today.ago(5.days) }
let(:three_days_ago) { today.ago(3.days) }
let(:yesterday) { today.yesterday }
let(:cache_dir) { File.expand_path(uploader_class.cache_dir, CarrierWave.root) }

before do
FileUtils.mkdir_p File.expand_path("#{five_days_ago_int}-234-2213", cache_dir)
FileUtils.mkdir_p File.expand_path("#{three_days_ago_int}-234-2213", cache_dir)
FileUtils.mkdir_p File.expand_path("#{yesterday_int}-234-2213", cache_dir)
[five_days_ago, three_days_ago, yesterday, (today - 1.minute)].each do |created_date|
Timecop.freeze (created_date) do
FileUtils.mkdir_p File.expand_path(CarrierWave.generate_cache_id, cache_dir)
end
end
end

after { FileUtils.rm_rf(cache_dir) }

it "clears all files older than now in the default cache directory" do
Timecop.freeze(today) { uploader_class.clean_cached_files!(0) }

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

it "clears all files older than, by default, 24 hours in the default cache directory" do
Timecop.freeze(Time.at(1370261522)) { uploader_class.clean_cached_files! }
Timecop.freeze(today) { uploader_class.clean_cached_files! }

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

it "allows to set since how many seconds delete the cached files" do
Timecop.freeze(Time.at(1370261522)) { uploader_class.clean_cached_files!(60*60*24*4) }
Timecop.freeze(today) { uploader_class.clean_cached_files!(4.days) }

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

it "'s aliased on the CarrierWave module" do
Timecop.freeze(Time.at(1370261522)) { CarrierWave.clean_cached_files! }
Timecop.freeze(today) { CarrierWave.clean_cached_files! }

expect(Dir.glob("#{cache_dir}/*").size).to eq(1)
expect(Dir.glob("#{cache_dir}/*").size).to eq(2)
end
end
end
36 changes: 23 additions & 13 deletions spec/storage/fog_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,39 +258,49 @@ class FogSpec#{fog_credentials[:provider]}Uploader < CarrierWave::Uploader::Base
end

describe '#clean_cache!' do
let(:now){ Time.now.to_i }
let(:today) { '2016/10/09 10:00:00'.to_time }
let(:five_days_ago) { today.ago(5.days) }
let(:three_days_ago) { today.ago(3.days) }
let(:yesterday) { today.yesterday }
before do
# clean up
@directory.files.each{|file| file.destroy }
# We can't use simple time freezing because of AWS request time check
five_days_ago_int = now - 367270
three_days_ago_int = now - 194400
yesterday_int = now - 21600
[five_days_ago, three_days_ago, yesterday, (today - 1.minute)].each do |created_date|
key = nil
Timecop.freeze created_date do
key = "uploads/tmp/#{CarrierWave.generate_cache_id}/test.jpg"
end
@directory.files.create(:key => key, :body => 'A test, 1234', :public => true)
end
end

[five_days_ago_int, three_days_ago_int, yesterday_int].each do |as_of|
@directory.files.create(:key => "uploads/tmp/#{as_of}-234-2213/test.jpg", :body => 'A test, 1234', :public => true)
it "should clear all files older than now in the default cache directory" do
Timecop.freeze(today) do
@uploader.clean_cached_files!(0)
end
expect(@directory.files.all(:prefix => 'uploads/tmp').size).to eq(0)
end

it "should clear all files older than, by defaul, 24 hours in the default cache directory" do
Timecop.freeze(Time.at(now)) do
Timecop.freeze(today) do
@uploader.clean_cached_files!
end
expect(@directory.files.all(:prefix => 'uploads/tmp').size).to eq(1)
expect(@directory.files.all(:prefix => 'uploads/tmp').size).to eq(2)
end

it "should permit to set since how many seconds delete the cached files" do
Timecop.freeze(Time.at(now)) do
@uploader.clean_cached_files!(60*60*24*4)
Timecop.freeze(today) do
@uploader.clean_cached_files!(4.days)
end
expect(@directory.files.all(:prefix => 'uploads/tmp').size).to eq(2)
expect(@directory.files.all(:prefix => 'uploads/tmp').size).to eq(3)
end

it "should be aliased on the CarrierWave module" do
Timecop.freeze(Time.at(now)) do
Timecop.freeze(today) do
CarrierWave.clean_cached_files!
end
expect(@directory.files.all(:prefix => 'uploads/tmp').size).to eq(1)
expect(@directory.files.all(:prefix => 'uploads/tmp').size).to eq(2)
end
end

Expand Down