12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'app/sweepers/cms_content_sweeper.rb', line 12
def delete_all_cached_pages
cache_dir = File.expand_path(Manage::CmsPagesController.page_cache_directory)
public_dir = File.expand_path("#{Rails.root}/public")
begin
cache_dir = File.realpath cache_dir
public_dir = File.realpath public_dir
end
begin
if cache_dir == public_dir
expire_page controller: 'cms/content', action: 'show', content_path: nil
dangerous_names = [ 'assets', 'images', 'javascripts', 'media', 'stylesheets' ]
home_page = CmsPage.find_by_path('')
CmsPage.where(parent_id: home_page.id).each do |page|
if dangerous_names.include?(page.path)
Rails.logger.info "Expire tree #{path} using safe method"
sub_page_paths(page).each do |path|
expire_page controller: 'cms/content', action: 'show', content_path: path.split('/')
end
else
expire_page controller: 'cms/content', action: 'show', content_path: page.path.split('/')
path = File.expand_path(File.join(Manage::CmsPagesController.page_cache_directory, page.path))
Dir.chdir Rails.root
Dir.glob(Pathname.new(path).relative_path_from(Pathname.new(Rails.root)).to_s, File::FNM_CASEFOLD).each do |path|
path = File.realpath(File.expand_path(path, Rails.root))
if path && File.directory?(path) raise "Cache directory name #{path} failed sanity check" unless path =~ /^#{public_dir}/
Rails.logger.info "Expire tree #{path} using rm -rf"
FileUtils.rm_rf(path, secure: true)
end
end
end
end
else
FileUtils.rm_r(Dir.glob("#{cache_dir}/*")) rescue Errno::ENOENT
end
rescue StandardError => e
Rails.logger.error "Error while clearing cache: #{e.message}" unless e.is_a?(NoMethodError)
end
end
|