mirror of
https://github.com/tildeverse/lobsters
synced 2024-06-14 04:56:39 +00:00
avatar caching
This commit is contained in:
parent
60f2bbd8c1
commit
2b02e718a5
38
app/controllers/avatars_controller.rb
Normal file
38
app/controllers/avatars_controller.rb
Normal file
|
@ -0,0 +1,38 @@
|
|||
class AvatarsController < ApplicationController
|
||||
ALLOWED_SIZES = [ 16, 32, 100, 200 ]
|
||||
|
||||
CACHE_DIR = "#{Rails.root}/public/avatars/"
|
||||
|
||||
def show
|
||||
username, size = params[:username_size].to_s.scan(/\A(.+)-(\d+)\z/).first
|
||||
size = size.to_i
|
||||
|
||||
if !ALLOWED_SIZES.include?(size)
|
||||
raise ActionController::RoutingError.new("invalid size")
|
||||
end
|
||||
|
||||
if !username.match(User::VALID_USERNAME)
|
||||
raise ActionController::RoutingError.new("invalid user name")
|
||||
end
|
||||
|
||||
u = User.where(:username => username).first!
|
||||
|
||||
if !(av = u.fetched_avatar(size))
|
||||
raise ActionController::RoutingError.new("failed fetching avatar")
|
||||
end
|
||||
|
||||
if !Dir.exists?(CACHE_DIR)
|
||||
Dir.mkdir(CACHE_DIR)
|
||||
end
|
||||
|
||||
File.open("#{CACHE_DIR}/.#{u.username}-#{size}.png", "wb+") do |f|
|
||||
f.write av
|
||||
end
|
||||
|
||||
File.rename("#{CACHE_DIR}/.#{u.username}-#{size}.png",
|
||||
"#{CACHE_DIR}/#{u.username}-#{size}.png")
|
||||
|
||||
response.headers["Expires"] = 1.hour.from_now.httpdate
|
||||
send_data av, :type => "image/png", :disposition => "inline"
|
||||
end
|
||||
end
|
|
@ -142,9 +142,7 @@ class User < ActiveRecord::Base
|
|||
end
|
||||
|
||||
def avatar_url(size = 100)
|
||||
"https://www.gravatar.com/avatar/" +
|
||||
Digest::MD5.hexdigest(self.email.strip.downcase) +
|
||||
"?r=pg&d=identicon&s=#{size}"
|
||||
Rails.application.root_url + "avatars/#{self.username}-#{size}.png"
|
||||
end
|
||||
|
||||
def average_karma
|
||||
|
@ -266,6 +264,25 @@ class User < ActiveRecord::Base
|
|||
Keystore.value_for("user:#{self.id}:comments_posted").to_i
|
||||
end
|
||||
|
||||
def fetched_avatar(size = 100)
|
||||
gravatar_url = "https://www.gravatar.com/avatar/" <<
|
||||
Digest::MD5.hexdigest(self.email.strip.downcase) <<
|
||||
"?r=pg&d=identicon&s=#{size}"
|
||||
|
||||
begin
|
||||
s = Sponge.new
|
||||
s.timeout = 3
|
||||
res = s.fetch(gravatar_url)
|
||||
if res.present?
|
||||
return res
|
||||
end
|
||||
rescue => e
|
||||
Rails.logger.error "error fetching #{gravatar_url}: #{e.message}"
|
||||
end
|
||||
|
||||
nil
|
||||
end
|
||||
|
||||
def update_comments_posted_count!
|
||||
Keystore.put("user:#{self.id}:comments_posted", self.comments.active.count)
|
||||
end
|
||||
|
|
|
@ -106,6 +106,8 @@ Lobsters::Application.routes.draw do
|
|||
get "/u" => "users#tree"
|
||||
get "/u/:username" => "users#show", :as => "user", :format => /html|json/
|
||||
|
||||
get "/avatars/:username_size.png" => "avatars#show"
|
||||
|
||||
post "/users/:username/ban" => "users#ban", :as => "user_ban"
|
||||
post "/users/:username/unban" => "users#unban", :as => "user_unban"
|
||||
post "/users/:username/disable_invitation" => "users#disable_invitation", :as => "user_disable_invite"
|
||||
|
|
Loading…
Reference in New Issue
Block a user