s3direct 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a0da6ea008f558ec274a57def5b4c2d262c23470
4
+ data.tar.gz: 75afb9a551087d2a8c2a8bc5d351f374d59d5532
5
+ SHA512:
6
+ metadata.gz: 6c878f375eff37ed2156f114c9fa8f5f415e8cb1be32fa42e9a8640b82137d8acda8765ff00d3497dd993b76fef9f6982e5c1c7d2cd02cbe0689bc61d3470a63
7
+ data.tar.gz: bb5ac94ee6e6219d1aa26e15aa8f95a8b16d2c1ad0870066fba611fc833401a04170153d330d518c1dfb265088212d8e3652745a18c42f2db029ad815691b825
data/lib/s3direct/file.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module S3Direct
2
2
  class File
3
3
 
4
- attr_reader :model, :identifier, :pattern
4
+ attr_reader :model, :identifier, :pattern, :options
5
5
 
6
6
  def self.sanitize_filename(name)
7
7
  unless name.nil?
@@ -10,11 +10,11 @@ module S3Direct
10
10
  end
11
11
 
12
12
  def initialize(model, identifier, pattern, opts={})
13
- setup_options opts
14
-
15
13
  @model = model
16
14
  @identifier = identifier
17
15
  @pattern = pattern
16
+
17
+ @options = default_options.merge(opts)
18
18
  end
19
19
 
20
20
  def name
@@ -33,12 +33,12 @@ module S3Direct
33
33
  end
34
34
  end
35
35
 
36
- def upload_request(filename = name, options = {})
36
+ def upload_request(filename = name, opts = {})
37
37
  if filename.blank?
38
38
  raise "Can't create an upload request without a filename - " +
39
39
  "provide it as an argument or set #{identifier}_file on the model"
40
40
  end
41
- UploadRequest.new s3_path, self.class.sanitize_filename(filename), options
41
+ UploadRequest.new s3_path, self.class.sanitize_filename(filename), options.merge(opts)
42
42
  end
43
43
 
44
44
  def key
@@ -49,19 +49,24 @@ module S3Direct
49
49
  name.present?
50
50
  end
51
51
 
52
+ def max_upload_size
53
+ max_method = "#{identifier}_max_upload_size"
54
+
55
+ if model.respond_to?(max_method)
56
+ model.public_send(max_method)
57
+ end
58
+ end
59
+
52
60
  private
53
61
 
54
62
  def config
55
63
  ::S3Direct.config
56
64
  end
57
65
 
58
- def options
59
- # set up any defaults here
60
- @options ||= {}
61
- end
62
-
63
- def setup_options(opts)
64
- options.merge! opts
66
+ def default_options
67
+ Hash.new.tap do |h|
68
+ h[:max_upload_size] = max_upload_size if max_upload_size
69
+ end
65
70
  end
66
71
 
67
72
  def default_url
@@ -39,6 +39,10 @@ module S3Direct
39
39
  options.fetch(:acl, config.default_acl)
40
40
  end
41
41
 
42
+ def max_upload_size
43
+ options.fetch(:max_upload_size, config.max_upload_size)
44
+ end
45
+
42
46
  private
43
47
 
44
48
  # generate the policy document that amazon is expecting.
@@ -50,7 +54,7 @@ module S3Direct
50
54
  {'acl' => s3_acl},
51
55
  {'success_action_status' => '200'},
52
56
  {'key' => key},
53
- ['content-length-range', 0, config.max_upload_size]
57
+ ['content-length-range', 0, max_upload_size]
54
58
  ]
55
59
  }
56
60
 
@@ -1,3 +1,3 @@
1
1
  module S3Direct
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/spec/file_spec.rb CHANGED
@@ -26,6 +26,18 @@ describe S3Direct::File do
26
26
  expect(file.url).to_not eql "http://example.com/default.png"
27
27
  end
28
28
 
