carrierwave-rails3 0.4.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +527 -0
- data/lib/carrierwave.rb +103 -0
- data/lib/carrierwave/compatibility/paperclip.rb +95 -0
- data/lib/carrierwave/core_ext/file.rb +11 -0
- data/lib/carrierwave/mount.rb +359 -0
- data/lib/carrierwave/orm/activerecord.rb +75 -0
- data/lib/carrierwave/orm/datamapper.rb +27 -0
- data/lib/carrierwave/orm/mongoid.rb +23 -0
- data/lib/carrierwave/orm/mongomapper.rb +27 -0
- data/lib/carrierwave/orm/sequel.rb +45 -0
- data/lib/carrierwave/processing/image_science.rb +116 -0
- data/lib/carrierwave/processing/mini_magick.rb +261 -0
- data/lib/carrierwave/processing/rmagick.rb +278 -0
- data/lib/carrierwave/sanitized_file.rb +273 -0
- data/lib/carrierwave/storage/abstract.rb +30 -0
- data/lib/carrierwave/storage/cloud_files.rb +169 -0
- data/lib/carrierwave/storage/file.rb +48 -0
- data/lib/carrierwave/storage/grid_fs.rb +104 -0
- data/lib/carrierwave/storage/right_s3.rb +3 -0
- data/lib/carrierwave/storage/s3.rb +206 -0
- data/lib/carrierwave/test/matchers.rb +164 -0
- data/lib/carrierwave/uploader.rb +44 -0
- data/lib/carrierwave/uploader/cache.rb +146 -0
- data/lib/carrierwave/uploader/callbacks.rb +41 -0
- data/lib/carrierwave/uploader/configuration.rb +134 -0
- data/lib/carrierwave/uploader/default_url.rb +19 -0
- data/lib/carrierwave/uploader/download.rb +60 -0
- data/lib/carrierwave/uploader/extension_whitelist.rb +38 -0
- data/lib/carrierwave/uploader/mountable.rb +39 -0
- data/lib/carrierwave/uploader/processing.rb +84 -0
- data/lib/carrierwave/uploader/proxy.rb +62 -0
- data/lib/carrierwave/uploader/remove.rb +23 -0
- data/lib/carrierwave/uploader/store.rb +90 -0
- data/lib/carrierwave/uploader/url.rb +33 -0
- data/lib/carrierwave/uploader/versions.rb +147 -0
- data/lib/generators/templates/uploader.rb +47 -0
- data/lib/generators/uploader_generator.rb +13 -0
- data/spec/compatibility/paperclip_spec.rb +52 -0
- data/spec/mount_spec.rb +538 -0
- data/spec/orm/activerecord_spec.rb +271 -0
- data/spec/orm/datamapper_spec.rb +168 -0
- data/spec/orm/mongoid_spec.rb +202 -0
- data/spec/orm/mongomapper_spec.rb +202 -0
- data/spec/orm/sequel_spec.rb +183 -0
- data/spec/processing/image_science_spec.rb +56 -0
- data/spec/processing/mini_magick_spec.rb +76 -0
- data/spec/processing/rmagick_spec.rb +75 -0
- data/spec/sanitized_file_spec.rb +623 -0
- data/spec/spec_helper.rb +92 -0
- data/spec/storage/cloudfiles_spec.rb +78 -0
- data/spec/storage/grid_fs_spec.rb +86 -0
- data/spec/storage/s3_spec.rb +118 -0
- data/spec/uploader/cache_spec.rb +209 -0
- data/spec/uploader/callback_spec.rb +24 -0
- data/spec/uploader/configuration_spec.rb +105 -0
- data/spec/uploader/default_url_spec.rb +85 -0
- data/spec/uploader/download_spec.rb +75 -0
- data/spec/uploader/extension_whitelist_spec.rb +44 -0
- data/spec/uploader/mountable_spec.rb +33 -0
- data/spec/uploader/paths_spec.rb +22 -0
- data/spec/uploader/processing_spec.rb +73 -0
- data/spec/uploader/proxy_spec.rb +54 -0
- data/spec/uploader/remove_spec.rb +70 -0
- data/spec/uploader/store_spec.rb +264 -0
- data/spec/uploader/url_spec.rb +102 -0
- data/spec/uploader/versions_spec.rb +298 -0
- metadata +128 -0
data/spec/spec_helper.rb
ADDED
@@ -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,86 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
4
|
+
require 'mongo'
|
5
|
+
|
6
|
+
describe CarrierWave::Storage::GridFS do
|
7
|
+
|
8
|
+
before do
|
9
|
+
@database = Mongo::Connection.new('localhost', 27017).db('carrierwave_test')
|
10
|
+
@uploader = mock('an uploader')
|
11
|
+
@uploader.stub!(:grid_fs_database).and_return("carrierwave_test")
|
12
|
+
@uploader.stub!(:grid_fs_host).and_return("localhost")
|
13
|
+
@uploader.stub!(:grid_fs_port).and_return(27017)
|
14
|
+
@uploader.stub!(:grid_fs_access_url).and_return(nil)
|
15
|
+
@uploader.stub!(:grid_fs_username).and_return(nil)
|
16
|
+
@uploader.stub!(:grid_fs_password).and_return(nil)
|
17
|
+
|
18
|
+
@grid = Mongo::GridFileSystem.new(@database)
|
19
|
+
|
20
|
+
@storage = CarrierWave::Storage::GridFS.new(@uploader)
|
21
|
+
@file = stub_tempfile('test.jpg', 'application/xml')
|
22
|
+
end
|
23
|
+
|
24
|
+
after do
|
25
|
+
@grid.delete('uploads/bar.txt')
|
26
|
+
end
|
27
|
+
|
28
|
+
describe '#store!' do
|
29
|
+
before do
|
30
|
+
@uploader.stub!(:store_path).and_return('uploads/bar.txt')
|
31
|
+
@grid_fs_file = @storage.store!(@file)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should upload the file to gridfs" do
|
35
|
+
@grid.open('uploads/bar.txt', 'r').data.should == 'this is stuff'
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should not have a path" do
|
39
|
+
@grid_fs_file.path.should be_nil
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should not have a URL" do
|
43
|
+
@grid_fs_file.url.should be_nil
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should be deletable" do
|
47
|
+
@grid_fs_file.delete
|
48
|
+
lambda {@grid.open('uploads/bar.txt', 'r')}.should raise_error(Mongo::GridFileNotFound)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should store the content type on GridFS" do
|
52
|
+
@grid_fs_file.content_type.should == 'application/xml'
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe '#retrieve!' do
|
57
|
+
before do
|
58
|
+
@grid.open('uploads/bar.txt', 'w') { |f| f.write "A test, 1234" }
|
59
|
+
@uploader.stub!(:store_path).with('bar.txt').and_return('uploads/bar.txt')
|
60
|
+
@grid_fs_file = @storage.retrieve!('bar.txt')
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should retrieve the file contents from gridfs" do
|
64
|
+
@grid_fs_file.read.chomp.should == "A test, 1234"
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should not have a path" do
|
68
|
+
@grid_fs_file.path.should be_nil
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should not have a URL unless set" do
|
72
|
+
@grid_fs_file.url.should be_nil
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should return a URL if configured" do
|
76
|
+
@uploader.stub!(:grid_fs_access_url).and_return("/image/show")
|
77
|
+
@grid_fs_file.url.should == "/image/show/uploads/bar.txt"
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should be deletable" do
|
81
|
+
@grid_fs_file.delete
|
82
|
+
lambda {@grid.open('uploads/bar.txt', 'r')}.should raise_error(Mongo::GridFileNotFound)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
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
|