mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-23 15:48:43 -05:00
FIX: cleanup old letter avatars if needed
FEATURE: use image magick version as a key for letter avatars
This commit is contained in:
parent
f909233ed5
commit
5d31290dbc
4 changed files with 43 additions and 3 deletions
|
@ -24,7 +24,7 @@ class UserAvatarsController < ApplicationController
|
|||
params.require(:version)
|
||||
params.require(:size)
|
||||
|
||||
return render_dot if params[:version].to_i > LetterAvatar::VERSION
|
||||
return render_dot if params[:version] != LetterAvatar.version
|
||||
|
||||
image = LetterAvatar.generate(params[:username].to_s, params[:size].to_i)
|
||||
|
||||
|
|
|
@ -32,7 +32,7 @@
|
|||
Discourse.BaseUri = '<%= Discourse::base_uri "/" %>';
|
||||
Discourse.Environment = '<%= Rails.env %>';
|
||||
Discourse.SiteSettings = PreloadStore.get('siteSettings');
|
||||
Discourse.LetterAvatarVersion = <%= LetterAvatar::VERSION %>;
|
||||
Discourse.LetterAvatarVersion = '<%= LetterAvatar.version %>';
|
||||
PreloadStore.get("customEmoji").forEach(function(emoji) {
|
||||
Discourse.Dialect.registerEmoji(emoji.name, emoji.url);
|
||||
});
|
||||
|
|
|
@ -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
|
||||
|
|
15
spec/components/letter_avatar_spec.rb
Normal file
15
spec/components/letter_avatar_spec.rb
Normal file
|
@ -0,0 +1,15 @@
|
|||
require 'spec_helper'
|
||||
require 'letter_avatar'
|
||||
|
||||
describe LetterAvatar do
|
||||
it "can cleanup correctly" do
|
||||
path = LetterAvatar.cache_path
|
||||
|
||||
FileUtils.mkdir_p(path + "junk")
|
||||
LetterAvatar.generate("test", 100)
|
||||
|
||||
LetterAvatar.cleanup_old
|
||||
|
||||
Dir.entries(File.dirname(path)).length.should == 3
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue