Skip to content

Instantly share code, notes, and snippets.

@lmarburger
Created January 28, 2011 17:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lmarburger/800655 to your computer and use it in GitHub Desktop.
Save lmarburger/800655 to your computer and use it in GitHub Desktop.
Create thumbnails from images stored in S3
require 'rack/thumb'
require 'rack/s3'
class Thumber
def initialize(app)
@app = stack_app app
end
def stack_app(app)
Rack::Builder.new do
map '/thumb' do
use Rack::Thumb
run(lambda do |env|
Rails.logger.info "Thumber /thumb: #{ CloudApp.s3.bucket }#{ env['PATH_INFO'] }"
status, headers, body = Rack::S3.new(:bucket => CloudApp.s3.bucket).
call(env)
[ status, Thumber.mangle_headers(env['PATH_INFO'], headers), body ]
end)
end
# Ignore all other requests
map '/' do
run lambda { |env| app.call env }
end
end
end
def call(env)
@app.call env
end
def self.mangle_headers(path_info, headers)
headers.merge('Cache-Control' => "public; max-age=#{ 1.month }").
tap do |new_headers|
if new_headers['Content-Type'] == 'binary/octet-stream'
new_content_type = MIME::Types.type_for(path_info).first.
try(:content_type)
if new_content_type
new_headers['Content-Type'] = new_content_type
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment