Created
January 28, 2011 17:59
-
-
Save lmarburger/800655 to your computer and use it in GitHub Desktop.
Create thumbnails from images stored in S3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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