Class: Novector::VectorFallbackController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
app/controllers/novector/vector_fallback_controller.rb

Instance Method Summary collapse

Instance Method Details

#render_fallbackObject

Raises:

  • (ActionController::RoutingError)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/controllers/novector/vector_fallback_controller.rb', line 30

def render_fallback
  width = Integer(params[:width])
  height = Integer(params[:height])
  source = params[:src]
  format = params[:format]
  
  raise 'Asset is not an SVG file.' unless source =~ /\.svg$/
  raise 'Unsupported output format.' unless ['png', 'gif'].include? format
  
  # Open asset
  asset = Rails.application.assets.find_asset source
  raise ActionController::RoutingError.new('Asset not found') unless asset
  asset_mtime = File.mtime(asset.pathname)

  # If the asset is up to date on the client, don't waste the bandwidth
  last_time = Time.rfc2822(request.env['HTTP_IF_MODIFIED_SINCE']) rescue nil
  if last_time and asset_mtime  < last_time
    render :text => '304 Not Modified', :status => 304
    return
  end
  
  cache_name = '#{source}_#{asset_mtime}_#{width}_#{height}_#{format}'
  blob = Rails.cache.read(cache_name)
  unless blob
    blob = convert_svg(asset.pathname, width, height, format)
    Rails.cache.write(cache_name, blob)
  end

  # Send asset
  response.header['Last-Modified'] = asset_mtime.to_s
  send_data(blob, :type => "image/#{format}", :disposition => 'inline')
end