Permalink
Browse files

Fixes issue #1446 by checking file type with filemagic gem.

  • Loading branch information...
1 parent 5265c9d commit 08367cfa1f480060979a4626d3617abe7e842097 @fernandomm committed Jan 28, 2013
View
@@ -46,6 +46,7 @@ group :development, :test do
gem 'paperclip', '~> 3.3'
gem 'rspec-rails', '~> 2.11'
gem 'timecop', '~> 0.5'
+ gem 'ruby-filemagic', '~> 0.4'
end
gemspec
@@ -24,6 +24,11 @@ def resource_url(thumb = false)
return nil unless (uploader = bindings[:object].send(name)).present?
thumb.present? ? uploader.send(thumb).url : uploader.url
end
+
+ def resource_path(thumb = false)
+ return nil unless (uploader = bindings[:object].send(name)).present?
+ thumb.present? ? uploader.send(thumb).current_path : uploader.current_path
+ end
end
end
end
@@ -34,6 +34,11 @@ def resource_url thumb = false
return nil unless (v = value)
thumb ? v.thumb(thumb).try(:url) : v.url
end
+
+ def resource_path thumb = false
+ return nil unless (v = value)
+ thumb ? v.thumb(thumb).try(:path) : v.path
+ end
end
end
end
@@ -1,4 +1,5 @@
require 'rails_admin/config/fields/types/string'
+require 'filemagic'
module RailsAdmin
module Config
@@ -43,7 +44,7 @@ class FileUpload < RailsAdmin::Config::Fields::Base
end
register_instance_option :image? do
- (url = resource_url.to_s) && url.split('.').last =~ /jpg|jpeg|png|gif/i
+ (url = resource_url.to_s) && FileMagic.new.file(resource_path).match(/png|jpg|jpeg|gif/)
end
register_instance_option :allowed_methods do
@@ -21,6 +21,10 @@ class Paperclip < RailsAdmin::Config::Fields::Types::FileUpload
def resource_url(thumb = false)
value.try(:url, (thumb || :original))
end
+
+ def resource_path(thumb = false)
+ value.try(:path, (thumb || :original))
+ end
end
end
end
View
@@ -34,6 +34,7 @@ gem 'mini_magick', '~> 3.4'
gem 'mlb', '~> 0.5'
gem 'paperclip', '~> 3.3'
gem 'rails_admin', :path => '../../'
+gem 'ruby-filemagic'
# Gems used only for assets and not required
# in production environments by default.

0 comments on commit 08367cf

Please sign in to comment.