29
+ it "returns a nil max_upload_size if the model does not provide it" do
30
+ model = double(:model, avatar_file: nil)
31
+ file = S3Direct::File.new(model, :avatar, "some/path")
32
+ expect(file.max_upload_size).to be_nil
33
+ end
34
+
35
+ it "returns a the max_upload_size from the model if it responds to a method like avatar_max_upload_size" do
36
+ model = double(:model, avatar_file: nil, avatar_max_upload_size: 123)
37
+ file = S3Direct::File.new(model, :avatar, "some/path")
38
+ expect(file.max_upload_size).to eq(123)
39
+ end
40
+
29
41
  end
30
42
 
31
43
  describe S3Direct::File, "#exists?" do
@@ -66,6 +78,13 @@ describe S3Direct::File, "#upload_request" do
66
78
  file.upload_request('test.txt', {foo: 'bar'})
67
79
  end
68
80
 
81
+ it "receives the max_upload_size option if the model provides it" do
82
+ model = double(:model, media_max_upload_size: 2048)
83
+ file = S3Direct::File.new(model, :media, "foo/bar/bat")
84
+ S3Direct::UploadRequest.should_receive(:new).with("foo/bar/bat", "test.txt", {max_upload_size: 2048, foo: 'bar'})
85
+ file.upload_request('test.txt', {foo: 'bar'})
86
+ end
87
+
69
88
  it "raises an error if the filename is not set or provided" do
70
89
  model = double(:model)
71
90
  file = S3Direct::File.new(model, :media, "foo/bar/bat")
@@ -14,6 +14,28 @@ describe S3Direct::UploadRequest, '#attachment_filename' do
14
14
  end
15
15
  end
16
16
 
17
+ describe S3Direct::UploadRequest, 'the s3 upload policy' do
18
+ before do
19
+ S3Direct.config.stub(bucket_url: 'http://s3.com/mabucket/')
20
+ S3Direct.config.stub(secret_key: 'sekret')
21
+ end
22
+
23
+ def content_length_condition(upload_request)
24
+ policy = JSON[Base64.decode64(JSON[upload_request.to_json]['policy'])]
25
+ policy['conditions'].detect {|c| c.is_a?(Array) && c[0] == 'content-length-range'}
26
+ end
27
+
28
+ it 'defaults the max upload size to the config' do
29
+ upload_request = S3Direct::UploadRequest.new('/foo/bar', 'buzz.txt')
30
+ expect(content_length_condition(upload_request)).to eq(['content-length-range', 0, 1073741824])
31
+ end
32
+
33
+ it 'uses the max_upload_size instance option if provided' do
34
+ upload_request = S3Direct::UploadRequest.new('/foo/bar', 'buzz.txt', max_upload_size: 1024)
35
+ expect(content_length_condition(upload_request)).to eq(['content-length-range', 0, 1024])
36
+ end
37
+ end
38
+
17
39
  describe S3Direct::UploadRequest, '#to_json' do
18
40
  before do
19
41
  S3Direct.config.stub(bucket_url: 'http://s3.com/mabucket/')
metadata CHANGED
@@ -1,142 +1,125 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: s3direct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
5
- prerelease:
4
+ version: 0.2.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Brent Dillingham
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2014-03-20 00:00:00.000000000 Z
11
+ date: 2014-06-03 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: activesupport
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: 3.2.0
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - ">="
28
25
  - !ruby/object:Gem::Version
29
26
  version: 3.2.0
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: jquery-fileupload-rails
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ~>
31
+ - - "~>"
36
32
  - !ruby/object:Gem::Version
37
33
  version: 0.4.1
38
34
  type: :runtime
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ~>
38
+ - - "~>"
44
39
  - !ruby/object:Gem::Version
45
40
  version: 0.4.1
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: coffee-rails
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - ">="
52
46
  - !ruby/object:Gem::Version
53
47
  version: '0'
54
48
  type: :runtime
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - ">="
60
53
  - !ruby/object:Gem::Version
61
54
  version: '0'
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: ejs
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
- - - ! '>='
59
+ - - ">="
68
60
  - !ruby/object:Gem::Version
