winton-attachment_fu 1.0.5 → 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. metadata +16 -14
  2. data/CHANGELOG +0 -35
  3. data/README +0 -186
  4. data/Rakefile +0 -22
  5. data/amazon_s3.yml.tpl +0 -14
  6. data/init.rb +0 -17
  7. data/install.rb +0 -5
  8. data/lib/geometry.rb +0 -93
  9. data/lib/technoweenie/attachment_fu.rb +0 -473
  10. data/lib/technoweenie/attachment_fu/backends/db_file_backend.rb +0 -39
  11. data/lib/technoweenie/attachment_fu/backends/file_system_backend.rb +0 -101
  12. data/lib/technoweenie/attachment_fu/backends/s3_backend.rb +0 -303
  13. data/lib/technoweenie/attachment_fu/processors/core_image_processor.rb +0 -59
  14. data/lib/technoweenie/attachment_fu/processors/gd2_processor.rb +0 -54
  15. data/lib/technoweenie/attachment_fu/processors/image_science_processor.rb +0 -61
  16. data/lib/technoweenie/attachment_fu/processors/mini_magick_processor.rb +0 -59
  17. data/lib/technoweenie/attachment_fu/processors/rmagick_processor.rb +0 -54
  18. data/test/backends/db_file_test.rb +0 -16
  19. data/test/backends/file_system_test.rb +0 -80
  20. data/test/backends/remote/s3_test.rb +0 -119
  21. data/test/base_attachment_tests.rb +0 -77
  22. data/test/basic_test.rb +0 -70
  23. data/test/database.yml +0 -18
  24. data/test/extra_attachment_test.rb +0 -67
  25. data/test/fixtures/attachment.rb +0 -148
  26. data/test/fixtures/files/fake/rails.png +0 -0
  27. data/test/fixtures/files/foo.txt +0 -1
  28. data/test/fixtures/files/rails.png +0 -0
  29. data/test/geometry_test.rb +0 -101
  30. data/test/processors/core_image_test.rb +0 -37
  31. data/test/processors/gd2_test.rb +0 -31
  32. data/test/processors/image_science_test.rb +0 -31
  33. data/test/processors/mini_magick_test.rb +0 -31
  34. data/test/processors/rmagick_test.rb +0 -255
  35. data/test/schema.rb +0 -108
  36. data/test/test_helper.rb +0 -150
  37. data/test/validation_test.rb +0 -55
  38. data/vendor/red_artisan/core_image/filters/color.rb +0 -27
  39. data/vendor/red_artisan/core_image/filters/effects.rb +0 -31
  40. data/vendor/red_artisan/core_image/filters/perspective.rb +0 -25
  41. data/vendor/red_artisan/core_image/filters/quality.rb +0 -25
  42. data/vendor/red_artisan/core_image/filters/scale.rb +0 -47
  43. data/vendor/red_artisan/core_image/filters/watermark.rb +0 -32
  44. data/vendor/red_artisan/core_image/processor.rb +0 -123
