FIX: cleanup old letter avatars if needed

FEATURE: use image magick version as a key for letter avatars
This commit is contained in:
Sam
2015-04-07 13:03:43 +10:00
parent f909233ed5
commit 5d31290dbc
4 changed files with 43 additions and 3 deletions

View File

@@ -23,8 +23,12 @@ class LetterAvatar
end
end
def version
"2_#{image_magick_version}"
end
def cache_path
"public/uploads/letter_avatars/#{VERSION}"
"public/uploads/letter_avatars/#{version}"
end
def generate(username, size, opts = nil)
@@ -93,6 +97,27 @@ class LetterAvatar
r,g,b = color
"'rgb(#{r},#{g},#{b})'"
end
def image_magick_version
@image_magick_version ||=
begin
Thread.new do
sleep 2
cleanup_old
end
Digest::MD5.hexdigest(`convert --version` << `convert -list font`)
end
end
def cleanup_old
skip = File.basename(cache_path)
parent_path = File.dirname(cache_path)
Dir.entries(parent_path).each do |path|
unless ['.','..'].include?(path) || path == skip
FileUtils.rm_rf(parent_path + "/" + path)
end
end
end
end
# palette of optimally disctinct colors