Skip to content

Instantly share code, notes, and snippets.

@tastycode
Created June 5, 2011 02:16
Show Gist options
  • Save tastycode/1008583 to your computer and use it in GitHub Desktop.
Save tastycode/1008583 to your computer and use it in GitHub Desktop.
Gem Flaws
root@tara:~/tmp/1008583# locate images_controller.rb
/usr/lib/ruby/gems/1.8/gems/refinerycms-images-0.9.9.21/app/controllers/admin/images_controller.rb
root@tara:~/tmp/1008583# vim `locate images_controller.rb`
# grep -R Dragonfly .
./lib/refinerycms-images.rb: app_images = Dragonfly[:images]
./lib/refinerycms-images.rb: app_images.analyser.register(Dragonfly::Analysis::ImageMagickAnalyser)
./lib/refinerycms-images.rb: app_images.analyser.register(Dragonfly::Analysis::FileCommandAnalyser)
./lib/refinerycms-images.rb: app.config.middleware.insert_after 'Rack::Lock', 'Dragonfly::Middleware', :images, '/system/images'
./lib/refinerycms-images.rb: app.config.middleware.insert_before 'Dragonfly::Middleware', 'Rack::Cache', {
./config/routes.rb: match '/system/images/*dragonfly', :to => Dragonfly[:images]
./app/controllers/admin/images_controller.rb: rescue Dragonfly::FunctionManager::UnableToHandle
app.analyser.register(Dragonfly::Analysis::FileCommandAnalyser) do |a|
a.use_filesystem = false # defaults to true
a.file_command = '/opt/local/bin/file' # defaults to 'file'
a.num_bytes_to_check = 1024 # defaults to 255 - only applies if not using the filesystem
end
module Dragonfly
module Analysis
class FileCommandAnalyser
include Configurable
configurable_attr :file_command, "file"
configurable_attr :use_filesystem, false
configurable_attr :num_bytes_to_check, 255
def mime_type(temp_object)
content_type = if use_filesystem
`#{file_command} -b --mime '#{temp_object.path}'`
else
IO.popen("#{file_command} -b --mime -", 'r+') do |io|
if num_bytes_to_check
io.write temp_object.data[0, num_bytes_to_check]
else
io.write temp_object.data
end
io.close_write
io.read
end
end.split(';').first
content_type.strip if content_type
end
end
end
end
git diff refinerycms-images.rb
diff --git a/refinerycms-images.rb b/refinerycms-images.rb
index 3fef0f7..e3d3caa 100644
--- a/refinerycms-images.rb
+++ b/refinerycms-images.rb
@@ -31,7 +31,9 @@ module Refinery
app_images.define_macro(ActiveRecord::Base, :image_accessor)
app_images.analyser.register(Dragonfly::Analysis::ImageMagickAnalyser)
- app_images.analyser.register(Dragonfly::Analysis::FileCommandAnalyser)
+ app_images.analyser.register(Dragonfly::Analysis::FileCommandAnalyser) do |a|
+ a.use_filesystem = true
+ end
# This url_suffix makes it so that dragonfly urls work in traditional
# situations where the filename and extension are required,
require 'dragonfly'
require 'rack/cache'
require 'refinerycms-core'
module Refinery
module Images
class << self
attr_accessor :root
def root
@root ||= Pathname.new(File.expand_path('../../', __FILE__))
end
end
class Engine < ::Rails::Engine
initializer 'serve static assets' do |app|
app.middleware.insert_after ::ActionDispatch::Static, ::ActionDispatch::Static, "#{root}/public"
end
initializer 'images-with-dragonfly' do |app|
app_images = Dragonfly[:images]
app_images.configure_with(:imagemagick)
app_images.configure_with(:rails) do |c|
c.datastore.root_path = Rails.root.join('public', 'system', 'images').to_s
c.url_path_prefix = '/system/images'
c.secret = RefinerySetting.find_or_set(:dragonfly_secret,
Array.new(24) { rand(256) }.pack('C*').unpack('H*').first)
end
app_images.configure_with(:heroku, ENV['S3_BUCKET']) if Refinery.s3_backend
app_images.define_macro(ActiveRecord::Base, :image_accessor)
app_images.analyser.register(Dragonfly::Analysis::ImageMagickAnalyser)
app_images.analyser.register(Dragonfly::Analysis::FileCommandAnalyser)
# This url_suffix makes it so that dragonfly urls work in traditional
# situations where the filename and extension are required, e.g. lightbox.
# What this does is takes the url that is about to be produced e.g.
# /system/images/BAhbB1sHOgZmIiMyMDEwLzA5LzAxL1NTQ19DbGllbnRfQ29uZi5qcGdbCDoGcDoKdGh1bWIiDjk0MngzNjAjYw
# and adds the filename onto the end (say the image was 'refinery_is_awesome.jpg')
# /system/images/BAhbB1sHOgZmIiMyMDEwLzA5LzAxL1NTQ19DbGllbnRfQ29uZi5qcGdbCDoGcDoKdGh1bWIiDjk0MngzNjAjYw/refinery_is_awesome.jpg
# Officially the way to do it, from: http://markevans.github.com/dragonfly/file.URLs.html
app_images.url_suffix = proc{|job|
object_file_name = job.uid_basename.gsub(%r{^(\d{4}|\d{2})[_/]\d{2}[_/]\d{2}[_/]\d{2,3}[_/](\d{2}/\d{2}/\d{3}/)?}, '')
"/#{object_file_name}#{job.encoded_extname || job.uid_extname}"
}
### Extend active record ###
app.config.middleware.insert_after 'Rack::Lock', 'Dragonfly::Middleware', :images, '/system/images'
app.config.middleware.insert_before 'Dragonfly::Middleware', 'Rack::Cache', {
:verbose => Rails.env.development?,
:metastore => "file:#{Rails.root.join('tmp', 'dragonfly', 'cache', 'meta')}",
:entitystore => "file:#{Rails.root.join('tmp', 'dragonfly', 'cache', 'body')}"
}
end
config.after_initialize do
::Refinery::Plugin.register do |plugin|
plugin.name = 'refinery_images'
plugin.directory = 'images'
plugin.version = %q{0.9.9.21}
plugin.menu_match = /(refinery|admin)\/image(_dialog)?s$/
plugin.activity = {
:class => Image
}
end
end
end
end
end
::Refinery.engines << 'dashboard'
dragonfly (0.8.5) lib/dragonfly/analysis/file_command_analyser.rb:16:in `popen'
dragonfly (0.8.5) lib/dragonfly/analysis/file_command_analyser.rb:16:in `mime_type'
dragonfly (0.8.5) lib/dragonfly/function_manager.rb:37:in `call'
dragonfly (0.8.5) lib/dragonfly/function_manager.rb:37:in `call_last'
dragonfly (0.8.5) lib/dragonfly/function_manager.rb:36:in `catch'
dragonfly (0.8.5) lib/dragonfly/function_manager.rb:36:in `call_last'
dragonfly (0.8.5) lib/dragonfly/function_manager.rb:35:in `each'
dragonfly (0.8.5) lib/dragonfly/function_manager.rb:35:in `call_last'
dragonfly (0.8.5) lib/dragonfly/analyser.rb:26:in `analyse'
dragonfly (0.8.5) lib/dragonfly/job.rb:200:in `analyse'
(eval):3:in `mime_type'
dragonfly (0.8.5) lib/dragonfly/active_model_extensions/attachment.rb:152:in `send'
dragonfly (0.8.5) lib/dragonfly/active_model_extensions/attachment.rb:152:in `set_magic_attributes'
dragonfly (0.8.5) lib/dragonfly/active_model_extensions/attachment.rb:152:in `each'
dragonfly (0.8.5) lib/dragonfly/active_model_extensions/attachment.rb:152:in `set_magic_attributes'
dragonfly (0.8.5) lib/dragonfly/active_model_extensions/attachment.rb:33:in `assign'
dragonfly (0.8.5) lib/dragonfly/active_model_extensions/class_methods.rb:22:in `image='
activerecord (3.0.7) lib/active_record/base.rb:1559:in `send'
activerecord (3.0.7) lib/active_record/base.rb:1559:in `attributes='
activerecord (3.0.7) lib/active_record/base.rb:1555:in `each'
activerecord (3.0.7) lib/active_record/base.rb:1555:in `attributes='
activerecord (3.0.7) lib/active_record/base.rb:1407:in `initialize'
activerecord (3.0.7) lib/active_record/base.rb:497:in `new'
activerecord (3.0.7) lib/active_record/base.rb:497:in `create'
refinerycms-images (0.9.9.21) app/controllers/admin/images_controller.rb:48:in `create'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment