paperclip_archive_processor 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,47 @@
1
+ require 'helper'
2
+
3
+ class ExtractorTest < Test::Unit::TestCase
4
+
5
+ context '#find_extractor' do
6
+
7
+ context 'content type "application/x-zip-compressed" given' do
8
+
9
+ should 'return the zip extractor' do
10
+ extractor = PaperclipArchiveProcessor::Extractor.extractor_for("application/x-zip-compressed")
11
+ assert_same PaperclipArchiveProcessor::Extractor::Zip, extractor
12
+ end
13
+
14
+ end
15
+
16
+
17
+ context 'unknown centent type given' do
18
+
19
+ should 'raise an extractor not found error' do
20
+ assert_raise PaperclipArchiveProcessor::Extractor::ExtractorNotFound do
21
+ PaperclipArchiveProcessor::Extractor.extractor_for("application/unknown-compressed")
22
+ end
23
+ end
24
+
25
+ end
26
+
27
+ end
28
+
29
+ context '#extract' do
30
+
31
+ context 'content type "application/x-zip-compressed" given' do
32
+
33
+ setup do
34
+ @extractor = PaperclipArchiveProcessor::Extractor.extractor_for("application/x-zip-compressed")
35
+ end
36
+
37
+ should 'invoke ZipUtil#unpack' do
38
+ destination = File.join(Dir::tmpdir, 'foo')
39
+ PaperclipArchiveProcessor::ZipUtil.expects(:unpack).with(file, destination).returns([])
40
+ @extractor.extract(file, destination)
41
+ end
42
+
43
+ end
44
+
45
+ end
46
+
47
+ end
Binary file
Binary file
Binary file
Binary file
Binary file
data/test/helper.rb ADDED
@@ -0,0 +1,64 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+ require 'mocha'
13
+ require 'redgreen'
14
+ require 'active_record'
15
+ require 'active_record/version'
16
+ require 'active_support'
17
+ require 'aws/s3'
18
+
19
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
20
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
21
+ require 'paperclip_archive_processor'
22
+
23
+ ROOT = File.join(File.dirname(__FILE__), '..')
24
+
25
+ class Test::Unit::TestCase
26
+ def setup
27
+ silence_warnings do
28
+ Object.const_set(:Rails, stub('Rails', :root => ROOT, :env => 'test'))
29
+ end
30
+ create_dummy_class
31
+ end
32
+ end
33
+
34
+ config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
35
+ ActiveRecord::Base.logger = ActiveSupport::BufferedLogger.new(File.dirname(__FILE__) + "/debug.log")
36
+ ActiveRecord::Base.establish_connection(config['test'])
37
+
38
+
39
+ def reset_class(class_name)
40
+ ActiveRecord::Base.send(:include, Paperclip::Glue)
41
+ Object.send(:remove_const, class_name) rescue nil
42
+ klass = Object.const_set(class_name, Class.new(ActiveRecord::Base))
43
+ klass.class_eval{ include Paperclip::Glue }
44
+ klass
45
+ end
46
+
47
+ def rebuild_table(table_name, options = {})
48
+ ActiveRecord::Base.connection.create_table table_name, :force => true do |table|
49
+ table.column :archive_file_name, :string
50
+ table.column :archive_content_type, :string
51
+ table.column :archive_file_size, :integer
52
+ table.column :archive_updated_at, :datetime
53
+ table.column :archive_fingerprint, :string
54
+ end
55
+ end
56
+
57
+ def create_dummy_class
58
+ reset_class(:Dummy)
59
+ rebuild_table(:dummies)
60
+ end
61
+
62
+ def file(name = '5x.zip')
63
+ (@_files ||= {})[name] ||= File.new(File.join(File.dirname(__FILE__), 'fixtures', name), 'rb')
64
+ end
@@ -0,0 +1,69 @@
1
+ require 'helper'
2
+
3
+ class IntegrationTest < Test::Unit::TestCase
4
+
5
+ should 'extend paperclip class methods' do
6
+ Paperclip::Railtie.insert
7
+ assert Paperclip::ClassMethods.public_method_defined?(:has_attached_file_original)
8
+ end
9
+
10
+ context 'has configured an attachment for :archive' do
11
+
12
+ context 'extract_archive option is set to true' do
13
+
14
+ setup do
15
+ Dummy.has_attached_file :archive, :extract_archive => true
16
+ @dummy = Dummy.new
17
+ end
18
+
19
+ context 'archive file assigned' do
20
+
21
+ setup do
22
+ @dummy.archive = file
23
+ end
24
+
25
+ should 'mark archive as dirty' do
26
+ @dummy.save
27
+ assert @dummy.instance_variable_get(:@_archive_has_changed)
28
+ end
29
+
30
+ should 'call the processor extract method' do
31
+ PaperclipArchiveProcessor::Processor.expects(:extract).with(@dummy.archive)
32
+ @dummy.save
33
+ end
34
+
35
+ end
36
+
37
+ context 'archive file unassigned' do
38
+
39
+ should 'mark not archive as dirty' do
40
+ @dummy.save
41
+ assert ! @dummy.instance_variable_get(:@_archive_has_changed)
42
+ end
43
+
44
+ should 'not call the processor extract method' do
45
+ PaperclipArchiveProcessor::Processor.expects(:extract).never
46
+ @dummy.save
47
+ end
48
+
49
+ end
50
+
51
+ end
52
+
53
+ context 'extract_archive option is undefined or set to false' do
54
+
55
+ setup do
56
+ Dummy.has_attached_file :archive
57
+ @dummy = Dummy.new
58
+ @dummy.archive = file
59
+ end
60
+
61
+ should 'not call the processor extract method' do
62
+ PaperclipArchiveProcessor::Processor.expects(:extract).never
63
+ @dummy.save
64
+ end
65
+
66
+ end
67
+
68
+ end
69
+ end
@@ -0,0 +1,94 @@
1
+ require 'helper'
2
+
3
+ class ProcessorTest < Test::Unit::TestCase
4
+
5
+ context 'extract' do
6
+
7
+ context 'attachment content type "application/x-zip-compressed"' do
8
+
9
+ setup do
10
+ Dummy.has_attached_file :archive, :extract_archive => true
11
+ @dummy = Dummy.new
12
+ @dummy.archive = file
13
+ end
14
+
15
+ context 'attachment is configured to use the filesystem storage' do
16
+
17
+ should 'use the filesystem processor' do
18
+ PaperclipArchiveProcessor::Processor::Filesystem.expects(:extract).with(@dummy.archive, PaperclipArchiveProcessor::Extractor::Zip)
19
+ PaperclipArchiveProcessor::Processor.extract(@dummy.archive)
20
+ end
21
+
22
+ should 'call the extractor with local file paths' do
23
+ path = @dummy.archive.to_file.path
24
+ destination = File.dirname(path)
25
+ PaperclipArchiveProcessor::Extractor::Zip.expects(:extract).with(path, destination).returns([])
26
+ PaperclipArchiveProcessor::Processor.extract(@dummy.archive)
27
+ end
28
+
29
+ end
30
+
31
+ context 'attachment is configured to use the s3 storage' do
32
+
33
+ setup do
34
+ Dummy.has_attached_file :archive,
35
+ :storage => :s3,
36
+ :s3_credentials => { :access_key_id => 'id', :secret_access_key => 'key' },
37
+ :url => ':s3_alias_url',
38
+ :path => 'foo',
39
+ :bucket => 'test',
40
+ :extract_archive => true
41
+ @dummy = Dummy.new
42
+ @dummy.archive = file('5x.zip')
43
+ end
44
+
45
+ should 'use the s3 processor' do
46
+ PaperclipArchiveProcessor::Processor::S3.expects(:extract).with(@dummy.archive, PaperclipArchiveProcessor::Extractor::Zip)
47
+ PaperclipArchiveProcessor::Processor.extract(@dummy.archive)
48
+ end
49
+
50
+ should 'call the extractor with tempfile destination path' do
51
+ Dir.expects(:mktmpdir).returns(Dir.tmpdir).at_least_once
52
+ path = @dummy.archive.to_file.path
53
+ PaperclipArchiveProcessor::Extractor::Zip.expects(:extract).with(path, Dir.mktmpdir).returns([])
54
+ PaperclipArchiveProcessor::Processor.extract(@dummy.archive)
55
+ end
56
+
57
+ should 'upload to s3' do
58
+ archive = @dummy.archive
59
+ AWS::S3::S3Object.expects(:store).with(
60
+ regexp_matches(/.*[1-5]+$/),
61
+ kind_of(File),
62
+ archive.instance_variable_get(:@options)[:bucket],
63
+ {
64
+ :content_type => archive.content_type,
65
+ :access => archive.instance_variable_get(:@s3_permissions)
66
+ }.merge(archive.instance_variable_get(:@s3_headers))
67
+ ).times(5)
68
+ PaperclipArchiveProcessor::Processor.extract(@dummy.archive)
69
+ end
70
+
71
+ end
72
+
73
+ context 'attachment is configured to use an unknown storage type' do
74
+
75
+ setup do
76
+ Dummy.has_attached_file :archive, :extract_archive => true
77
+ @dummy = Dummy.new
78
+ @dummy.archive = file
79
+ @dummy.archive.instance_variable_set(:@storage, :tape)
80
+ end
81
+
82
+ should 'raise an storage not found error' do
83
+ assert_raise PaperclipArchiveProcessor::Processor::StorageMethodNotFound do
84
+ PaperclipArchiveProcessor::Processor.extract(@dummy.archive)
85
+ end
86
+ end
87
+
88
+ end
89
+
90
+ end
91
+
92
+ end
93
+
94
+ end
metadata ADDED
@@ -0,0 +1,323 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paperclip_archive_processor
3
+ version: !ruby/object:Gem::Version
4
+ hash: 17
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 3
9
+ - 1
10
+ version: 0.3.1
11
+ platform: ruby
12
+ authors:
13
+ - sleistner
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-20 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ type: :runtime
23
+ prerelease: false
24
+ name: paperclip
25
+ version_requirements: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ hash: 5
31
+ segments:
32
+ - 2
33
+ - 3
34
+ - 3
35
+ version: 2.3.3
36
+ requirement: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ type: :runtime
39
+ prerelease: false
40
+ name: zip
41
+ version_requirements: &id002 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ hash: 11
47
+ segments:
48
+ - 2
49
+ - 0
50
+ - 2
51
+ version: 2.0.2
52
+ requirement: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ type: :runtime
55
+ prerelease: false
56
+ name: aws-s3
57
+ version_requirements: &id003 !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 3
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ requirement: *id003
67
+ - !ruby/object:Gem::Dependency
68
+ type: :development
69
+ prerelease: false
70
+ name: shoulda
71
+ version_requirements: &id004 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ hash: 3
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ requirement: *id004
81
+ - !ruby/object:Gem::Dependency
82
+ type: :development
83
+ prerelease: false
84
+ name: mocha
85
+ version_requirements: &id005 !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ hash: 3
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ requirement: *id005
95
+ - !ruby/object:Gem::Dependency
96
+ type: :development
97
+ prerelease: false
98
+ name: redgreen
99
+ version_requirements: &id006 !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ hash: 3
105
+ segments:
106
+ - 0
107
+ version: "0"
108
+ requirement: *id006
109
+ - !ruby/object:Gem::Dependency
110
+ type: :development
111
+ prerelease: false
112
+ name: activerecord
113
+ version_requirements: &id007 !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ hash: 3
119
+ segments:
120
+ - 0
121
+ version: "0"
122
+ requirement: *id007
123
+ - !ruby/object:Gem::Dependency
124
+ type: :development
125
+ prerelease: false
126
+ name: activesupport
127
+ version_requirements: &id008 !ruby/object:Gem::Requirement
128
+ none: false
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ hash: 3
133
+ segments:
134
+ - 0
135
+ version: "0"
136
+ requirement: *id008
137
+ - !ruby/object:Gem::Dependency
138
+ type: :development
139
+ prerelease: false
140
+ name: sqlite3-ruby
141
+ version_requirements: &id009 !ruby/object:Gem::Requirement
142
+ none: false
143
+ requirements:
144
+ - - ~>
145
+ - !ruby/object:Gem::Version
146
+ hash: 27
147
+ segments:
148
+ - 1
149
+ - 3
150
+ - 0
151
+ version: 1.3.0
152
+ requirement: *id009
153
+ - !ruby/object:Gem::Dependency
154
+ type: :development
155
+ prerelease: false
156
+ name: bundler
157
+ version_requirements: &id010 !ruby/object:Gem::Requirement
158
+ none: false
159
+ requirements:
160
+ - - ~>
161
+ - !ruby/object:Gem::Version
162
+ hash: 23
163
+ segments:
164
+ - 1
165
+ - 0
166
+ - 0
167
+ version: 1.0.0
168
+ requirement: *id010
169
+ - !ruby/object:Gem::Dependency
170
+ type: :development
171
+ prerelease: false
172
+ name: jeweler
173
+ version_requirements: &id011 !ruby/object:Gem::Requirement
174
+ none: false
175
+ requirements:
176
+ - - ~>
177
+ - !ruby/object:Gem::Version
178
+ hash: 7
179
+ segments:
180
+ - 1
181
+ - 5
182
+ - 2
183
+ version: 1.5.2
184
+ requirement: *id011
185
+ - !ruby/object:Gem::Dependency
186
+ type: :development
187
+ prerelease: false
188
+ name: rcov
189
+ version_requirements: &id012 !ruby/object:Gem::Requirement
190
+ none: false
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ hash: 3
195
+ segments:
196
+ - 0
197
+ version: "0"
198
+ requirement: *id012
199
+ - !ruby/object:Gem::Dependency
200
+ type: :runtime
201
+ prerelease: false
202
+ name: paperclip
203
+ version_requirements: &id013 !ruby/object:Gem::Requirement
204
+ none: false
205
+ requirements:
206
+ - - ~>
207
+ - !ruby/object:Gem::Version
208
+ hash: 5
209
+ segments:
210
+ - 2
211
+ - 3
212
+ - 3
213
+ version: 2.3.3
214
+ requirement: *id013
215
+ - !ruby/object:Gem::Dependency
216
+ type: :runtime
217
+ prerelease: false
218
+ name: zip
219
+ version_requirements: &id014 !ruby/object:Gem::Requirement
220
+ none: false
221
+ requirements:
222
+ - - ">="
223
+ - !ruby/object:Gem::Version
224
+ hash: 11
225
+ segments:
226
+ - 2
227
+ - 0
228
+ - 2
229
+ version: 2.0.2
230
+ requirement: *id014
231
+ - !ruby/object:Gem::Dependency
232
+ type: :runtime
233
+ prerelease: false
234
+ name: aws-s3
235
+ version_requirements: &id015 !ruby/object:Gem::Requirement
236
+ none: false
237
+ requirements:
238
+ - - ">="
239
+ - !ruby/object:Gem::Version
240
+ hash: 3
241
+ segments:
242
+ - 0
243
+ version: "0"
244
+ requirement: *id015
245
+ description: "\n extract paperclip attachments and save them to file or s3 storage\n "
246
+ email: sleistner@gmail.com
247
+ executables: []
248
+
249
+ extensions: []
250
+
251
+ extra_rdoc_files:
252
+ - LICENSE.txt
253
+ - README.rdoc
254
+ files:
255
+ - .document
256
+ - Gemfile
257
+ - Gemfile.lock
258
+ - LICENSE.txt
259
+ - README.rdoc
260
+ - Rakefile
261
+ - VERSION
262
+ - lib/paperclip_archive_processor.rb
263
+ - lib/paperclip_archive_processor/extractor.rb
264
+ - lib/paperclip_archive_processor/integration.rb
265
+ - lib/paperclip_archive_processor/processor.rb
266
+ - lib/paperclip_archive_processor/zip_util.rb
267
+ - paperclip_archive_processor.gemspec
268
+ - public/system/archives/1/original/1
269
+ - public/system/archives/1/original/2
270
+ - public/system/archives/1/original/3
271
+ - public/system/archives/1/original/4
272
+ - public/system/archives/1/original/5
273
+ - public/system/archives/1/original/5x.zip
274
+ - test/database.yml
275
+ - test/debug.log
276
+ - test/extractor_test.rb
277
+ - test/fixtures/1x.zip
278
+ - test/fixtures/2x.zip
279
+ - test/fixtures/3x.zip
280
+ - test/fixtures/4x.zip
281
+ - test/fixtures/5x.zip
282
+ - test/helper.rb
283
+ - test/integration_test.rb
284
+ - test/processor_test.rb
285
+ has_rdoc: true
286
+ homepage: http://github.com/sleistner/paperclip_archive_processor
287
+ licenses:
288
+ - MIT
289
+ post_install_message:
290
+ rdoc_options: []
291
+
292
+ require_paths:
293
+ - lib
294
+ required_ruby_version: !ruby/object:Gem::Requirement
295
+ none: false
296
+ requirements:
297
+ - - ">="
298
+ - !ruby/object:Gem::Version
299
+ hash: 3
300
+ segments:
301
+ - 0
302
+ version: "0"
303
+ required_rubygems_version: !ruby/object:Gem::Requirement
304
+ none: false
305
+ requirements:
306
+ - - ">="
307
+ - !ruby/object:Gem::Version
308
+ hash: 3
309
+ segments:
310
+ - 0
311
+ version: "0"
312
+ requirements: []
313
+
314
+ rubyforge_project:
315
+ rubygems_version: 1.3.7
316
+ signing_key:
317
+ specification_version: 3
318
+ summary: extract paperclip attachments and save them to file or s3 storage
319
+ test_files:
320
+ - test/extractor_test.rb
321
+ - test/helper.rb
322
+ - test/integration_test.rb
323
+ - test/processor_test.rb