samlown-carrierwave 0.4.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (111) hide show
  1. data/Generators +4 -0
  2. data/History.txt +125 -0
  3. data/Manifest.txt +110 -0
  4. data/README.rdoc +524 -0
  5. data/Rakefile +39 -0
  6. data/carrierwave.gemspec +85 -0
  7. data/cucumber.yml +2 -0
  8. data/features/caching.feature +28 -0
  9. data/features/download.feature +20 -0
  10. data/features/file_storage.feature +37 -0
  11. data/features/file_storage_overridden_filename.feature +38 -0
  12. data/features/file_storage_overridden_store_dir.feature +38 -0
  13. data/features/file_storage_reversing_processor.feature +43 -0
  14. data/features/fixtures/bork.txt +1 -0
  15. data/features/fixtures/monkey.txt +1 -0
  16. data/features/grid_fs_storage.feature +32 -0
  17. data/features/mount_activerecord.feature +46 -0
  18. data/features/mount_datamapper.feature +46 -0
  19. data/features/step_definitions/activerecord_steps.rb +22 -0
  20. data/features/step_definitions/caching_steps.rb +14 -0
  21. data/features/step_definitions/datamapper_steps.rb +29 -0
  22. data/features/step_definitions/download_steps.rb +4 -0
  23. data/features/step_definitions/file_steps.rb +53 -0
  24. data/features/step_definitions/general_steps.rb +85 -0
  25. data/features/step_definitions/mount_steps.rb +19 -0
  26. data/features/step_definitions/store_steps.rb +18 -0
  27. data/features/support/activerecord.rb +30 -0
  28. data/features/support/datamapper.rb +7 -0
  29. data/features/support/env.rb +22 -0
  30. data/features/versions_basics.feature +50 -0
  31. data/features/versions_nested_versions.feature +70 -0
  32. data/features/versions_overridden_filename.feature +51 -0
  33. data/features/versions_overriden_store_dir.feature +41 -0
  34. data/lib/carrierwave.rb +98 -0
  35. data/lib/carrierwave/compatibility/paperclip.rb +95 -0
  36. data/lib/carrierwave/core_ext/blank.rb +46 -0
  37. data/lib/carrierwave/core_ext/file.rb +11 -0
  38. data/lib/carrierwave/core_ext/inheritable_attributes.rb +108 -0
  39. data/lib/carrierwave/core_ext/module_setup.rb +51 -0
  40. data/lib/carrierwave/mount.rb +359 -0
  41. data/lib/carrierwave/orm/activerecord.rb +73 -0
  42. data/lib/carrierwave/orm/datamapper.rb +27 -0
  43. data/lib/carrierwave/orm/mongoid.rb +23 -0
  44. data/lib/carrierwave/orm/mongomapper.rb +27 -0
  45. data/lib/carrierwave/orm/sequel.rb +45 -0
  46. data/lib/carrierwave/processing/image_science.rb +101 -0
  47. data/lib/carrierwave/processing/mini_magick.rb +265 -0
  48. data/lib/carrierwave/processing/rmagick.rb +282 -0
  49. data/lib/carrierwave/sanitized_file.rb +273 -0
  50. data/lib/carrierwave/storage/abstract.rb +30 -0
  51. data/lib/carrierwave/storage/cloud_files.rb +169 -0
  52. data/lib/carrierwave/storage/file.rb +48 -0
  53. data/lib/carrierwave/storage/grid_fs.rb +97 -0
  54. data/lib/carrierwave/storage/right_s3.rb +3 -0
  55. data/lib/carrierwave/storage/s3.rb +206 -0
  56. data/lib/carrierwave/test/matchers.rb +128 -0
  57. data/lib/carrierwave/uploader.rb +44 -0
  58. data/lib/carrierwave/uploader/cache.rb +145 -0
  59. data/lib/carrierwave/uploader/callbacks.rb +42 -0
  60. data/lib/carrierwave/uploader/configuration.rb +132 -0
  61. data/lib/carrierwave/uploader/default_url.rb +19 -0
  62. data/lib/carrierwave/uploader/download.rb +59 -0
  63. data/lib/carrierwave/uploader/extension_whitelist.rb +37 -0
  64. data/lib/carrierwave/uploader/mountable.rb +39 -0
  65. data/lib/carrierwave/uploader/processing.rb +83 -0
  66. data/lib/carrierwave/uploader/proxy.rb +62 -0
  67. data/lib/carrierwave/uploader/remove.rb +22 -0
  68. data/lib/carrierwave/uploader/store.rb +89 -0
  69. data/lib/carrierwave/uploader/url.rb +33 -0
  70. data/lib/carrierwave/uploader/versions.rb +146 -0
  71. data/merb_generators/uploader_generator.rb +22 -0
  72. data/rails_generators/uploader/USAGE +2 -0
  73. data/rails_generators/uploader/templates/uploader.rb +47 -0
  74. data/rails_generators/uploader/uploader_generator.rb +21 -0
  75. data/script/console +10 -0
  76. data/script/destroy +14 -0
  77. data/script/generate +14 -0
  78. data/spec/compatibility/paperclip_spec.rb +52 -0
  79. data/spec/fixtures/bork.txt +1 -0
  80. data/spec/fixtures/landscape.jpg +0 -0
  81. data/spec/fixtures/portrait.jpg +0 -0
  82. data/spec/fixtures/test.jpeg +1 -0
  83. data/spec/fixtures/test.jpg +1 -0
  84. data/spec/mount_spec.rb +538 -0
  85. data/spec/orm/activerecord_spec.rb +271 -0
  86. data/spec/orm/datamapper_spec.rb +168 -0
  87. data/spec/orm/mongoid_spec.rb +202 -0
  88. data/spec/orm/mongomapper_spec.rb +202 -0
  89. data/spec/orm/sequel_spec.rb +183 -0
  90. data/spec/processing/image_science_spec.rb +56 -0
  91. data/spec/processing/mini_magick_spec.rb +76 -0
  92. data/spec/processing/rmagick_spec.rb +75 -0
  93. data/spec/sanitized_file_spec.rb +623 -0
  94. data/spec/spec_helper.rb +92 -0
  95. data/spec/storage/cloudfiles_spec.rb +78 -0
  96. data/spec/storage/grid_fs_spec.rb +83 -0
  97. data/spec/storage/s3_spec.rb +118 -0
  98. data/spec/uploader/cache_spec.rb +209 -0
  99. data/spec/uploader/configuration_spec.rb +105 -0
  100. data/spec/uploader/default_url_spec.rb +85 -0
  101. data/spec/uploader/download_spec.rb +75 -0
  102. data/spec/uploader/extension_whitelist_spec.rb +44 -0
  103. data/spec/uploader/mountable_spec.rb +33 -0
  104. data/spec/uploader/paths_spec.rb +22 -0
  105. data/spec/uploader/processing_spec.rb +73 -0
  106. data/spec/uploader/proxy_spec.rb +54 -0
  107. data/spec/uploader/remove_spec.rb +70 -0
  108. data/spec/uploader/store_spec.rb +264 -0
  109. data/spec/uploader/url_spec.rb +102 -0
  110. data/spec/uploader/versions_spec.rb +298 -0
  111. metadata +433 -0
