s3direct 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ee3f1ab2d359fbf373c9237b0a621d848985e8a1
4
- data.tar.gz: 2722aeaaf920b376816dc101e8bbae44ef2bec98
3
+ metadata.gz: a5725f0ef72faabfef6926494d8b15ac263e06d9
4
+ data.tar.gz: c4940ed2f276792757bb2e6dddd876688e93a2d2
5
5
  SHA512:
6
- metadata.gz: 82f1a13b662cd065e56cb127ff8f5d660c0ff178d088b63951f15b0aba71347abe7d7e114cdaa8edc7638647cd1b9c920f75d3e9d28e0c4353041385252bc4b6
7
- data.tar.gz: c3393197d1fb0beb1bd6367d602a7175df01462bbb55a1584d29187c6cc2273ea2c480b5e84856238665d575a93eb9c31687fc41c99af885864f718e5a6a7b33
6
+ metadata.gz: dba5b1d48a966673d65462e8043e798702995b70573196d55fd8d1c7a86fe5e3be5b98bcb5a8a8754e0be057d9f3415facf63715d8bef28750f8c7d97c545b68
7
+ data.tar.gz: 62a3a1afd7065b95c5a0d3aff9596b118e4a0223eab6fc314b62f87f525b6f1f0bc1af056cd5d02ef37dcfa1367341341b9c3049923eb3c17631eb1d17355b15
@@ -0,0 +1,6 @@
1
+ ### 0.4.0
2
+
3
+ * enhancements
4
+ * Previously the Content-Type was not set on s3, it was defaulting to 'binary/octet-stream'.
5
+ The mime-types gem now provides content type detection based of the filename and, for maximum
6
+ accuracy, an optional `filetype` param from the browser file object.
@@ -1,6 +1,7 @@
1
1
  require "active_support/all"
2
2
 
3
3
  require "s3direct/version"
4
+ require "s3direct/content_type_detection"
4
5
  require "s3direct/file"
5
6
  require "s3direct/string_interpolator"
6
7
  require "s3direct/upload_request"
@@ -0,0 +1,52 @@
1
+ require "mime-types"
2
+
3
+ module S3Direct
4
+ class ContentTypeDetection < Struct.new(:filename, :filetype)
5
+ class Default
6
+ def self.content_type
7
+ 'binary/octet-stream'.dup
8
+ end
9
+ end
10
+
11
+ def lookup
12
+ value = if filetype.to_s.empty?
13
+ FilenameStrategy.new(filename).lookup
14
+ else
15
+ HybridStrategy.new(filename, filetype).lookup
16
+ end
17
+
18
+ remap(value)
19
+ end
20
+
21
+ def remap(value)
22
+ mappings = {
23
+ "application/mp4" => "video/mp4",
24
+ "audio/mp3" => "audio/mpeg"
25
+ }
26
+
27
+ mappings.fetch(value, value)
28
+ end
29
+ end
30
+
31
+ class FilenameStrategy < Struct.new(:filename)
32
+ def lookup
33
+ types = MIME::Types.type_for(filename)
34
+ (types.first || ContentTypeDetection::Default).content_type
35
+ end
36
+ end
37
+
38
+ class HybridStrategy < Struct.new(:filename, :filetype)
39
+ def lookup
40
+ types = MIME::Types.type_for(filename)
41
+
42
+ type = if types.length == 1
43
+ types.first
44
+ elsif types.length > 1
45
+ media_type = filetype.split('/').first
46
+ types.detect {|t| t.media_type == media_type}
47
+ end
48
+
49
+ (type || ContentTypeDetection::Default).content_type
50
+ end
51
+ end
52
+ end
@@ -29,6 +29,10 @@ module S3Direct
29
29
  data["Content-Disposition"] = %Q{attachment; filename="#{attachment_filename}"}
30
30
  end
31
31
 
32
+ if content_type
33
+ data["Content-Type"] = content_type
34
+ end
35
+
32
36
  data.to_json
33
37
  end
34
38
 
@@ -36,6 +40,14 @@ module S3Direct
36
40
  options[:attachment_filename].presence
37
41
  end
38
42
 
43
+ def filetype
44
+ options[:filetype].presence
45
+ end
46
+
47
+ def content_type
48
+ ContentTypeDetection.new(filename, filetype).lookup
49
+ end
50
+
39
51
  def s3_acl
40
52
  options.fetch(:acl, config.default_acl)
41
53
  end
@@ -63,6 +75,10 @@ module S3Direct
63
75
  policy['conditions'] << {"Content-Disposition" => %Q{attachment; filename="#{attachment_filename}"}}
