Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/crafterm/attachment_fu
Browse files Browse the repository at this point in the history
  • Loading branch information
Ismael Celis committed May 22, 2008
2 parents 2996bcc + 834553a commit 5bd1cbd
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/technoweenie/attachment_fu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,16 @@ def uploaded_data() nil; end
#
# TODO: Allow it to work with Merb tempfiles too.
def uploaded_data=(file_data)
return nil if file_data.nil? || file_data.size == 0
self.content_type = file_data.content_type
self.filename = file_data.original_filename if respond_to?(:filename)
if file_data.respond_to?(:content_type)
return nil if file_data.size == 0
self.content_type = file_data.content_type
self.filename = file_data.original_filename if respond_to?(:filename)
else
return nil if file_data.blank? || file_data['size'] == 0
self.content_type = file_data['content_type']
self.filename = file_data['filename']
file_data = file_data['tempfile']
end
if file_data.is_a?(StringIO)
file_data.rewind
self.temp_data = file_data.read
Expand Down Expand Up @@ -364,6 +371,7 @@ def random_tempfile_filename
end

def sanitize_filename(filename)
return unless filename
returning filename.strip do |name|
# NOTE: File.basename doesn't work right with Windows paths on Unix
# get only the filename, not the whole path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ def resize_image(img, size)
processor.render do |result|
self.width = result.extent.size.width if respond_to?(:width)
self.height = result.extent.size.height if respond_to?(:height)

# Get a new temp_path for the image before saving
self.temp_path = Tempfile.new(random_tempfile_filename, Technoweenie::AttachmentFu.tempfile_path).path
result.save self.temp_path, OSX::NSJPEGFileType
self.size = File.size(self.temp_path)
end
Expand Down
20 changes: 20 additions & 0 deletions test/base_attachment_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ def test_should_create_file_from_uploaded_file
end
end

def test_should_create_file_from_merb_temp_file
assert_created do
attachment = upload_merb_file :filename => '/files/foo.txt'
assert_valid attachment
assert !attachment.db_file.new_record? if attachment.respond_to?(:db_file)
assert attachment.image?
assert !attachment.size.zero?
#assert_equal 3, attachment.size
assert_nil attachment.width
assert_nil attachment.height
end
end

def test_reassign_attribute_data
assert_created 1 do
attachment = upload_file :filename => '/files/rails.png'
Expand Down Expand Up @@ -54,4 +67,11 @@ def test_should_save_without_updating_file
assert !attachment.save_attachment?
assert_nothing_raised { attachment.save! }
end

def test_should_handle_nil_file_upload
attachment = attachment_model.create :uploaded_data => ''
assert_raise ActiveRecord::RecordInvalid do
attachment.save!
end
end
end
3 changes: 3 additions & 0 deletions test/basic_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ def test_should_sanitize_filenames

@attachment.filename = 'f o!O-.bar'
assert_equal 'f_o_O-.bar', @attachment.filename

@attachment.filename = nil
assert_nil @attachment.filename
end

def test_should_convert_thumbnail_name
Expand Down
10 changes: 10 additions & 0 deletions test/extra_attachment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ def test_should_create_file_from_uploaded_file
end
end

def test_should_create_file_from_merb_temp_file
assert_created do
attachment = upload_merb_file :filename => '/files/foo.txt'
assert_valid attachment
assert !attachment.db_file.new_record? if attachment.respond_to?(:db_file)
assert attachment.image?
assert !attachment.size.zero?
end
end

def test_should_create_image_from_uploaded_file_with_custom_content_type
assert_created do
attachment = upload_file :content_type => 'foo/bar', :filename => '/files/rails.png'
Expand Down
6 changes: 6 additions & 0 deletions test/processors/core_image_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ def test_should_resize_image
# test geometry string
assert_equal 31, geo.width
assert_equal 41, geo.height

# This makes sure that we didn't overwrite the original file
# and will end up with a thumbnail instead of the original
assert_equal 42, attachment.width
assert_equal 55, attachment.height

end
else
def test_flunk
Expand Down
8 changes: 8 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ def upload_file(options = {})
end
end

def upload_merb_file(options = {})
use_temp_file options[:filename] do |file|
att = attachment_model.create :uploaded_data => {"size" => file.size, "content_type" => options[:content_type] || 'image/png', "filename" => file, 'tempfile' => fixture_file_upload(file, options[:content_type] || 'image/png')}
att.reload unless att.new_record?
return att
end
end

def use_temp_file(fixture_filename)
temp_path = File.join('/tmp', File.basename(fixture_filename))
FileUtils.mkdir_p File.join(fixture_path, 'tmp')
Expand Down

0 comments on commit 5bd1cbd

Please sign in to comment.