carrierwave_direct 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +17 -12
- data/lib/carrierwave_direct/locale/en.yml +3 -1
- data/lib/carrierwave_direct/uploader.rb +8 -2
- data/lib/carrierwave_direct/validations/active_model.rb +22 -12
- data/lib/carrierwave_direct/version.rb +1 -1
- data/spec/orm/activerecord_spec.rb +18 -3
- data/spec/spec_helper.rb +3 -0
- data/spec/uploader_spec.rb +24 -4
- metadata +24 -19
- data/lib/carrierwave_direct/locale/en.rb +0 -22
data/README.md
CHANGED
@@ -6,16 +6,16 @@
|
|
6
6
|
|
7
7
|
[CarrierWaveDirect](https://github.com/dwilkie/carrierwave_direct) works on top of [CarrierWave](https://github.com/jnicklas/carrierwave) and provides a simple way to achieve this.
|
8
8
|
|
9
|
+
## Example Application
|
10
|
+
|
11
|
+
For a concrete example on how to use [CarrierWaveDirect](https://github.com/dwilkie/carrierwave_direct) in a Rails application check out the [Example Application](https://github.com/dwilkie/carrierwave_direct_example).
|
12
|
+
|
9
13
|
## Compatibility
|
10
14
|
|
11
15
|
Right now, CarrierWaveDirect works with [Amazon S3](http://aws.amazon.com/s3/). Adding support for [Google Storage for Developers](http://code.google.com/apis/storage/) should be fairly straight forward since the direct upload form is essentially the same. Please see the contributing section if you would like support for Google Storage for Developers or any other service that provides direct upload capabilities.
|
12
16
|
|
13
17
|
Please be aware that this gem (and S3 in general) only support single file uploads. If you want to upload multiple files simultaneously you'll have to use a javascript or flash uploader.
|
14
18
|
|
15
|
-
## Information
|
16
|
-
|
17
|
-
More information, and how-tos [available on the wiki](https://github.com/dwilkie/carrierwave_direct/wiki)
|
18
|
-
|
19
19
|
## Installation
|
20
20
|
|
21
21
|
Install the latest release:
|
@@ -32,7 +32,7 @@ Note that CarrierWaveDirect is not compatible with Rails 2.
|
|
32
32
|
|
33
33
|
Please read the [CarrierWave readme](https://github.com/jnicklas/carrierwave) first
|
34
34
|
|
35
|
-
CarrierWaveDirect works with [fog](https://github.com/
|
35
|
+
CarrierWaveDirect works with [fog](https://github.com/fog/fog) so make sure you have [CarrierWave](https://github.com/jnicklas/carrierwave) set up and initialized with your fog credentials, for example:
|
36
36
|
|
37
37
|
CarrierWave.configure do |config|
|
38
38
|
config.fog_credentials = {
|
@@ -41,10 +41,9 @@ CarrierWaveDirect works with [fog](https://github.com/geemus/fog) so make sure y
|
|
41
41
|
:aws_secret_access_key => 'yyy', # required
|
42
42
|
:region => 'eu-west-1' # optional, defaults to 'us-east-1'
|
43
43
|
}
|
44
|
-
config.fog_directory = '
|
45
|
-
|
46
|
-
|
47
|
-
config.fog_attributes = {'Cache-Control'=>'max-age=315576000'} # optional, defaults to {}
|
44
|
+
config.fog_directory = 'name_of_your_aws_bucket' # required
|
45
|
+
# see https://github.com/jnicklas/carrierwave#using-amazon-s3
|
46
|
+
# for more optional configuration
|
48
47
|
end
|
49
48
|
|
50
49
|
If you haven't already done so generate an uploader
|
@@ -376,12 +375,15 @@ The Active Record validations use the Rails i18n framework. Add these keys to yo
|
|
376
375
|
carrierwave_direct_filename_taken: filename was already taken
|
377
376
|
carrierwave_direct_upload_missing: upload is missing
|
378
377
|
carrierwave_direct_attachment_missing: attachment is missing
|
379
|
-
carrierwave_direct_filename_invalid: is invalid.
|
380
|
-
|
378
|
+
carrierwave_direct_filename_invalid: "is invalid. "
|
379
|
+
carrierwave_direct_allowed_extensions: Allowed file types are %{extensions}
|
380
|
+
carrierwave_direct_allowed_schemes: Allowed schemes are %{schemes}
|
381
381
|
|
382
382
|
## Caveats
|
383
383
|
|
384
|
-
Don't name your string column `file`. It will result in a stack level too deep exception. See [this issue](https://github.com/dwilkie/carrierwave_direct/issues/10) for more info
|
384
|
+
Don't name your string column `file`. It will result in a stack level too deep exception. See [this issue](https://github.com/dwilkie/carrierwave_direct/issues/10) for more info.
|
385
|
+
|
386
|
+
If you're Rails app was newly generated *after* version 3.2.3 and your testing this in development you may run into an issue where an `ActiveModel::MassAssignmentSecurity::Error` is raised when being redirected from S3. You can fix this by setting `config.active_record.mass_assignment_sanitizer = :logger` in your `config/environments/development.rb` file.
|
385
387
|
|
386
388
|
## Contributing to CarrierWaveDirect
|
387
389
|
|
@@ -404,3 +406,6 @@ You should now be able to run the tests
|
|
404
406
|
* [jgorset (Johannes Gorset)](https://github.com/jgorset) - Added note about removing 'store_dir' in README
|
405
407
|
* [frahugo (Hugo Frappier)](https://github.com/frahugo) - Fix bug where CarrierWaveDirect Validations were being added to non CarrierWaveDirect ActiveRecord models
|
406
408
|
* [bak (Benjamin Cullen-Kerney)](https://github.com/bak) - Fix bug where CarrierWaveDirect specific methods were being added to non CarrierWaveDirect ActiveRecord models
|
409
|
+
* [rhalff (Rob Halff)](https://github.com/rhalff) - Doc fix
|
410
|
+
* [nicknovitski (Nick Novitski)](https://github.com/nicknovitski) - Update old link in README
|
411
|
+
* [gabrielengel (Gabriel Engel)](https://github.com/gabrielengel) - Refactor I18n not to use procs
|
@@ -4,4 +4,6 @@ en:
|
|
4
4
|
carrierwave_direct_filename_taken: filename was already taken
|
5
5
|
carrierwave_direct_upload_missing: upload is missing
|
6
6
|
carrierwave_direct_attachment_missing: attachment is missing
|
7
|
-
|
7
|
+
carrierwave_direct_filename_invalid: "is invalid. "
|
8
|
+
carrierwave_direct_allowed_extensions: Allowed file types are %{extensions}
|
9
|
+
carrierwave_direct_allowed_schemes: Allowed schemes are %{schemes}
|
@@ -40,11 +40,17 @@ module CarrierWaveDirect
|
|
40
40
|
end
|
41
41
|
|
42
42
|
def key
|
43
|
-
@key
|
43
|
+
return @key if @key.present?
|
44
|
+
if url.present?
|
45
|
+
self.key = URI.parse(url).path # explicitly set key
|
46
|
+
else
|
47
|
+
@key = "#{store_dir}/#{guid}/#{FILENAME_WILDCARD}"
|
48
|
+
end
|
49
|
+
@key
|
44
50
|
end
|
45
51
|
|
46
52
|
def has_key?
|
47
|
-
|
53
|
+
key !~ /#{Regexp.escape(FILENAME_WILDCARD)}\z/
|
48
54
|
end
|
49
55
|
|
50
56
|
def acl
|
@@ -22,11 +22,14 @@ module CarrierWaveDirect
|
|
22
22
|
class FilenameFormatValidator < ::ActiveModel::EachValidator
|
23
23
|
def validate_each(record, attribute, value)
|
24
24
|
if record.new_record? && record.send("has_#{attribute}_upload?") && record.key !~ record.send(attribute).key_regexp
|
25
|
-
record.
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
25
|
+
extensions = record.send(attribute).extension_white_list
|
26
|
+
message = I18n.t("errors.messages.carrierwave_direct_filename_invalid")
|
27
|
+
|
28
|
+
if extensions.present?
|
29
|
+
message += I18n.t("errors.messages.carrierwave_direct_allowed_extensions", :extensions => extensions.to_sentence)
|
30
|
+
end
|
31
|
+
|
32
|
+
record.errors.add(attribute, message)
|
30
33
|
end
|
31
34
|
end
|
32
35
|
end
|
@@ -37,13 +40,21 @@ module CarrierWaveDirect
|
|
37
40
|
remote_net_url = record.send("remote_#{attribute}_net_url")
|
38
41
|
uploader = record.send(attribute)
|
39
42
|
url_scheme_white_list = uploader.url_scheme_white_list
|
43
|
+
|
40
44
|
if (remote_net_url !~ URI.regexp(url_scheme_white_list) || remote_net_url !~ /#{uploader.extension_regexp}\z/)
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
45
|
+
extensions = uploader.extension_white_list
|
46
|
+
|
47
|
+
message = I18n.t("errors.messages.carrierwave_direct_filename_invalid")
|
48
|
+
|
49
|
+
if extensions.present?
|
50
|
+
message += I18n.t("errors.messages.carrierwave_direct_allowed_extensions", :extensions => extensions.to_sentence)
|
51
|
+
end
|
52
|
+
|
53
|
+
if url_scheme_white_list.present?
|
54
|
+
message += I18n.t("errors.messages.carrierwave_direct_allowed_schemes", :schemes => url_scheme_white_list.to_sentence)
|
55
|
+
end
|
56
|
+
|
57
|
+
record.errors.add(:"remote_#{attribute}_net_url", message)
|
47
58
|
end
|
48
59
|
end
|
49
60
|
end
|
@@ -123,4 +134,3 @@ module CarrierWaveDirect
|
|
123
134
|
end
|
124
135
|
|
125
136
|
Dir[File.dirname(__FILE__) << "/../locale/*.*"].each {|file| I18n.load_path << file }
|
126
|
-
|
@@ -72,7 +72,8 @@ describe CarrierWaveDirect::ActiveRecord do
|
|
72
72
|
it "should use i18n for the error messages" do
|
73
73
|
subject.valid?
|
74
74
|
subject.errors[:video].should == [
|
75
|
-
I18n.t("errors.messages.carrierwave_direct_filename_invalid"
|
75
|
+
I18n.t("errors.messages.carrierwave_direct_filename_invalid") +
|
76
|
+
I18n.t("errors.messages.carrierwave_direct_allowed_extensions", :extensions => %w{avi mp4}.to_sentence)
|
76
77
|
]
|
77
78
|
end
|
78
79
|
end
|
@@ -80,7 +81,22 @@ describe CarrierWaveDirect::ActiveRecord do
|
|
80
81
|
shared_examples_for "a remote net url i18n error message" do
|
81
82
|
it "should use i18n for the error messages" do
|
82
83
|
subject.valid?
|
83
|
-
|
84
|
+
|
85
|
+
messages = I18n.t("errors.messages.carrierwave_direct_filename_invalid")
|
86
|
+
|
87
|
+
if i18n_options
|
88
|
+
if i18n_options[:extension_white_list]
|
89
|
+
extensions = i18n_options[:extension_white_list].to_sentence
|
90
|
+
messages += I18n.t("errors.messages.carrierwave_direct_allowed_extensions", :extensions => extensions)
|
91
|
+
end
|
92
|
+
|
93
|
+
if i18n_options[:url_scheme_white_list]
|
94
|
+
schemes = i18n_options[:url_scheme_white_list].to_sentence
|
95
|
+
messages += I18n.t("errors.messages.carrierwave_direct_allowed_schemes", :schemes => schemes)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
subject.errors[:remote_video_net_url].should == [ messages ]
|
84
100
|
end
|
85
101
|
end
|
86
102
|
|
@@ -548,4 +564,3 @@ describe CarrierWaveDirect::ActiveRecord do
|
|
548
564
|
end
|
549
565
|
end
|
550
566
|
end
|
551
|
-
|
data/spec/spec_helper.rb
CHANGED
data/spec/uploader_spec.rb
CHANGED
@@ -21,7 +21,8 @@ describe CarrierWaveDirect::Uploader do
|
|
21
21
|
:mounted_as => :video,
|
22
22
|
:filename => "filename",
|
23
23
|
:extension => ".avi",
|
24
|
-
:version => :thumb
|
24
|
+
:version => :thumb,
|
25
|
+
:s3_bucket_url => "https://s3-bucket.s3.amazonaws.com"
|
25
26
|
}
|
26
27
|
|
27
28
|
SAMPLE_DATA.merge!(
|
@@ -37,6 +38,10 @@ describe CarrierWaveDirect::Uploader do
|
|
37
38
|
:s3_key => "#{sample(:store_dir)}/#{sample(:stored_filename)}"
|
38
39
|
)
|
39
40
|
|
41
|
+
SAMPLE_DATA.merge!(
|
42
|
+
:s3_file_url => "#{sample(:s3_bucket_url)}/#{sample(:s3_key)}"
|
43
|
+
)
|
44
|
+
|
40
45
|
SAMPLE_DATA.freeze
|
41
46
|
|
42
47
|
let(:subject) { DirectUploader.new }
|
@@ -105,6 +110,21 @@ describe CarrierWaveDirect::Uploader do
|
|
105
110
|
mounted_subject.key.should =~ /^#{sample(:store_dir)}\/#{GUID_REGEXP}\/\$\{filename\}$/
|
106
111
|
end
|
107
112
|
end
|
113
|
+
|
114
|
+
context "but the uploaders url is '#{sample(:s3_file_url)}'" do
|
115
|
+
before do
|
116
|
+
mounted_subject.stub(:url).and_return(sample(:s3_file_url))
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should return '/#{sample(:s3_key)}'" do
|
120
|
+
mounted_subject.key.should == "/#{sample(:s3_key)}"
|
121
|
+
end
|
122
|
+
|
123
|
+
it "should set the key explicitly in order to update the version keys" do
|
124
|
+
mounted_subject.should_receive("key=").with("/#{sample(:s3_key)}")
|
125
|
+
mounted_subject.key
|
126
|
+
end
|
127
|
+
end
|
108
128
|
end
|
109
129
|
|
110
130
|
context "where the key is set to '#{sample(:key)}'" do
|
@@ -284,20 +304,20 @@ describe CarrierWaveDirect::Uploader do
|
|
284
304
|
mounted_subject.filename
|
285
305
|
end
|
286
306
|
end
|
287
|
-
|
307
|
+
|
288
308
|
context "and the model's remote #{sample(:mounted_as)} url has whitespace in it" do
|
289
309
|
before do
|
290
310
|
mounted_model.stub(
|
291
311
|
"remote_#{mounted_subject.mounted_as}_url"
|
292
312
|
).and_return("http://anyurl.com/any_path/video_dir/filename 2.avi")
|
293
313
|
end
|
294
|
-
|
314
|
+
|
295
315
|
it "should be sanitized (whitespace replaced with _)" do
|
296
316
|
mounted_subject.filename
|
297
317
|
mounted_subject.key.should =~ /filename_2.avi$/
|
298
318
|
end
|
299
319
|
end
|
300
|
-
|
320
|
+
|
301
321
|
context "and the model's remote #{sample(:mounted_as)} url is blank" do
|
302
322
|
before do
|
303
323
|
mounted_model.stub(
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: carrierwave_direct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-11-19 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: carrierwave
|
16
|
-
requirement: &
|
16
|
+
requirement: &80672100 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *80672100
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: uuid
|
27
|
-
requirement: &
|
27
|
+
requirement: &80671730 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *80671730
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: fog
|
38
|
-
requirement: &
|
38
|
+
requirement: &80671380 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *80671380
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rspec
|
49
|
-
requirement: &
|
49
|
+
requirement: &80671060 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *80671060
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: timecop
|
60
|
-
requirement: &
|
60
|
+
requirement: &80670780 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *80670780
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rails
|
71
|
-
requirement: &
|
71
|
+
requirement: &80670500 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ! '>='
|
@@ -76,10 +76,10 @@ dependencies:
|
|
76
76
|
version: '0'
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *80670500
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: sqlite3
|
82
|
-
requirement: &
|
82
|
+
requirement: &80670220 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
85
85
|
- - ! '>='
|
@@ -87,10 +87,10 @@ dependencies:
|
|
87
87
|
version: '0'
|
88
88
|
type: :development
|
89
89
|
prerelease: false
|
90
|
-
version_requirements: *
|
90
|
+
version_requirements: *80670220
|
91
91
|
- !ruby/object:Gem::Dependency
|
92
92
|
name: capybara
|
93
|
-
requirement: &
|
93
|
+
requirement: &80669910 !ruby/object:Gem::Requirement
|
94
94
|
none: false
|
95
95
|
requirements:
|
96
96
|
- - ! '>='
|
@@ -98,7 +98,7 @@ dependencies:
|
|
98
98
|
version: '0'
|
99
99
|
type: :development
|
100
100
|
prerelease: false
|
101
|
-
version_requirements: *
|
101
|
+
version_requirements: *80669910
|
102
102
|
description: Process your uploads in the background by uploading directly to S3
|
103
103
|
email:
|
104
104
|
- dwilkie@gmail.com
|
@@ -116,7 +116,6 @@ files:
|
|
116
116
|
- lib/carrierwave_direct.rb
|
117
117
|
- lib/carrierwave_direct/action_view_extensions/form_helper.rb
|
118
118
|
- lib/carrierwave_direct/form_builder.rb
|
119
|
-
- lib/carrierwave_direct/locale/en.rb
|
120
119
|
- lib/carrierwave_direct/locale/en.yml
|
121
120
|
- lib/carrierwave_direct/mount.rb
|
122
121
|
- lib/carrierwave_direct/orm/activerecord.rb
|
@@ -155,12 +154,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
155
154
|
- - ! '>='
|
156
155
|
- !ruby/object:Gem::Version
|
157
156
|
version: '0'
|
157
|
+
segments:
|
158
|
+
- 0
|
159
|
+
hash: 763991155
|
158
160
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
159
161
|
none: false
|
160
162
|
requirements:
|
161
163
|
- - ! '>='
|
162
164
|
- !ruby/object:Gem::Version
|
163
165
|
version: '0'
|
166
|
+
segments:
|
167
|
+
- 0
|
168
|
+
hash: 763991155
|
164
169
|
requirements: []
|
165
170
|
rubyforge_project: carrierwave_direct
|
166
171
|
rubygems_version: 1.8.10
|
@@ -1,22 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
{
|
4
|
-
:en => {
|
5
|
-
:errors => {
|
6
|
-
:messages => {
|
7
|
-
:carrierwave_direct_filename_invalid => lambda {|key, options|
|
8
|
-
message = "is invalid"
|
9
|
-
message << ". Allowed file types are #{options[:extension_white_list].to_sentence}" if options[:extension_white_list] && options[:extension_white_list].any?
|
10
|
-
message
|
11
|
-
},
|
12
|
-
:carrierwave_direct_remote_net_url_invalid => lambda {|key, options|
|
13
|
-
message = "is invalid"
|
14
|
-
message << ". Allowed file types are #{options[:extension_white_list].to_sentence}" if options[:extension_white_list] && options[:extension_white_list].any?
|
15
|
-
message << ". Allowed url schemes are #{options[:url_scheme_white_list].to_sentence}" if options[:url_scheme_white_list] && options[:url_scheme_white_list].any?
|
16
|
-
message
|
17
|
-
}
|
18
|
-
}
|
19
|
-
}
|
20
|
-
}
|
21
|
-
}
|
22
|
-
|