Rewrite extension detection + add tests

pull/6107/head
Carla Iriberri 9 years ago
parent 87650d239d
commit 87b1c209ba

@ -8,7 +8,7 @@ class Asset < Sequel::Model
PUBLIC_ATTRIBUTES = %w{ id public_url user_id kind }
VALID_EXTENSIONS = %w{ jpg gif png svg }
VALID_EXTENSIONS = %w{ .jpeg .jpg .gif .png .svg }
attr_accessor :asset_file, :url
@ -66,17 +66,19 @@ class Asset < Sequel::Model
metadata = CartoDB::ImageMetadata.new(@file.path)
errors.add(:file, "is too big, 1024x1024 max") if metadata.width > 1024 || metadata.height > 1024
# If metadata reports no size, 99% sure not valid, so out
errors.add(:file, "doesn't appears to be an image") if metadata.width == 0 || metadata.height == 0
errors.add(:file, "doesn't appear to be an image") if metadata.width == 0 || metadata.height == 0
rescue => e
errors.add(:file, "error while uploading: #{e.message}")
end
def asset_file_extension
(asset_file.respond_to?(:original_filename) ? asset_file.original_filename : asset_file)
.split(".")
.last
.slice(0, 3) # Filename might include a postfix hash (e.g. Rack::Test::UploadedFile adds it)
.downcase
filename = asset_file.respond_to?(:original_filename) ? asset_file.original_filename : asset_file
extension = File.extname(filename).downcase
# Filename might include a postfix hash -- Rack::Test::UploadedFile adds it
extension.gsub!(/\d+-\w+-\w+\z/, '') if Rails.env.test?
extension if VALID_EXTENSIONS.include?(extension)
end
##

@ -35,7 +35,7 @@ describe Asset do
it 'validates file correct metadata' do
asset = Asset.new user_id: @user.id, asset_file: (Rails.root + 'spec/support/data/fake_png.png').to_s
asset.valid?.should be_false
asset.errors.full_messages.should include("file doesn't appears to be an image")
asset.errors.full_messages.should include("file doesn't appear to be an image")
end
it 'validates file size' do

@ -53,6 +53,22 @@ describe "Assets API" do
end
end
it "finds image file extension" do
asset = FactoryGirl.create(:asset, user_id: $user_1.id)
Asset::VALID_EXTENSIONS.each do |extension|
asset_name = "cartofante" + extension
asset.stubs(:asset_file).returns(asset_name)
asset.asset_file_extension.should == extension
end
end
it "detects incorrect image file extension" do
asset = FactoryGirl.create(:asset, user_id: $user_1.id)
asset.stubs(:asset_file).returns("cartofante.gifv")
asset.asset_file_extension.should == nil
end
it "deletes an asset" do
FactoryGirl.create(:asset, user_id: $user_1.id)
$user_1.reload

Loading…
Cancel
Save