coryodaniel-milton 0.3.7
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.markdown +350 -0
- data/Rakefile +9 -0
- data/init.rb +1 -0
- data/lib/milton.rb +111 -0
- data/lib/milton/attachment.rb +202 -0
- data/lib/milton/core/file.rb +22 -0
- data/lib/milton/core/tempfile.rb +44 -0
- data/lib/milton/derivatives/derivative.rb +106 -0
- data/lib/milton/derivatives/thumbnail.rb +39 -0
- data/lib/milton/derivatives/thumbnail/crop_calculator.rb +64 -0
- data/lib/milton/derivatives/thumbnail/image.rb +49 -0
- data/lib/milton/storage/disk_file.rb +84 -0
- data/lib/milton/storage/s3_file.rb +103 -0
- data/lib/milton/storage/stored_file.rb +46 -0
- data/lib/milton/uploading.rb +82 -0
- data/test/fixtures/big-milton.jpg +0 -0
- data/test/fixtures/milton.jpg +0 -0
- data/test/fixtures/mini-milton.jpg +0 -0
- data/test/fixtures/unsanitary .milton.jpg +0 -0
- data/test/milton/attachment_test.rb +359 -0
- data/test/milton/milton_test.rb +21 -0
- data/test/milton/resizing_test.rb +70 -0
- data/test/s3_helper.rb +59 -0
- data/test/schema.rb +13 -0
- data/test/test_helper.rb +78 -0
- metadata +87 -0
@@ -0,0 +1,21 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class MiltonTest < ActiveSupport::TestCase
|
4
|
+
context "running a syscall" do
|
5
|
+
should "return false on failure" do
|
6
|
+
assert !Milton.syscall('ls this_directory_definitely_doesnt_exist')
|
7
|
+
end
|
8
|
+
|
9
|
+
should "return output on success" do
|
10
|
+
assert_equal "foo\n", Milton.syscall("echo foo")
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context "running a syscall!" do
|
15
|
+
should "raise a Milton::SyscallFailedError if it fails" do
|
16
|
+
assert_raise Milton::SyscallFailedError do
|
17
|
+
Milton.syscall!('ls this_directory_definitely_doesnt_exist')
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class ResizingTest < ActiveSupport::TestCase
|
4
|
+
context "processing thumbnails on create" do
|
5
|
+
context "with recipes" do
|
6
|
+
class ImageWithRecipes < Image
|
7
|
+
is_attachment(
|
8
|
+
:storage_options => { :root => ActiveSupport::TestCase.output_path },
|
9
|
+
:recipes => {
|
10
|
+
:foo => [{ :thumbnail => { :size => '50x50', :crop => true } }],
|
11
|
+
:bar => [{ :thumbnail => { :size => '10x10' } }],
|
12
|
+
:baz => [{ :thumbnail => { :size => '50x50' } }, { :thumbnail => { :size => '25x25', :crop => true } }],
|
13
|
+
}
|
14
|
+
)
|
15
|
+
end
|
16
|
+
|
17
|
+
setup do
|
18
|
+
@image = ImageWithRecipes.create! :file => upload('milton.jpg')
|
19
|
+
end
|
20
|
+
|
21
|
+
should "recognize options from :foo recipe" do
|
22
|
+
assert_equal 'milton.crop_size=50x50.jpg', File.basename(@image.path(:foo))
|
23
|
+
end
|
24
|
+
|
25
|
+
should "create :foo thumbnail" do
|
26
|
+
assert File.exists?(@image.path(:foo))
|
27
|
+
end
|
28
|
+
|
29
|
+
should "create :bar thumbnail" do
|
30
|
+
assert File.exists?(@image.path(:bar))
|
31
|
+
end
|
32
|
+
|
33
|
+
should "run :baz recipes in order" do
|
34
|
+
assert_equal 'milton.size=50x50.crop_size=25x25.jpg', File.basename(@image.path(:baz))
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context "without sizes" do
|
39
|
+
setup do
|
40
|
+
@image = Image.create! :file => upload('milton.jpg')
|
41
|
+
end
|
42
|
+
|
43
|
+
should "not create a 50x50 thumbnail" do
|
44
|
+
assert !File.exists?(@image.path(:thumbnail => { :size => '50x50' }))
|
45
|
+
end
|
46
|
+
|
47
|
+
should "happily return path to non-existant 50x50 thumbnail" do
|
48
|
+
assert_equal 'milton.size=50x50.jpg', File.basename(@image.path(:thumbnail => { :size => '50x50' }))
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context "with postprocessing" do
|
53
|
+
class ImageWithPostprocessing < Image
|
54
|
+
is_attachment :storage_options => { :root => ActiveSupport::TestCase.output_path }, :postprocessing => true
|
55
|
+
end
|
56
|
+
|
57
|
+
setup do
|
58
|
+
@image = ImageWithPostprocessing.create! :file => upload('milton.jpg')
|
59
|
+
end
|
60
|
+
|
61
|
+
should "create a 50x50 thumbnail" do
|
62
|
+
assert File.exists?(@image.path(:thumbnail => { :size => '50x50' }))
|
63
|
+
end
|
64
|
+
|
65
|
+
should "create a cropped 50x50 thumbnail" do
|
66
|
+
assert File.exists?(@image.path(:thumbnail => { :size => '50x50', :crop => true }))
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
data/test/s3_helper.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'right_aws'
|
2
|
+
|
3
|
+
# this fakes S3 and makes it write to the file-system so we can check results
|
4
|
+
module RightAws
|
5
|
+
raise "Must define S3_ROOT with fake S3 root before requiring s3_helper" unless defined?(S3_ROOT)
|
6
|
+
|
7
|
+
class S3
|
8
|
+
def buckets
|
9
|
+
Dir.glob(S3_ROOT + '/*').collect do |bucket|
|
10
|
+
Bucket.new(self, File.basename(bucket), Time.now, Owner.new(1, 'owner'))
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class Key
|
15
|
+
def put(data=nil, perms=nil, headers={})
|
16
|
+
Rails.logger.info "Putting to fake S3 store: #{filename}"
|
17
|
+
FileUtils.mkdir_p(File.join(S3_ROOT, @bucket.name, File.dirname(@name)))
|
18
|
+
FileUtils.cp(data.path, filename)
|
19
|
+
end
|
20
|
+
|
21
|
+
def delete
|
22
|
+
Rails.logger.info "Deleting from fake S3 store: #{filename}"
|
23
|
+
FileUtils.rm(filename)
|
24
|
+
end
|
25
|
+
|
26
|
+
def exists?
|
27
|
+
File.exists?(filename)
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def filename
|
33
|
+
File.join(S3_ROOT, @bucket.name, @name)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class Bucket
|
38
|
+
def key(key_name, head=false)
|
39
|
+
Key.new(self, key_name, nil, {}, {}, Time.now, Time.now.to_s, 100, '', Owner.new(1, 'owner'))
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
class Grantee
|
44
|
+
def apply
|
45
|
+
true
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
class S3Interface < RightAwsBase
|
51
|
+
def get(bucket, key, headers={}, &block)
|
52
|
+
File.open(File.join(S3_ROOT, bucket, key), 'rb').each{ |io| block.call(io) }
|
53
|
+
end
|
54
|
+
|
55
|
+
def create_bucket(bucket, headers={})
|
56
|
+
FileUtils.mkdir_p(File.join(S3_ROOT, bucket))
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
data/test/schema.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
ActiveRecord::Schema.define :version => 0 do
|
2
|
+
create_table :attachments, :force => true do |t|
|
3
|
+
t.string :filename
|
4
|
+
end
|
5
|
+
|
6
|
+
create_table :images, :force => true do |t|
|
7
|
+
t.string :filename
|
8
|
+
t.string :content_type
|
9
|
+
end
|
10
|
+
|
11
|
+
create_table :not_uploadables, :force => true do |t|
|
12
|
+
end
|
13
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../../../../test/test_helper'
|
2
|
+
puts "\nWARNING: The tests require a Rails environment, try running them from within a Rails application.\n\n" unless defined?(Rails)
|
3
|
+
puts "\nWARNING: The tests require Contest or Shoulda gem loaded in your Rails environment\n\n" unless ActiveSupport::TestCase.respond_to?(:context)
|
4
|
+
unless %w( ActiveRecord::ConnectionAdapters::PostgreSQLAdapter ActiveRecord::ConnectionAdapters::MysqlAdapter ).include?(ActiveRecord::Base.connection.class.to_s)
|
5
|
+
puts "\nWARNING: The tests will fail using any adapter other than PostgreSQL or MySQL because they depend on a proper transaction rollback that retains the current sequence, Milton itself will still work however.\n\n"
|
6
|
+
end
|
7
|
+
|
8
|
+
require 'flexmock/test_unit'
|
9
|
+
require 'redgreen' rescue LoadError
|
10
|
+
|
11
|
+
Rails.backtrace_cleaner.remove_silencers!
|
12
|
+
Rails.backtrace_cleaner.add_silencer do |line|
|
13
|
+
(%w( /opt /var lib/active_support lib/active_record vendor/gems vendor/rails )).any? { |dir| line.include?(dir) }
|
14
|
+
end
|
15
|
+
|
16
|
+
$: << File.expand_path(File.dirname(__FILE__) + '/..')
|
17
|
+
|
18
|
+
load(File.dirname(__FILE__) + '/schema.rb')
|
19
|
+
|
20
|
+
class ActiveSupport::TestCase
|
21
|
+
self.use_transactional_fixtures = true
|
22
|
+
self.use_instantiated_fixtures = false
|
23
|
+
ActiveSupport::TestCase.fixture_path = File.join(File.dirname(__FILE__), 'fixtures/')
|
24
|
+
|
25
|
+
@@output_path = File.expand_path(File.join(File.dirname(__FILE__), 'output'))
|
26
|
+
cattr_reader :output_path
|
27
|
+
def output_path;ActiveSupport::TestCase.output_path;end;
|
28
|
+
|
29
|
+
# remove files created from previous test run, happens before instead of
|
30
|
+
# after so you can view them after you run the tests
|
31
|
+
FileUtils.rm_rf(output_path)
|
32
|
+
|
33
|
+
def upload(file, type='image/jpg')
|
34
|
+
ActionController::TestUploadedFile.new(fixture_file(file), type)
|
35
|
+
end
|
36
|
+
|
37
|
+
def fixture_file(file)
|
38
|
+
ActionController::TestCase.fixture_path + file
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
module ActiveSupport::Testing::Declarative
|
43
|
+
def pending_test(name, &block)
|
44
|
+
test(name) do
|
45
|
+
puts "\nPENDING: #{name} (in #{eval('"#{__FILE__}:#{__LINE__}"', block.binding)})"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
class Attachment < ActiveRecord::Base
|
51
|
+
is_attachment :storage_options => { :root => ActiveSupport::TestCase.output_path }
|
52
|
+
end
|
53
|
+
|
54
|
+
class Image < ActiveRecord::Base
|
55
|
+
is_attachment :storage_options => { :root => ActiveSupport::TestCase.output_path }
|
56
|
+
end
|
57
|
+
|
58
|
+
class Net::HTTP
|
59
|
+
def connect
|
60
|
+
raise "Trying to hit the interwebz!!!"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
S3_ROOT = File.join(ActiveSupport::TestCase.output_path, 's3')
|
65
|
+
require File.join(File.dirname(__FILE__), 's3_helper')
|
66
|
+
|
67
|
+
class S3File
|
68
|
+
class << self
|
69
|
+
def path(url)
|
70
|
+
url.scan /http:\/\/(.*)\.s3.amazonaws.com\/(.*)\/(.*)/
|
71
|
+
File.join(S3_ROOT, $1, $2, $3)
|
72
|
+
end
|
73
|
+
|
74
|
+
def exists?(url)
|
75
|
+
File.exist?(path(url))
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: coryodaniel-milton
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 3
|
8
|
+
- 7
|
9
|
+
version: 0.3.7
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Ben Alavi
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-04-30 00:00:00 -07:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Rails file and upload handling plugin built for extensibility. Supports Amazon S3 and resizes images.
|
22
|
+
email: ben.alavi@citrusbyte.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- README.markdown
|
31
|
+
- MIT-LICENSE
|
32
|
+
- Rakefile
|
33
|
+
- init.rb
|
34
|
+
- lib/milton/attachment.rb
|
35
|
+
- lib/milton/core/file.rb
|
36
|
+
- lib/milton/core/tempfile.rb
|
37
|
+
- lib/milton/derivatives/derivative.rb
|
38
|
+
- lib/milton/derivatives/thumbnail/crop_calculator.rb
|
39
|
+
- lib/milton/derivatives/thumbnail/image.rb
|
40
|
+
- lib/milton/derivatives/thumbnail.rb
|
41
|
+
- lib/milton/storage/disk_file.rb
|
42
|
+
- lib/milton/storage/s3_file.rb
|
43
|
+
- lib/milton/storage/stored_file.rb
|
44
|
+
- lib/milton/uploading.rb
|
45
|
+
- lib/milton.rb
|
46
|
+
- test/fixtures/big-milton.jpg
|
47
|
+
- test/fixtures/milton.jpg
|
48
|
+
- test/fixtures/mini-milton.jpg
|
49
|
+
- test/fixtures/unsanitary .milton.jpg
|
50
|
+
- test/milton/attachment_test.rb
|
51
|
+
- test/milton/milton_test.rb
|
52
|
+
- test/milton/resizing_test.rb
|
53
|
+
- test/s3_helper.rb
|
54
|
+
- test/schema.rb
|
55
|
+
- test/test_helper.rb
|
56
|
+
has_rdoc: true
|
57
|
+
homepage: http://labs.citrusbyte.com/projects/milton
|
58
|
+
licenses: []
|
59
|
+
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
segments:
|
70
|
+
- 0
|
71
|
+
version: "0"
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
segments:
|
77
|
+
- 0
|
78
|
+
version: "0"
|
79
|
+
requirements: []
|
80
|
+
|
81
|
+
rubyforge_project: coryodaniel-milton
|
82
|
+
rubygems_version: 1.3.6
|
83
|
+
signing_key:
|
84
|
+
specification_version: 3
|
85
|
+
summary: Rails file and upload handling plugin built for extensibility. Supports Amazon S3 and resizes images.
|
86
|
+
test_files: []
|
87
|
+
|