Class: Middleman::Thumbnailer::Rack

Inherits:
Object
  • Object
show all
Defined in:
lib/middleman-thumbnailer/extension.rb

Overview

Rack middleware to convert images on the fly

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Rack

Init

Parameters:

  • app (Class)
  • options (Hash) (defaults to: {})


127
128
129
130
131
132
133
134
135
# File 'lib/middleman-thumbnailer/extension.rb', line 127

def initialize(app, options={})
  @app = app
  @options = options

  files = DirGlob.glob(options[:images_source_dir], options[:namespace_directory], options[:filetypes])

  @original_map = ThumbnailGenerator.original_map_for_files(files, options[:dimensions])

end

Instance Method Details

#call(env) ⇒ Array

Rack interface

Parameters:

  • env (Rack::Environmemt)

Returns:

  • (Array)


140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/middleman-thumbnailer/extension.rb', line 140

def call(env)
  status, headers, response = @app.call(env)

  path = env["PATH_INFO"]

  absolute_path = File.join(@options[:source_dir], path)

  root_dir =  @options[:middleman_app].root 

  tmp_dir = File.join(root_dir, 'tmp', 'middleman-thumbnail-cache')

  if original_specs = @original_map[absolute_path]
    thumbnail_path = Pathname.new original_specs[:spec][:name]
    #the name for the generate spec filename is relative to the source dir, remove this
    thumbnail_path = thumbnail_path.relative_path_from Pathname.new(@options[:source_dir])

    cache_file = File.join tmp_dir, thumbnail_path.to_s
    cache_dir = File.dirname cache_file
    FileUtils.mkdir_p(cache_dir) unless Dir.exist?(cache_dir)

    file_info = nil
    file_data = nil

    original_file = original_specs[:original]
    original_mtime = File.mtime original_file
    if File.exist?(cache_file) && (original_mtime == File.mtime(cache_file))
      file_data = IO.read(cache_file)
      file_info = {length: ::Rack::Utils.bytesize(file_data).to_s, mime_type: ::MIME::Types.type_for(cache_file).first.to_s}
    else
      image = ThumbnailGenerator.image_for_spec(original_file, original_specs[:spec])
      file_data = image.to_blob

      file_info = {length: ::Rack::Utils.bytesize(file_data).to_s, mime_type: image.mime_type}

      File.open(cache_file, 'wb') do |file|
        file.write file_data
      end

      File.utime(original_mtime, original_mtime, cache_file)

      image.destroy!
    end

    status = 200
    headers["Content-Length"] = file_info[:length]
    headers["Content-Type"] = file_info[:mime_type]
    response = [file_data]
  end
  [status, headers, response]
end