@@ -0,0 +1,92 @@
1
+ # encoding: utf-8
2
+
3
+ $TESTING=true
4
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
5
+
6
+ require 'rubygems'
7
+
8
+ require 'tempfile'
9
+ #require 'ruby-debug'
10
+ require 'spec'
11
+ require 'spec/autorun'
12
+
13
+ require 'carrierwave'
14
+ require 'timecop'
15
+ require 'time'
16
+ require 'json'
17
+
18
+ require 'logger'
19
+
20
+ alias :running :lambda
21
+
22
+ def file_path( *paths )
23
+ File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', *paths))
24
+ end
25
+
26
+ def public_path( *paths )
27
+ File.expand_path(File.join(File.dirname(__FILE__), 'public', *paths))
28
+ end
29
+
30
+ CarrierWave.root = public_path
31
+
32
+ module CarrierWave
33
+ module Test
34
+ module MockStorage
35
+ def mock_storage(kind)
36
+ storage = mock("storage for #{kind} uploader")
37
+ storage.stub!(:setup!)
38
+ storage
39
+ end
40
+ end
41
+
42
+ module MockFiles
43
+ def stub_merb_tempfile(filename)
44
+ raise "#{path} file does not exist" unless File.exist?(file_path(filename))
45
+
46
+ t = Tempfile.new(filename)
47
+ FileUtils.copy_file(file_path(filename), t.path)
48
+
49
+ return t
50
+ end
51
+
52
+ def stub_tempfile(filename, mime_type=nil, fake_name=nil)
53
+ raise "#{path} file does not exist" unless File.exist?(file_path(filename))
54
+
55
+ t = Tempfile.new(filename)
56
+ FileUtils.copy_file(file_path(filename), t.path)
57
+
58
+ # This is stupid, but for some reason rspec won't play nice...
59
+ eval <<-EOF
60
+ def t.original_filename; '#{fake_name || filename}'; end
61
+ def t.content_type; '#{mime_type}'; end
62
+ def t.local_path; path; end
63
+ EOF
64
+
65
+ return t
66
+ end
67
+
68
+ def stub_stringio(filename, mime_type=nil, fake_name=nil)
69
+ if filename
70
+ t = StringIO.new( IO.read( file_path( filename ) ) )
71
+ else
72
+ t = StringIO.new
73
+ end
74
+ t.stub!(:local_path).and_return("")
75
+ t.stub!(:original_filename).and_return(filename || fake_name)
76
+ t.stub!(:content_type).and_return(mime_type)
77
+ return t
78
+ end
79
+
80
+ def stub_file(filename, mime_type=nil, fake_name=nil)
81
+ f = File.open(file_path(filename))
82
+ return f
83
+ end
84
+ end
85
+ end
86
+ end
87
+
88
+ Spec::Runner.configure do |config|
89
+ config.include CarrierWave::Test::Matchers
90
+ config.include CarrierWave::Test::MockFiles
91
+ config.include CarrierWave::Test::MockStorage
92
+ end
@@ -0,0 +1,78 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../spec_helper'
4
+
5
+ if ENV['CLOUDFILES_SPEC']
6
+ require 'cloudfiles'
7
+ require 'net/http'
8
+
9
+ describe CarrierWave::Storage::CloudFiles do
10
+ before do
11
+ @uploader = mock('an uploader')
12
+ @uploader.stub!(:cloud_files_username).and_return(ENV["CLOUD_FILES_USER_NAME"])
13
+ @uploader.stub!(:cloud_files_api_key).and_return(ENV["CLOUD_FILES_API_KEY"])
14
+ @uploader.stub!(:cloud_files_container).and_return(ENV['CARRIERWAVE_TEST_CONTAINER'])
15
+ @storage = CarrierWave::Storage::CloudFiles.new(@uploader)
16
+ @file = stub_tempfile('test.jpg', 'application/xml')
17
+
18
+ @cf = CloudFiles::Connection.new(ENV["CLOUD_FILES_USER_NAME"], ENV["CLOUD_FILES_API_KEY"])
19
+ @container = @cf.container(@uploader.cloud_files_container)
20
+ end
21
+
22
+ describe '#store!' do
23
+ before do
24
+ @uploader.stub!(:store_path).and_return('uploads/bar.txt')
25
+ @cloud_file = @storage.store!(@file)
26
+ end
27
+
28
+ it "should upload the file to Cloud Files" do
29
+ @container.object('uploads/bar.txt').data.should == 'this is stuff'
30
+ end
31
+
32
+ it "should have a path" do
33
+ @cloud_file.path.should == 'uploads/bar.txt'
34
+ end
35
+
36
+ it "should have an Rackspace URL" do
37
+ @cloud_file.url.should =~ %r!http://(.*?).cdn.cloudfiles.rackspacecloud.com/uploads/bar.txt!
38
+ end
39
+
40
+ it "should store the content type on Cloud Files" do
41
+ @cloud_file.content_type.should == 'application/xml'
42
+ end
43
+
44
+ it "should be deletable" do
45
+ @cloud_file.delete
46
+ @container.object_exists?('uploads/bar.txt').should be_false
47
+ end
48
+
49
+ end
50
+
51
+ describe '#retrieve!' do
52
+ before do
53
+ @container.create_object('uploads/bar.txt').write("A test, 1234")
54
+ @uploader.stub!(:store_path).with('bar.txt').and_return('uploads/bar.txt')
55
+
56
+ @cloud_file = @storage.retrieve!('bar.txt')
57
+ end
58
+
59
+ it "should retrieve the file contents from Cloud Files" do
60
+ @cloud_file.read.chomp.should == "A test, 1234"
61
+ end
62
+
63
+ it "should have a path" do
64
+ @cloud_file.path.should == 'uploads/bar.txt'
65
+ end
66
+
67
+ it "should have an Rackspace URL" do
68
+ @cloud_file.url.should =~ %r!http://(.*?).cdn.cloudfiles.rackspacecloud.com/uploads/bar.txt!
69
+ end
70
+
71
+ it "should be deletable" do
72
+ @cloud_file.delete
73
+ @container.object_exists?('uploads/bar.txt').should be_false
74
+ end
75
+ end
76
+
77
+ end
78
+ end
@@ -0,0 +1,83 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../spec_helper'
4
+
5
+ describe CarrierWave::Storage::GridFS do
6
+
7
+ before do
8
+ @database = Mongo::Connection.new('localhost', 27017).db('carrierwave_test')
9
+ @uploader = mock('an uploader')
10
+ @uploader.stub!(:grid_fs_database).and_return("carrierwave_test")
11
+ @uploader.stub!(:grid_fs_host).and_return("localhost")
12
+ @uploader.stub!(:grid_fs_port).and_return(27017)
13
+ @uploader.stub!(:grid_fs_access_url).and_return(nil)
14
+ @uploader.stub!(:grid_fs_username).and_return(nil)
15
+ @uploader.stub!(:grid_fs_password).and_return(nil)
16
+
17
+ @storage = CarrierWave::Storage::GridFS.new(@uploader)
18
+ @file = stub_tempfile('test.jpg', 'application/xml')
19
+ end
20
+
21
+ after do
22
+ GridFS::GridStore.unlink(@database, 'uploads/bar.txt')
23
+ end
24
+
25
+ describe '#store!' do
26
+ before do
27
+ @uploader.stub!(:store_path).and_return('uploads/bar.txt')
28
+ @grid_fs_file = @storage.store!(@file)
29
+ end
30
+
31
+ it "should upload the file to gridfs" do
32
+ GridFS::GridStore.read(@database, 'uploads/bar.txt').should == 'this is stuff'
33
+ end
34
+
35
+ it "should not have a path" do
36
+ @grid_fs_file.path.should be_nil
37
+ end
38
+
39
+ it "should not have a URL" do
40
+ @grid_fs_file.url.should be_nil
41
+ end
42
+
43
+ it "should be deletable" do
44
+ @grid_fs_file.delete
45
+ GridFS::GridStore.read(@database, 'uploads/bar.txt').should == ''
46
+ end
47
+
48
+ it "should store the content type on GridFS" do
49
+ @grid_fs_file.content_type.should == 'application/xml'
50
+ end
51
+ end
52
+
53
+ describe '#retrieve!' do
54
+ before do
55
+ GridFS::GridStore.open(@database, 'uploads/bar.txt', 'w') { |f| f.puts "A test, 1234" }
56
+ @uploader.stub!(:store_path).with('bar.txt').and_return('uploads/bar.txt')
57
+ @grid_fs_file = @storage.retrieve!('bar.txt')
58
+ end
59
+
60
+ it "should retrieve the file contents from gridfs" do
61
+ @grid_fs_file.read.chomp.should == "A test, 1234"
62
+ end
63
+
64
+ it "should not have a path" do
65
+ @grid_fs_file.path.should be_nil
66
+ end
67
+
68
+ it "should not have a URL unless set" do
69
+ @grid_fs_file.url.should be_nil
70
+ end
71
+
72
+ it "should return a URL if configured" do
73
+ @uploader.stub!(:grid_fs_access_url).and_return("/image/show")
74
+ @grid_fs_file.url.should == "/image/show/uploads/bar.txt"
75
+ end
76
+
77
+ it "should be deletable" do
78
+ @grid_fs_file.delete
79
+ GridFS::GridStore.read(@database, 'uploads/bar.txt').should == ''
80
+ end
81
+ end
82
+
83
+ end
@@ -0,0 +1,118 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../spec_helper'
4
+ require 'aws'
5
+
6
+ if ENV['S3_SPEC']
7
+ describe CarrierWave::Storage::S3 do
8
+ before do
9
+ @bucket = ENV['CARRIERWAVE_TEST_BUCKET']
10
+ @uploader = mock('an uploader')
11
+ @uploader.stub!(:s3_access_key_id).and_return(ENV["S3_ACCESS_KEY_ID"])
12
+ @uploader.stub!(:s3_secret_access_key).and_return(ENV["S3_SECRET_ACCESS_KEY"])
13
+ @uploader.stub!(:s3_bucket).and_return(@bucket)
14
+ @uploader.stub!(:s3_access_policy).and_return('public-read')
15
+ @uploader.stub!(:s3_cnamed).and_return(false)
16
+ @uploader.stub!(:s3_multi_thread).and_return(true)
17
+ @uploader.stub!(:s3_headers).and_return({'Expires' => 'Fri, 21 Jan 2021 16:51:06 GMT'})
18
+
19
+ @storage = CarrierWave::Storage::S3.new(@uploader)
20
+ @file = CarrierWave::SanitizedFile.new(file_path('test.jpg'))
21
+ end
22
+
23
+ after do
24
+ @storage.connection.delete(@bucket, 'uploads/bar.txt')
25
+ end
26
+
27
+ describe 'general setup' do
28
+ before(:each) do
29
+ @uploader.stub!(:s3_access_policy).and_return(nil)
30
+ @uploader.stub!(:s3_access).and_return(nil)
31
+ @s3_file = CarrierWave::Storage::S3::File.new(@uploader, @storage, 'uploads/bar.txt')
32
+ end
33
+
34
+ it "should use the default public-read access policy" do
35
+ @s3_file.access_policy.should eql('public-read')
36
+ end
37
+
38
+ it "should use provided access policy" do
39
+ @uploader.stub!(:s3_access_policy).and_return('bucket-owner-read')
40
+ @s3_file.access_policy.should eql('bucket-owner-read')
41
+ end
42
+
43
+ it "should use old s3_access config if s3_access_policy not set" do
44
+ @uploader.stub!(:s3_access).and_return(:public_read_write)
45
+ @s3_file.access_policy.should eql('public-read-write')
46
+ end
47
+ end
48
+
49
+ describe '#store!' do
50
+ before do
51
+ @uploader.stub!(:store_path).and_return('uploads/bar.txt')
52
+ @s3_file = @storage.store!(@file)
53
+ end
54
+
55
+ it "should upload the file to s3" do
56
+ @storage.connection.get_object(@bucket, 'uploads/bar.txt').should == 'this is stuff'
57
+ end
58
+
59
+ it "should have a path" do
60
+ @s3_file.path.should == 'uploads/bar.txt'
61
+ end
62
+
63
+ context "without cnamed bucket" do
64
+ it "should have a Euro supported Amazon URL" do
65
+ @uploader.stub!(:s3_cnamed).and_return(false)
66
+ @uploader.stub!(:s3_bucket).and_return('foo.bar')
67
+ @s3_file.url.should == "http://foo.bar.s3.amazonaws.com/uploads/bar.txt"
68
+ end
69
+ end
70
+
71
+ context "with cnamed bucket" do
72
+ it "should have a CNAMED URL" do
73
+ @uploader.stub!(:s3_cnamed).and_return(true)
74
+ @uploader.stub!(:s3_bucket).and_return('foo.bar')
75
+ @s3_file.url.should == 'http://foo.bar/uploads/bar.txt'
76
+ end
77
+ end
78
+
79
+ it "should be deletable" do
80
+ @s3_file.delete
81
+ lambda {@storage.connection.head(@bucket, 'uploads/bar.txt')}.should raise_error(Aws::AwsError)
82
+ end
83
+
84
+ it "should set headers" do
85
+ client = Net::HTTP.new("#{@bucket}.s3.amazonaws.com")
86
+ headers = client.request_head('/uploads/bar.txt')
87
+ headers["Expires"].should == 'Fri, 21 Jan 2021 16:51:06 GMT'
88
+ end
89
+
90
+ end
91
+
92
+ describe '#retrieve!' do
93
+ before do
94
+ @storage.connection.put(@bucket, "uploads/bar.txt", "A test, 1234", {'a-amz-acl' => 'public-read'})
95
+ @uploader.stub!(:store_path).with('bar.txt').and_return('uploads/bar.txt')
96
+ @s3_file = @storage.retrieve!('bar.txt')
97
+ end
98
+
99
+ it "should retrieve the file contents from s3" do
100
+ @s3_file.read.chomp.should == "A test, 1234"
101
+ end
102
+
103
+ it "should have a path" do
104
+ @s3_file.path.should == 'uploads/bar.txt'
105
+ end
106
+
107
+ it "should have an Amazon URL" do
108
+ @s3_file.url.should == "http://#{@bucket}.s3.amazonaws.com/uploads/bar.txt"
109
+ end
110
+
111
+ it "should be deletable" do
112
+ @s3_file.delete
113
+ lambda {@storage.connection.head(@bucket, 'uploads/bar.txt')}.should raise_error(Aws::AwsError)
114
+ end
115
+ end
116
+
117
+ end
118
+ end
@@ -0,0 +1,209 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../spec_helper'
4
+
5
+ describe CarrierWave::Uploader do
6
+
7
+ before do
8
+ @uploader_class = Class.new(CarrierWave::Uploader::Base)
9
+ @uploader = @uploader_class.new
10
+ end
11
+
12
+ after do
13
+ FileUtils.rm_rf(public_path)
14
+ end
15
+
16
+ describe '.clean_cached_files!' do
17
+ before do
18
+ @cache_dir = File.expand_path(@uploader_class.cache_dir, CarrierWave.root)
19
+ FileUtils.mkdir_p File.expand_path('20071201-1234-234-2213', @cache_dir)
20
+ FileUtils.mkdir_p File.expand_path('20071203-1234-234-2213', @cache_dir)
21
+ FileUtils.mkdir_p File.expand_path('20071205-1234-234-2213', @cache_dir)
22
+ end
23
+
24
+ after { FileUtils.rm_rf(@cache_dir) }
25
+
26
+ it "should clear all files older than 24 hours in the default cache directory" do
27
+ Timecop.freeze(Time.parse('2007-12-06 10:12')) do
28
+ @uploader_class.clean_cached_files!
29
+ end
30
+ Dir.glob("#{@cache_dir}/*").should have(1).element
31
+ end
32
+
33
+ it "should be aliased on the CarrierWave module" do
34
+ Timecop.freeze(Time.parse('2007-12-06 10:12')) do
35
+ CarrierWave.clean_cached_files!
36
+ end
37
+ Dir.glob("#{@cache_dir}/*").should have(1).element
38
+ end
39
+ end
40
+
41
+ describe '#cache_dir' do
42
+ it "should default to the config option" do
43
+ @uploader.cache_dir.should == 'uploads/tmp'
44
+ end
45
+ end
46
+
47
+ describe '#cache!' do
48
+
49
+ before do
50
+ CarrierWave.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
51
+ end
52
+
53
+ it "should cache a file" do
54
+ @uploader.cache!(File.open(file_path('test.jpg')))
55
+ @uploader.file.should be_an_instance_of(CarrierWave::SanitizedFile)
56
+ end
57
+
58
+ it "should be cached" do
59
+ @uploader.cache!(File.open(file_path('test.jpg')))
60
+ @uploader.should be_cached
61
+ end
62
+
63
+ it "should store the cache name" do
64
+ @uploader.cache!(File.open(file_path('test.jpg')))
65
+ @uploader.cache_name.should == '20071201-1234-345-2255/test.jpg'
66
+ end
67
+
68
+ it "should set the filename to the file's sanitized filename" do
69
+ @uploader.cache!(File.open(file_path('test.jpg')))
70
+ @uploader.filename.should == 'test.jpg'
71
+ end
72
+
73
+ it "should move it to the tmp dir" do
74
+ @uploader.cache!(File.open(file_path('test.jpg')))
75
+ @uploader.file.path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpg')
76
+ @uploader.file.exists?.should be_true
77
+ end
78
+
79
+ it "should set the url" do
80
+ @uploader.cache!(File.open(file_path('test.jpg')))
81
+ @uploader.url.should == '/uploads/tmp/20071201-1234-345-2255/test.jpg'
82
+ end
83
+
84
+ it "should raise an error when trying to cache a string" do
85
+ running {
86
+ @uploader.cache!(file_path('test.jpg'))
87
+ }.should raise_error(CarrierWave::FormNotMultipart)
88
+ end
89
+
90
+ it "should raise an error when trying to cache a pathname" do
91
+ running {
92
+ @uploader.cache!(Pathname.new(file_path('test.jpg')))
93
+ }.should raise_error(CarrierWave::FormNotMultipart)
94
+ end
95
+
96
+ it "should do nothing when trying to cache an empty file" do
97
+ @uploader.cache!(nil)
98
+ end
99
+
100
+ it "should set permissions if options are given" do
101
+ @uploader_class.permissions = 0777
102
+
103
+ @uploader.cache!(File.open(file_path('test.jpg')))
104
+ @uploader.should have_permissions(0777)
105
+ end
106
+ end
107
+
108
+ describe '#retrieve_from_cache!' do
109
+ it "should cache a file" do
110
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
111
+ @uploader.file.should be_an_instance_of(CarrierWave::SanitizedFile)
112
+ end
113
+
114
+ it "should be cached" do
115
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
116
+ @uploader.should be_cached
117
+ end
118
+
119
+ it "should set the path to the tmp dir" do
120
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
121
+ @uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpeg')
122
+ end
123
+
124
+ it "should overwrite a file that has already been cached" do
125
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
126
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/bork.txt')
127
+ @uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/bork.txt')
128
+ end
129
+
130
+ it "should store the cache_name" do
131
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
132
+ @uploader.cache_name.should == '20071201-1234-345-2255/test.jpeg'
133
+ end
134
+
135
+ it "should store the filename" do
136
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
137
+ @uploader.filename.should == 'test.jpeg'
138
+ end
139
+
140
+ it "should set the url" do
141
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
142
+ @uploader.url.should == '/uploads/tmp/20071201-1234-345-2255/test.jpeg'
143
+ end
144
+
145
+ it "should raise an error when the cache_id has an invalid format" do
146
+ running {
147
+ @uploader.retrieve_from_cache!('12345/test.jpeg')
148
+ }.should raise_error(CarrierWave::InvalidParameter)
149
+
150
+ @uploader.file.should be_nil
151
+ @uploader.filename.should be_nil
152
+ @uploader.cache_name.should be_nil
153
+ end
154
+
155
+ it "should raise an error when the original_filename contains invalid characters" do
156
+ running {
157
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/te/st.jpeg')
158
+ }.should raise_error(CarrierWave::InvalidParameter)
159
+ running {
160
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/te??%st.jpeg')
161
+ }.should raise_error(CarrierWave::InvalidParameter)
162
+
163
+ @uploader.file.should be_nil
164
+ @uploader.filename.should be_nil
165
+ @uploader.cache_name.should be_nil
166
+ end
167
+ end
168
+
169
+ describe 'with an overridden, reversing, filename' do
170
+ before do
171
+ @uploader_class.class_eval do
172
+ def filename
173
+ super.reverse unless super.blank?
174
+ end
175
+ end
176
+ end
177
+
178
+ describe '#cache!' do
179
+
180
+ before do
181
+ CarrierWave.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
182
+ end
183
+
184
+ it "should set the filename to the file's reversed filename" do
185
+ @uploader.cache!(File.open(file_path('test.jpg')))
186
+ @uploader.filename.should == "gpj.tset"
187
+ end
188
+
189
+ it "should move it to the tmp dir with the filename unreversed" do
190
+ @uploader.cache!(File.open(file_path('test.jpg')))
191
+ @uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpg')
192
+ @uploader.file.exists?.should be_true
193
+ end
194
+ end
195
+
196
+ describe '#retrieve_from_cache!' do
197
+ it "should set the path to the tmp dir" do
198
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpg')
199
+ @uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpg')
200
+ end
201
+
202
+ it "should set the filename to the reversed name of the file" do
203
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpg')
204
+ @uploader.filename.should == "gpj.tset"
205
+ end
206
+ end
207
+ end
208
+
209
+ end