64
76
  end
65
77
 
78
+ if content_type
79
+ policy['conditions'] << {"Content-Type" => content_type}
80
+ end
81
+
66
82
  encode(policy.to_json)
67
83
  end
68
84
 
@@ -1,3 +1,3 @@
1
1
  module S3Direct
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -22,6 +22,7 @@ Gem::Specification.new do |spec|
22
22
  spec.add_dependency 'jquery-fileupload-rails', '~> 0.4.1'
23
23
  spec.add_dependency 'coffee-rails'
24
24
  spec.add_dependency 'ejs'
25
+ spec.add_dependency 'mime-types'
25
26
 
26
27
  spec.add_development_dependency "bundler", "~> 1.3"
27
28
  spec.add_development_dependency "rails", ">= 3.2.1"
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe S3Direct::ContentTypeDetection do
4
+ it "will have a content_type of audio/webm for sounds.webm audio/webm" do
5
+ detection = S3Direct::ContentTypeDetection.new('sounds.webm', 'audio/webm')
6
+ expect(detection.lookup).to eq('audio/webm')
7
+ end
8
+
9
+ it "will have a content_type of video/webm for sights.webm video/webm" do
10
+ detection = S3Direct::ContentTypeDetection.new('signed.webm', 'video/webm')
11
+ expect(detection.lookup).to eq('video/webm')
12
+ end
13
+
14
+ it "will have a content_type of audio/mpeg for sights.webm and a blank filetype" do
15
+ detection = S3Direct::ContentTypeDetection.new('sing.mp3', '')
16
+ expect(detection.lookup).to eq('audio/mpeg')
17
+ end
18
+
19
+ it "will have a content_type of video/mp4 for sights.mp4 and a 'video/mp4' filetype" do
20
+ detection = S3Direct::ContentTypeDetection.new('sights.mp4', 'video/mp4')
21
+ expect(detection.lookup).to eq('video/mp4')
22
+ end
23
+
24
+ it "will have a content_type of video/mp4 for sights.mp4 and a blank filetype" do
25
+ detection = S3Direct::ContentTypeDetection.new('sights.mp4', '')
26
+ expect(detection.lookup).to eq('video/mp4')
27
+ end
28
+ end
@@ -14,6 +14,22 @@ describe S3Direct::UploadRequest, '#attachment_filename' do
14
14
  end
15
15
  end
16
16
 
17
+ describe S3Direct::UploadRequest, 'content_type' do
18
+ it "will have a content_type of image/jpeg for image/jpeg" do
19
+ upload_request = S3Direct::UploadRequest.new('/foo/bar', 'image.jpg', {
20
+ filetype: 'image/jpeg'
21
+ })
22
+ expect(upload_request.content_type).to eq('image/jpeg')
23
+ end
24
+
25
+ it "will have a content_type of audio/mpeg for audio/mp3" do
26
+ upload_request = S3Direct::UploadRequest.new('/foo/bar', 'baz.mp3', {
27
+ filetype: 'audio/mp3'
28
+ })
29
+ expect(upload_request.content_type).to eq('audio/mpeg')
30
+ end
31
+ end
32
+
17
33
  describe S3Direct::UploadRequest, 'the s3 upload policy' do
18
34
  before do
19
35
  S3Direct.config.stub(bucket_url: 'http://s3.com/mabucket/')
@@ -78,6 +94,23 @@ describe S3Direct::UploadRequest, '#to_json' do
78
94
  expect(condition['Content-Disposition']).to eq('attachment; filename="expected.txt"')
79
95
  end
80
96
  end
97
+
98
+ describe "the content_type" do
99
+ before do
100
+ upload_request = S3Direct::UploadRequest.new('/foo/bar', 'song.mp3')
101
+ @data = JSON[upload_request.to_json]
102
+ end
103
+
104
+ it "is included as Content-Type in the data" do
105
+ expect(@data["Content-Type"]).to eq('audio/mpeg')
106
+ end
107
+
108
+ it "is included n the policy" do
109
+ policy = JSON[Base64.decode64 @data['policy']]
110
+ condition = policy['conditions'].detect {|c| c.is_a?(Hash) && c.keys.include?('Content-Type') }
111
+ expect(condition['Content-Type']).to eq('audio/mpeg')
112
+ end
113
+ end
81
114
  end
82
115
 
83
116
  describe S3Direct::UploadRequest, '#s3_acl' do