@@ -1,54 +0,0 @@
1
- require 'rubygems'
2
- require 'gd2'
3
- module Technoweenie # :nodoc:
4
- module AttachmentFu # :nodoc:
5
- module Processors
6
- module Gd2Processor
7
- def self.included(base)
8
- base.send :extend, ClassMethods
9
- base.alias_method_chain :process_attachment, :processing
10
- end
11
-
12
- module ClassMethods
13
- # Yields a block containing a GD2 Image for the given binary data.
14
- def with_image(file, &block)
15
- im = GD2::Image.import(file)
16
- block.call(im)
17
- end
18
- end
19
-
20
- protected
21
- def process_attachment_with_processing
22
- return unless process_attachment_without_processing && image?
23
- with_image do |img|
24
- resize_image_or_thumbnail! img
25
- self.width = img.width
26
- self.height = img.height
27
- callback_with_args :after_resize, img
28
- end
29
- end
30
-
31
- # Performs the actual resizing operation for a thumbnail
32
- def resize_image(img, size)
33
- size = size.first if size.is_a?(Array) && size.length == 1
34
- if size.is_a?(Fixnum) || (size.is_a?(Array) && size.first.is_a?(Fixnum))
35
- if size.is_a?(Fixnum)
36
- # Borrowed from image science's #thumbnail method and adapted
37
- # for this.
38
- scale = size.to_f / (img.width > img.height ? img.width.to_f : img.height.to_f)
39
- img.resize!((img.width * scale).round(1), (img.height * scale).round(1), false)
40
- else
41
- img.resize!(size.first, size.last, false)
42
- end
43
- else
44
- w, h = [img.width, img.height] / size.to_s
45
- img.resize!(w, h, false)
46
- end
47
- self.temp_path = random_tempfile_filename
48
- self.size = img.export(self.temp_path)
49
- end
50
-
51
- end
52
- end
53
- end
54
- end
@@ -1,61 +0,0 @@
1
- require 'image_science'
2
- module Technoweenie # :nodoc:
3
- module AttachmentFu # :nodoc:
4
- module Processors
5
- module ImageScienceProcessor
6
- def self.included(base)
7
- base.send :extend, ClassMethods
8
- base.alias_method_chain :process_attachment, :processing
9
- end
10
-
11
- module ClassMethods
12
- # Yields a block containing an Image Science image for the given binary data.
13
- def with_image(file, &block)
14
- ::ImageScience.with_image file, &block
15
- end
16
- end
17
-
18
- protected
19
- def process_attachment_with_processing
20
- return unless process_attachment_without_processing && image?
21
- with_image do |img|
22
- self.width = img.width if respond_to?(:width)
23
- self.height = img.height if respond_to?(:height)
24
- resize_image_or_thumbnail! img
25
- end
26
- end
27
-
28
- # Performs the actual resizing operation for a thumbnail
29
- def resize_image(img, size)
30
- # create a dummy temp file to write to
31
- # ImageScience doesn't handle all gifs properly, so it converts them to
32
- # pngs for thumbnails. It has something to do with trying to save gifs
33
- # with a larger palette than 256 colors, which is all the gif format
34
- # supports.
35
- filename.sub! /gif$/, 'png'
36
- content_type.sub!(/gif$/, 'png')
37
- self.temp_path = write_to_temp_file(filename)
38
- grab_dimensions = lambda do |img|
39
- self.width = img.width if respond_to?(:width)
40
- self.height = img.height if respond_to?(:height)
41
- img.save self.temp_path
42
- self.size = File.size(self.temp_path)
43
- callback_with_args :after_resize, img
44
- end
45
-
46
- size = size.first if size.is_a?(Array) && size.length == 1
47
- if size.is_a?(Fixnum) || (size.is_a?(Array) && size.first.is_a?(Fixnum))
48
- if size.is_a?(Fixnum)
49
- img.thumbnail(size, &grab_dimensions)
50
- else
51
- img.resize(size[0], size[1], &grab_dimensions)
52
- end
53
- else
54
- new_size = [img.width, img.height] / size.to_s
55
- img.resize(new_size[0], new_size[1], &grab_dimensions)
56
- end
57
- end
58
- end
59
- end
60
- end
61
- end
@@ -1,59 +0,0 @@
1
- require 'mini_magick'
2
- module Technoweenie # :nodoc:
3
- module AttachmentFu # :nodoc:
4
- module Processors
5
- module MiniMagickProcessor
6
- def self.included(base)
7
- base.send :extend, ClassMethods
8
- base.alias_method_chain :process_attachment, :processing
9
- end
10
-
11
- module ClassMethods
12
- # Yields a block containing an MiniMagick Image for the given binary data.
13
- def with_image(file, &block)
14
- begin
15
- binary_data = file.is_a?(MiniMagick::Image) ? file : MiniMagick::Image.from_file(file) unless !Object.const_defined?(:MiniMagick)
16
- rescue
17
- # Log the failure to load the image.
18
- logger.debug("Exception working with image: #{$!}")
19
- binary_data = nil
20
- end
21
- block.call binary_data if block && binary_data
22
- ensure
23
- !binary_data.nil?
24
- end
25
- end
26
-
27
- protected
28
- def process_attachment_with_processing
29
- return unless process_attachment_without_processing
30
- with_image do |img|
31
- resize_image_or_thumbnail! img
32
- self.width = img[:width] if respond_to?(:width)
33
- self.height = img[:height] if respond_to?(:height)
34
- callback_with_args :after_resize, img
35
- end if image?
36
- end
37
-
38
- # Performs the actual resizing operation for a thumbnail
39
- def resize_image(img, size)
40
- size = size.first if size.is_a?(Array) && size.length == 1
41
- img.combine_options do |commands|
42
- commands.strip unless attachment_options[:keep_profile]
43
- if size.is_a?(Fixnum) || (size.is_a?(Array) && size.first.is_a?(Fixnum))
44
- if size.is_a?(Fixnum)
45
- size = [size, size]
46
- commands.resize(size.join('x'))
47
- else
48
- commands.resize(size.join('x') + '!')
49
- end
50
- else
51
- commands.resize(size.to_s)
52
- end
53
- end
54
- self.temp_path = img
55
- end
56
- end
57
- end
58
- end
59
- end
@@ -1,54 +0,0 @@
1
- require 'RMagick'
2
- module Technoweenie # :nodoc:
3
- module AttachmentFu # :nodoc:
4
- module Processors
5
- module RmagickProcessor
6
- def self.included(base)
7
- base.send :extend, ClassMethods
8
- base.alias_method_chain :process_attachment, :processing
9
- end
10
-
11
- module ClassMethods
12
- # Yields a block containing an RMagick Image for the given binary data.
13
- def with_image(file, &block)
14
- begin
15
- binary_data = file.is_a?(Magick::Image) ? file : Magick::Image.read(file).first unless !Object.const_defined?(:Magick)
16
- rescue
17
- # Log the failure to load the image. This should match ::Magick::ImageMagickError
18
- # but that would cause acts_as_attachment to require rmagick.
19
- logger.debug("Exception working with image: #{$!}")
20
- binary_data = nil
21
- end
22
- block.call binary_data if block && binary_data
23
- ensure
24
- !binary_data.nil?
25
- end
26
- end
27
-
28
- protected
29
- def process_attachment_with_processing
30
- return unless process_attachment_without_processing
31
- with_image do |img|
32
- resize_image_or_thumbnail! img
33
- self.width = img.columns if respond_to?(:width)
34
- self.height = img.rows if respond_to?(:height)
35
- callback_with_args :after_resize, img
36
- end if image?
37
- end
38
-
39
- # Performs the actual resizing operation for a thumbnail
40
- def resize_image(img, size)
41
- size = size.first if size.is_a?(Array) && size.length == 1 && !size.first.is_a?(Fixnum)
42
- if size.is_a?(Fixnum) || (size.is_a?(Array) && size.first.is_a?(Fixnum))
43
- size = [size, size] if size.is_a?(Fixnum)
44
- img.thumbnail!(*size)
45
- else
46
- img.change_geometry(size.to_s) { |cols, rows, image| image.resize!(cols<1 ? 1 : cols, rows<1 ? 1 : rows) }
47
- end
48
- img.strip! unless attachment_options[:keep_profile]
49
- self.temp_path = write_to_temp_file(img.to_blob)
50
- end
51
- end
52
- end
53
- end
54
- end
@@ -1,16 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper'))
2
-
3
- class DbFileTest < Test::Unit::TestCase
4
- include BaseAttachmentTests
5
- attachment_model Attachment
6
-
7
- def test_should_call_after_attachment_saved(klass = Attachment)
8
- attachment_model.saves = 0
9
- assert_created do
10
- upload_file :filename => '/files/rails.png'
11
- end
12
- assert_equal 1, attachment_model.saves
13
- end
14
-
15
- test_against_subclass :test_should_call_after_attachment_saved, Attachment
16
- end
@@ -1,80 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper'))
2
-
3
- class FileSystemTest < Test::Unit::TestCase
4
- include BaseAttachmentTests
5
- attachment_model FileAttachment
6
-
7
- def test_filesystem_size_for_file_attachment(klass = FileAttachment)
8
- attachment_model klass
9
- assert_created 1 do
10
- attachment = upload_file :filename => '/files/rails.png'
11
- assert_equal attachment.size, File.open(attachment.full_filename).stat.size
12
- end
13
- end
14
-
15
- test_against_subclass :test_filesystem_size_for_file_attachment, FileAttachment
16
-
17
- def test_should_not_overwrite_file_attachment(klass = FileAttachment)
18
- attachment_model klass
19
- assert_created 2 do
20
- real = upload_file :filename => '/files/rails.png'
21
- assert_valid real
22
- assert !real.new_record?, real.errors.full_messages.join("\n")
23
- assert !real.size.zero?
24
-
25
- fake = upload_file :filename => '/files/fake/rails.png'
26
- assert_valid fake
27
- assert !fake.size.zero?
28
-
29
- assert_not_equal File.open(real.full_filename).stat.size, File.open(fake.full_filename).stat.size
30
- end
31
- end
32
-
33
- test_against_subclass :test_should_not_overwrite_file_attachment, FileAttachment
34
-
35
- def test_should_store_file_attachment_in_filesystem(klass = FileAttachment)
36
- attachment_model klass
37
- attachment = nil
38
- assert_created do
39
- attachment = upload_file :filename => '/files/rails.png'
40
- assert_valid attachment
41
- assert File.exists?(attachment.full_filename), "#{attachment.full_filename} does not exist"
42
- end
43
- attachment
44
- end
45
-
46
- test_against_subclass :test_should_store_file_attachment_in_filesystem, FileAttachment
47
-
48
- def test_should_delete_old_file_when_updating(klass = FileAttachment)
49
- attachment_model klass
50
- attachment = upload_file :filename => '/files/rails.png'
51
- old_filename = attachment.full_filename
52
- assert_not_created do
53
- use_temp_file 'files/rails.png' do |file|
54
- attachment.filename = 'rails2.png'
55
- attachment.temp_path = File.join(fixture_path, file)
56
- attachment.save!
57
- assert File.exists?(attachment.full_filename), "#{attachment.full_filename} does not exist"
58
- assert !File.exists?(old_filename), "#{old_filename} still exists"
59
- end
60
- end
61
- end
62
-
63
- test_against_subclass :test_should_delete_old_file_when_updating, FileAttachment
64
-
65
- def test_should_delete_old_file_when_renaming(klass = FileAttachment)
66
- attachment_model klass
67
- attachment = upload_file :filename => '/files/rails.png'
68
- old_filename = attachment.full_filename
69
- assert_not_created do
70
- attachment.filename = 'rails2.png'
71
- attachment.save
72
- assert File.exists?(attachment.full_filename), "#{attachment.full_filename} does not exist"
73
- assert !File.exists?(old_filename), "#{old_filename} still exists"
74
- assert !attachment.reload.size.zero?
75
- assert_equal 'rails2.png', attachment.filename
76
- end
77
- end
78
-
79
- test_against_subclass :test_should_delete_old_file_when_renaming, FileAttachment
80
- end
@@ -1,119 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'test_helper'))
2
- require 'net/http'
3
-
4
- class S3Test < Test::Unit::TestCase
5
- def self.test_S3?
6
- true unless ENV["TEST_S3"] == "false"
7
- end
8
-
9
- if test_S3? && File.exist?(File.join(File.dirname(__FILE__), '../../amazon_s3.yml'))
10
- include BaseAttachmentTests
11
- attachment_model S3Attachment
12
-
13
- def test_should_create_correct_bucket_name(klass = S3Attachment)
14
- attachment_model klass
15
- attachment = upload_file :filename => '/files/rails.png'
16
- assert_equal attachment.s3_config[:bucket_name], attachment.bucket_name
17
- end
18
-
19
- test_against_subclass :test_should_create_correct_bucket_name, S3Attachment
20
-
21
- def test_should_create_default_path_prefix(klass = S3Attachment)
22
- attachment_model klass
23
- attachment = upload_file :filename => '/files/rails.png'
24
- assert_equal File.join(attachment_model.table_name, attachment.attachment_path_id), attachment.base_path
25
- end
26
-
27
- test_against_subclass :test_should_create_default_path_prefix, S3Attachment
28
-
29
- def test_should_create_custom_path_prefix(klass = S3WithPathPrefixAttachment)
30
- attachment_model klass
31
- attachment = upload_file :filename => '/files/rails.png'
32
- assert_equal File.join('some/custom/path/prefix', attachment.attachment_path_id), attachment.base_path
33
- end
34
-
35
- test_against_subclass :test_should_create_custom_path_prefix, S3WithPathPrefixAttachment
36
-
37
- def test_should_create_valid_url(klass = S3Attachment)
38
- attachment_model klass
39
- attachment = upload_file :filename => '/files/rails.png'
40
- assert_equal "#{s3_protocol}#{s3_hostname}#{s3_port_string}/#{attachment.bucket_name}/#{attachment.full_filename}", attachment.s3_url
41
- end
42
-
43
- test_against_subclass :test_should_create_valid_url, S3Attachment
44
-
45
- def test_should_create_authenticated_url(klass = S3Attachment)
46
- attachment_model klass
47
- attachment = upload_file :filename => '/files/rails.png'
48
- assert_match /^http.+AWSAccessKeyId.+Expires.+Signature.+/, attachment.authenticated_s3_url(:use_ssl => true)
49
- end
50
-
51
- test_against_subclass :test_should_create_authenticated_url, S3Attachment
52
-
53
- def test_should_create_authenticated_url_for_thumbnail(klass = S3Attachment)
54
- attachment_model klass
55
- attachment = upload_file :filename => '/files/rails.png'
56
- ['large', :large].each do |thumbnail|
57
- assert_match(
58
- /^http.+rails_large\.png.+AWSAccessKeyId.+Expires.+Signature/,
59
- attachment.authenticated_s3_url(thumbnail),
60
- "authenticated_s3_url failed with #{thumbnail.class} parameter"
61
- )
62
- end
63
- end
64
-
65
- def test_should_save_attachment(klass = S3Attachment)
66
- attachment_model klass
67
- assert_created do
68
- attachment = upload_file :filename => '/files/rails.png'
69
- assert_valid attachment
70
- assert attachment.image?
71
- assert !attachment.size.zero?
72
- assert_kind_of Net::HTTPOK, http_response_for(attachment.s3_url)
73
- end
74
- end
75
-
76
- test_against_subclass :test_should_save_attachment, S3Attachment
77
-
78
- def test_should_delete_attachment_from_s3_when_attachment_record_destroyed(klass = S3Attachment)
79
- attachment_model klass
80
- attachment = upload_file :filename => '/files/rails.png'
81
-
82
- urls = [attachment.s3_url] + attachment.thumbnails.collect(&:s3_url)
83
-
84
- urls.each {|url| assert_kind_of Net::HTTPOK, http_response_for(url) }
85
- attachment.destroy
86
- urls.each do |url|
87
- begin
88
- http_response_for(url)
89
- rescue Net::HTTPForbidden, Net::HTTPNotFound
90
- nil
91
- end
92
- end
93
- end
94
-
95
- test_against_subclass :test_should_delete_attachment_from_s3_when_attachment_record_destroyed, S3Attachment
96
-
97
- protected
98
- def http_response_for(url)
99
- url = URI.parse(url)
100
- Net::HTTP.start(url.host, url.port) {|http| http.request_head(url.path) }
101
- end
102
-
103
- def s3_protocol
104
- Technoweenie::AttachmentFu::Backends::S3Backend.protocol
105
- end
106
-
107
- def s3_hostname
108
- Technoweenie::AttachmentFu::Backends::S3Backend.hostname
109
- end
110
-
111
- def s3_port_string
112
- Technoweenie::AttachmentFu::Backends::S3Backend.port_string
113
- end
114
- else
115
- def test_flunk_s3
116
- puts "s3 config file not loaded, tests not running"
117
- end
118
- end
119
- end