Class: RailsResponsiveImages::AssetsController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
app/controllers/rails_responsive_images/assets_controller.rb

Instance Method Summary collapse

Instance Method Details

#responsive_imageObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/rails_responsive_images/assets_controller.rb', line 3

def responsive_image
  filepath = params[:filepath]
  filename = params[:filename]
  format = params[:format]
  image_filename = "#{filename}.#{format}"

  # TODO cleanup
  requested_image_size = filepath.match(/(responsive_images_)\d+/).to_s.split('_').last

  original_filepath = "#{filepath}".sub(/(responsive_images_)\d+/, '/')
    .sub(/\/\//, '') # remove duplicated slashes
    .sub(/\A\//, '') # remove leading slash

  full_original_filepath = Rails.root.join('app', 'assets', 'images', original_filepath, image_filename)
  full_responsive_filepath = Rails.root.join('app', 'assets', 'images', filepath, image_filename)

  Image.instance.create_responsive_folder!(full_responsive_filepath)

  fail ActionController::RoutingError.new('Not found') unless File.exist?(full_original_filepath)

  Image.instance.generate_responsive_image!(full_original_filepath, requested_image_size, full_responsive_filepath)

  mime_type = MIME::Types.type_for("#{full_responsive_filepath}").first.content_type
  send_file full_responsive_filepath, type: mime_type, disposition: :inline
end