metadata CHANGED
@@ -1,125 +1,139 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: s3direct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brent Dillingham
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-30 00:00:00.000000000 Z
11
+ date: 2014-08-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: 3.2.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 3.2.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: jquery-fileupload-rails
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
33
  version: 0.4.1
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ~>
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: 0.4.1
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: coffee-rails
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: ejs
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: mime-types
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
67
81
  - !ruby/object:Gem::Version
68
82
  version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: bundler
71
85
  requirement: !ruby/object:Gem::Requirement
72
86
  requirements:
73
- - - ~>
87
+ - - "~>"
74
88
  - !ruby/object:Gem::Version
75
89
  version: '1.3'
76
90
  type: :development
77
91
  prerelease: false
78
92
  version_requirements: !ruby/object:Gem::Requirement
79
93
  requirements:
80
- - - ~>
94
+ - - "~>"
81
95
  - !ruby/object:Gem::Version
82
96
  version: '1.3'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: rails
85
99
  requirement: !ruby/object:Gem::Requirement
86
100
  requirements:
87
- - - '>='
101
+ - - ">="
88
102
  - !ruby/object:Gem::Version
89
103
  version: 3.2.1
90
104
  type: :development
91
105
  prerelease: false
92
106
  version_requirements: !ruby/object:Gem::Requirement
93
107
  requirements:
94
- - - '>='
108
+ - - ">="
95
109
  - !ruby/object:Gem::Version
96
110
  version: 3.2.1
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: rake
99
113
  requirement: !ruby/object:Gem::Requirement
100
114
  requirements:
101
- - - '>='
115
+ - - ">="
102
116
  - !ruby/object:Gem::Version
103
117
  version: '0'
104
118
  type: :development
105
119
  prerelease: false
106
120
  version_requirements: !ruby/object:Gem::Requirement
107
121
  requirements:
108
- - - '>='
122
+ - - ">="
109
123
  - !ruby/object:Gem::Version
110
124
  version: '0'
111
125
  - !ruby/object:Gem::Dependency
112
126
  name: rspec
113
127
  requirement: !ruby/object:Gem::Requirement
114
128
  requirements:
115
- - - '>='
129
+ - - ">="
116
130
  - !ruby/object:Gem::Version
117
131
  version: '0'
118
132
  type: :development
119
133
  prerelease: false
120
134
  version_requirements: !ruby/object:Gem::Requirement
121
135
  requirements:
122
- - - '>='
136
+ - - ">="
123
137
  - !ruby/object:Gem::Version
124
138
  version: '0'
125
139
  description: Upload directly to S3
@@ -129,12 +143,14 @@ executables: []
129
143
  extensions: []
130
144
  extra_rdoc_files: []
131
145
  files:
132
- - .gitignore
146
+ - ".gitignore"
147
+ - CHANGELOG.md
133
148
  - Gemfile
134
149
  - LICENSE.txt
135
150
  - README.md
136
151
  - Rakefile
137
152
  - lib/s3direct.rb
153
+ - lib/s3direct/content_type_detection.rb
138
154
  - lib/s3direct/file.rb
139
155
  - lib/s3direct/string_interpolator.rb
140
156
  - lib/s3direct/upload_request.rb
@@ -142,6 +158,7 @@ files:
142
158
  - lib/s3direct/version.rb
143
159
  - s3direct.gemspec
144
160
  - spec/config_spec.rb
161
+ - spec/content_type_detection_spec.rb
145
162
  - spec/file_spec.rb
146
163
  - spec/spec_helper.rb
147
164
  - spec/string_interpolator_spec.rb
@@ -159,22 +176,23 @@ require_paths:
159
176
  - lib
160
177
  required_ruby_version: !ruby/object:Gem::Requirement
161
178
  requirements:
162
- - - '>='
179
+ - - ">="
163
180
  - !ruby/object:Gem::Version
164
181
  version: '0'
165
182
  required_rubygems_version: !ruby/object:Gem::Requirement
166
183
  requirements:
167
- - - '>='
184
+ - - ">="
168
185
  - !ruby/object:Gem::Version
169
186
  version: '0'
170
187
  requirements: []
171
188
  rubyforge_project:
172
- rubygems_version: 2.0.14
189
+ rubygems_version: 2.2.2
173
190
  signing_key:
174
191
  specification_version: 4
175
192
  summary: Upload directly to S3
176
193
  test_files:
177
194
  - spec/config_spec.rb
195
+ - spec/content_type_detection_spec.rb
178
196
  - spec/file_spec.rb
179
197
  - spec/spec_helper.rb
180
198
  - spec/string_interpolator_spec.rb