bin/httpthumbnailer in httpthumbnailer-0.0.3 vs bin/httpthumbnailer in httpthumbnailer-0.0.4
- old
+ new
@@ -15,77 +15,94 @@
sinatra.set :environment, 'production'
sinatra.set :server, ['mongrel']
sinatra.set :lock, true
sinatra.set :boundary, "thumnail image data"
sinatra.set :logging, true
+sinatra.set :debug, true
sinatra.before do
- unless @thumbnailer
- @thumbnailer = Thumbnailer.new(:logger => logger)
- @thumbnailer.method('crop') do |image, spec|
- image.resize_to_fill(spec.width, spec.height)
+ logger.level = Logger::DEBUG if settings.debug == true
+ if $thumbnailer.nil?
+ $thumbnailer = Thumbnailer.new(:logger => logger)
+
+ $thumbnailer.method('crop') do |image, spec|
+ image.resize_to_fill!(spec.width, spec.height)
end
- @thumbnailer.method('fit') do |image, spec|
- image.resize_to_fit(spec.width, spec.height)
+ $thumbnailer.method('fit') do |image, spec|
+ image.resize_to_fit!(spec.width, spec.height)
end
- @thumbnailer.method('pad') do |image, spec|
- Magick::Image.new(spec.width, spec.height) {
+ $thumbnailer.method('pad') do |image, spec|
+ image.resize_to_fit!(spec.width, spec.height)
+
+ out = Magick::Image.new(spec.width, spec.height) {
self.background_color = Magick::Pixel.new(Magick::MaxRGB, Magick::MaxRGB, Magick::MaxRGB, Magick::MaxRGB) # transparent
- }.composite!(image.resize_to_fit(spec.width, spec.height), Magick::CenterGravity, Magick::OverCompositeOp)
+ }.composite!(image, Magick::CenterGravity, Magick::OverCompositeOp)
+
+ image.destroy!
+ out
end
end
end
sinatra.helpers do
- def plain_exception(exception)
+ def plain_response(msg)
headers "Content-Type" => "text/plain"
- body "Error: #{exception.class.name}: #{exception}\n"
+ body msg.gsub("\n", "\r\n") + "\r\n"
end
+
+ def plain_exception(exception)
+ plain_response("Error: #{exception.class.name}: #{exception}")
+ end
end
sinatra.get '/' do
logger.info 'hello'
end
-sinatra.put %r{/thumbnail/(.*)} do |specs|
- image = begin
- @thumbnailer.load('current', request.body)
- rescue => e
- plain_exception(e)
- halt 415
- end
+sinatra.get '/stats/images' do
+ $thumbnailer.images.to_s
+end
+sinatra.put %r{/thumbnail/(.*)} do |specs|
thumbnail_specs = ThumbnailSpecs.from_uri(specs)
+ $thumbnailer.load(request.body) do |original_image_handler|
+ status 200
+ headers "Content-Type" => "multipart/mixed; boundary=\"#{settings.boundary}\""
- status 200
- headers "Content-Type" => "multipart/mixed; boundary=\"#{settings.boundary}\""
- stream do |out|
- thumbnail_specs.each do |spec|
- logger.info "Thumbnailing: #{spec}"
- out << "--#{settings.boundary}\r\n"
+ stream do |out| # this is non blocking
+ original_image_handler.use do |original_image|
+ thumbnail_specs.each do |spec|
+ logger.info "Thumbnailing: #{spec}"
+ out << "--#{settings.boundary}\r\n"
- begin
- thumbnail = @thumbnailer.thumbnail('current', spec)
- thumbnail_data = thumbnail.to_blob do |inf|
- inf.format = spec.format
- end
+ begin
+ thumbnail_data = original_image.thumbnail(spec)
- out << "Content-Type: #{spec.mime}\r\n\r\n"
- out << thumbnail_data
-
- thumbnail_data = nil
- thumbnail.destroy!
- rescue => e
- out << "Content-Type: text/plain\r\n\r\n"
- out << "Error: #{e.class.name}: #{e}\r\n"
- ensure
- out << "\r\n"
+ out << "Content-Type: #{spec.mime}\r\n\r\n"
+ out << thumbnail_data
+ rescue => e
+ logger.error "Thumbnailing error: #{e.class.name}: #{e}: \n#{e.backtrace.join("\n")}"
+ out << "Content-Type: text/plain\r\n\r\n"
+ out << "Error: #{e.class.name}: #{e}\r\n"
+ ensure
+ out << "\r\n"
+ end
+ end
+ out << "--#{settings.boundary}--"
end
end
- out << "--#{settings.boundary}--"
end
+end
+
+sinatra.error Thumbnailer::UnsupportedMediaTypeError do
+ plain_exception(env['sinatra.error'])
+ halt 415
+end
+
+sinatra.error 404 do
+ plain_response("Resource '#{request.path_info}' not found")
end
sinatra.error do
plain_exception(env['sinatra.error'])
end