From 5d31290dbc75461ae945003e7fadae83d6c20bc9 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 7 Apr 2015 13:03:43 +1000 Subject: [PATCH] FIX: cleanup old letter avatars if needed FEATURE: use image magick version as a key for letter avatars --- app/controllers/user_avatars_controller.rb | 2 +- .../common/_discourse_javascript.html.erb | 2 +- lib/letter_avatar.rb | 27 ++++++++++++++++++- spec/components/letter_avatar_spec.rb | 15 +++++++++++ 4 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 spec/components/letter_avatar_spec.rb diff --git a/app/controllers/user_avatars_controller.rb b/app/controllers/user_avatars_controller.rb index b8f1a3492..80093b600 100644 --- a/app/controllers/user_avatars_controller.rb +++ b/app/controllers/user_avatars_controller.rb @@ -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) diff --git a/app/views/common/_discourse_javascript.html.erb b/app/views/common/_discourse_javascript.html.erb index 8f0c83dd7..7beae7ecb 100644 --- a/app/views/common/_discourse_javascript.html.erb +++ b/app/views/common/_discourse_javascript.html.erb @@ -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); }); diff --git a/lib/letter_avatar.rb b/lib/letter_avatar.rb index 48d333f4e..c05006882 100644 --- a/lib/letter_avatar.rb +++ b/lib/letter_avatar.rb @@ -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 diff --git a/spec/components/letter_avatar_spec.rb b/spec/components/letter_avatar_spec.rb new file mode 100644 index 000000000..9f5574b99 --- /dev/null +++ b/spec/components/letter_avatar_spec.rb @@ -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