Move html rendering to image updater

This commit is contained in:
Marcin Kulik 2015-03-30 10:23:58 +00:00
parent 7d199ad416
commit 08749dd32d
3 changed files with 40 additions and 37 deletions

View File

@ -28,12 +28,7 @@ class AsciicastsController < ApplicationController
end
format.png do
if asciicast.image_stale?
with_player_html_file do |html_path|
image_updater.update(asciicast, html_path)
end
end
image_updater.update(asciicast) if asciicast.image_stale?
redirect_to asciicast.image_url
end
end
@ -90,21 +85,7 @@ class AsciicastsController < ApplicationController
end
def image_updater
AsciicastImageUpdater.new
end
def with_player_html_file
html = render_to_string(
template: 'asciicasts/screenshot.html.slim',
layout: 'screenshot',
locals: { page: BareAsciicastPagePresenter.build(asciicast, params) }
)
Dir.mktmpdir do |dir|
path = "#{dir}/asciicast.html"
File.open(path, 'w') { |f| f.write(html) }
yield(path)
end
AsciicastImageUpdater.new(self)
end
end

View File

@ -2,7 +2,7 @@ class BareAsciicastPagePresenter
attr_reader :asciicast, :playback_options
def self.build(asciicast, playback_options)
def self.build(asciicast, playback_options = {})
decorated_asciicast = asciicast.decorate
playback_options = {

View File

@ -1,31 +1,53 @@
class AsciicastImageUpdater
PIXEL_DENSITY = 2
attr_reader :rasterizer, :image_inspector
attr_reader :template_renderer, :rasterizer, :image_inspector
def initialize(rasterizer = Rasterizer.new, image_inspector = ImageInspector.new)
def initialize(template_renderer, rasterizer = Rasterizer.new, image_inspector = ImageInspector.new)
@template_renderer = template_renderer
@rasterizer = rasterizer
@image_inspector = image_inspector
end
def update(asciicast, page_path)
def update(asciicast)
Dir.mktmpdir do |dir|
page_path = "#{dir}/asciicast.html"
image_path = "#{dir}/#{asciicast.image_filename}"
rasterizer.generate_image(page_path, image_path, 'png', '.asciinema-player', PIXEL_DENSITY)
generate_html_file(asciicast, page_path)
generate_png_file(page_path, image_path)
image_width, image_height = image_inspector.get_size(image_path)
update_asciicast(asciicast, image_path, image_width, image_height)
end
end
private
def generate_html_file(asciicast, path)
html = template_renderer.render_to_string(
template: 'asciicasts/screenshot.html.slim',
layout: 'screenshot',
locals: { page: BareAsciicastPagePresenter.build(asciicast) },
)
File.open(path, 'w') { |f| f.write(html) }
end
def generate_png_file(page_path, image_path)
rasterizer.generate_image(page_path, image_path, 'png', '.asciinema-player', PIXEL_DENSITY)
end
def update_asciicast(asciicast, image_path, image_width, image_height)
File.open(image_path) do |f|
asciicast.image = f
end
width, height = image_inspector.get_size(image_path)
# "display" size is 1/PIXEL_DENSITY of the actual one
asciicast.image_width = width / PIXEL_DENSITY
asciicast.image_height = height / PIXEL_DENSITY
asciicast.image_width = image_width / PIXEL_DENSITY
asciicast.image_height = image_height / PIXEL_DENSITY
asciicast.save!
end
end
end