Skip to content

Commit

Permalink
cache also image file.
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Aug 16, 2010
1 parent c6092a1 commit 59d22ab
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 22 deletions.
59 changes: 38 additions & 21 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,24 @@ def render_to_surface(surface, paper, info, font)
context.move_to(margin, margin + (max_name_height - layout.pixel_size[1]) / 2)
context.show_pango_layout(layout)

screen_name = info[:screen_name]
layout = make_layout(context,
"@#{info[:screen_name]}",
"@#{screen_name}",
paper.width - image_width - margin * 3,
image_height,
font)
context.move_to(margin, paper.height - layout.pixel_size[1] - margin)
context.show_pango_layout(layout)

profile_image_url = info[:profile_image_url].gsub(/_normal\.png\z/, '.png')
pixbuf = open(profile_image_url) do |image_file|
loader = Gdk::PixbufLoader.new
loader.last_write(image_file.read)
loader.pixbuf
image_data = cache_file("images", "#{screen_name}.png") do
open(profile_image_url) do |image_file|
image_file.read
end
end
loader = Gdk::PixbufLoader.new
loader.last_write(image_data)
pixbuf = loader.pixbuf
context.save do
context.translate(paper.width - image_width - margin,
paper.height - image_height - margin)
Expand All @@ -126,19 +130,10 @@ def user_info(user_name)
end

def retrieve_user_info(user_name)
user_info_cache_dir = File.join(File.dirname(__FILE__), "var", "cache", "users")
raise Sinatra::NotFound if danger_path_component?(user_name)
user_info_cache = File.join(user_info_cache_dir, "#{user_name}.xml")
FileUtils.mkdir_p(user_info_cache_dir)
if File.exist?(user_info_cache)
xml_data = File.read(user_info_cache)
else
xml_data = open("http://twitter.com/users/#{u(user_name)}.xml") do |xml|
xml_data = cache_file("users", user_name) do
open("http://twitter.com/users/#{u(user_name)}.xml") do |xml|
xml.read
end
File.open(user_info_cache, "w") do |xml|
xml.print(xml_data)
end
end
info = {}
doc = REXML::Document.new(xml_data)
Expand Down Expand Up @@ -167,12 +162,33 @@ def danger_path_component?(component)
component == ".." or /\// =~ component
end

def cache_file(data, *path)
def cache_file(*path)
if path.any? {|component| danger_path_component?(component)}
yield
else
base_dir = File.expand_path(File.dirname(__FILE__))
cache_path = File.join(base_dir, "var", "cache", *path)
if File.exist?(cache_path)
File.open(cache_path, "rb") do |file|
file.read
end
else
FileUtils.mkdir_p(File.dirname(cache_path))
data = yield
File.open(cache_path, "wb") do |file|
file.print(data)
end
data
end
end
end

def cache_public_file(data, *path)
return if path.any? {|component| danger_path_component?(component)}
base_dir = File.expand_path(File.dirname(__FILE__))
path = File.join(base_dir, "public", *path)
FileUtils.mkdir_p(File.dirname(path))
File.open(path, "w") do |file|
File.open(path, "wb") do |file|
file.print(data)
end
end
Expand All @@ -192,7 +208,8 @@ def prepare_font_name(name)
format = "png"

nameplate = render_nameplate(user, font, format, 0.3)
cache_file(nameplate, "fonts", font, "thumbnails", "#{user}.#{format}")
cache_public_file(nameplate,
"fonts", font, "thumbnails", "#{user}.#{format}")
nameplate
rescue
raise Sinatra::NotFound
Expand All @@ -210,7 +227,7 @@ def prepare_font_name(name)
end

nameplate = render_nameplate(user, font, format)
cache_file(nameplate, "fonts", font, "#{user}.#{format}")
cache_public_file(nameplate, "fonts", font, "#{user}.#{format}")
nameplate
rescue
raise Sinatra::NotFound
Expand All @@ -235,7 +252,7 @@ def prepare_font_name(name)
end

nameplate = render_nameplate(user, "Sans", format)
cache_file(nameplate, "#{user}.#{format}")
cache_putblic_file(nameplate, "#{user}.#{format}")
nameplate
rescue
raise Sinatra::NotFound
Expand Down
1 change: 1 addition & 0 deletions var/cache/images/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*
2 changes: 1 addition & 1 deletion var/cache/users/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
*.*
*

0 comments on commit 59d22ab

Please sign in to comment.