69
61
  version: '0'
70
62
  type: :runtime
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
- - - ! '>='
66
+ - - ">="
76
67
  - !ruby/object:Gem::Version
77
68
  version: '0'
78
69
  - !ruby/object:Gem::Dependency
79
70
  name: bundler
80
71
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
72
  requirements:
83
- - - ~>
73
+ - - "~>"
84
74
  - !ruby/object:Gem::Version
85
75
  version: '1.3'
86
76
  type: :development
87
77
  prerelease: false
88
78
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
79
  requirements:
91
- - - ~>
80
+ - - "~>"
92
81
  - !ruby/object:Gem::Version
93
82
  version: '1.3'
94
83
  - !ruby/object:Gem::Dependency
95
84
  name: rails
96
85
  requirement: !ruby/object:Gem::Requirement
97
- none: false
98
86
  requirements:
99
- - - ! '>='
87
+ - - ">="
100
88
  - !ruby/object:Gem::Version
101
89
  version: 3.2.1
102
90
  type: :development
103
91
  prerelease: false
104
92
  version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
93
  requirements:
107
- - - ! '>='
94
+ - - ">="
108
95
  - !ruby/object:Gem::Version
109
96
  version: 3.2.1
110
97
  - !ruby/object:Gem::Dependency
111
98
  name: rake
112
99
  requirement: !ruby/object:Gem::Requirement
113
- none: false
114
100
  requirements:
115
- - - ! '>='
101
+ - - ">="
116
102
  - !ruby/object:Gem::Version
117
103
  version: '0'
118
104
  type: :development
119
105
  prerelease: false
120
106
  version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
107
  requirements:
123
- - - ! '>='
108
+ - - ">="
124
109
  - !ruby/object:Gem::Version
125
110
  version: '0'
126
111
  - !ruby/object:Gem::Dependency
127
112
  name: rspec
128
113
  requirement: !ruby/object:Gem::Requirement
129
- none: false
130
114
  requirements:
131
- - - ! '>='
115
+ - - ">="
132
116
  - !ruby/object:Gem::Version
133
117
  version: '0'
134
118
  type: :development
135
119
  prerelease: false
136
120
  version_requirements: !ruby/object:Gem::Requirement
137
- none: false
138
121
  requirements:
139
- - - ! '>='
122
+ - - ">="
140
123
  - !ruby/object:Gem::Version
141
124
  version: '0'
142
125
  description: Upload directly to S3
@@ -146,7 +129,7 @@ executables: []
146
129
  extensions: []
147
130
  extra_rdoc_files: []
148
131
  files:
149
- - .gitignore
132
+ - ".gitignore"
150
133
  - Gemfile
151
134
  - LICENSE.txt
152
135
  - README.md
@@ -169,33 +152,26 @@ files:
169
152
  homepage: ''
170
153
  licenses:
171
154
  - MIT
155
+ metadata: {}
172
156
  post_install_message:
173
157
  rdoc_options: []
174
158
  require_paths:
175
159
  - lib
176
160
  required_ruby_version: !ruby/object:Gem::Requirement
177
- none: false
178
161
  requirements:
179
- - - ! '>='
162
+ - - ">="
180
163
  - !ruby/object:Gem::Version
181
164
  version: '0'
182
- segments:
183
- - 0
184
- hash: 1023143937309546202
185
165
  required_rubygems_version: !ruby/object:Gem::Requirement
186
- none: false
187
166
  requirements:
188
- - - ! '>='
167
+ - - ">="
189
168
  - !ruby/object:Gem::Version
190
169
  version: '0'
191
- segments:
192
- - 0
193
- hash: 1023143937309546202
194
170
  requirements: []
195
171
  rubyforge_project:
196
- rubygems_version: 1.8.23
172
+ rubygems_version: 2.2.2
197
173
  signing_key:
198
- specification_version: 3
174
+ specification_version: 4
199
175
  summary: Upload directly to S3
200
176
  test_files:
201
177
  - spec/config_spec.rb