paperclip_archive_processor 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,22 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ gem 'paperclip', '~> 2.3.3'
7
+ gem 'zip', '>= 2.0.2'
8
+ gem 'aws-s3', :require => 'aws/s3'
9
+
10
+ # Add dependencies to develop your gem here.
11
+ # Include everything needed to run rake, tests, features, etc.
12
+ group :development do
13
+ gem "shoulda", ">= 0"
14
+ gem "mocha", ">= 0"
15
+ gem "redgreen"
16
+ gem "activerecord"
17
+ gem "activesupport"
18
+ gem "sqlite3-ruby", "~>1.3.0"
19
+ gem "bundler", "~> 1.0.0"
20
+ gem "jeweler", "~> 1.5.2"
21
+ gem "rcov", ">= 0"
22
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,56 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (3.0.3)
5
+ activesupport (= 3.0.3)
6
+ builder (~> 2.1.2)
7
+ i18n (~> 0.4)
8
+ activerecord (3.0.3)
9
+ activemodel (= 3.0.3)
10
+ activesupport (= 3.0.3)
11
+ arel (~> 2.0.2)
12
+ tzinfo (~> 0.3.23)
13
+ activesupport (3.0.3)
14
+ arel (2.0.7)
15
+ aws-s3 (0.6.2)
16
+ builder
17
+ mime-types
18
+ xml-simple
19
+ builder (2.1.2)
20
+ git (1.2.5)
21
+ i18n (0.5.0)
22
+ jeweler (1.5.2)
23
+ bundler (~> 1.0.0)
24
+ git (>= 1.2.5)
25
+ rake
26
+ mime-types (1.16)
27
+ mocha (0.9.10)
28
+ rake
29
+ paperclip (2.3.8)
30
+ activerecord
31
+ activesupport
32
+ rake (0.8.7)
33
+ rcov (0.9.9)
34
+ redgreen (1.2.2)
35
+ shoulda (2.11.3)
36
+ sqlite3-ruby (1.3.2)
37
+ tzinfo (0.3.24)
38
+ xml-simple (1.0.12)
39
+ zip (2.0.2)
40
+
41
+ PLATFORMS
42
+ ruby
43
+
44
+ DEPENDENCIES
45
+ activerecord
46
+ activesupport
47
+ aws-s3
48
+ bundler (~> 1.0.0)
49
+ jeweler (~> 1.5.2)
50
+ mocha
51
+ paperclip (~> 2.3.3)
52
+ rcov
53
+ redgreen
54
+ shoulda
55
+ sqlite3-ruby (~> 1.3.0)
56
+ zip (>= 2.0.2)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 sleistner
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = paperclip_archive_processor
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to paperclip_archive_processor
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 sleistner. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,61 @@
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 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ gem.name = "paperclip_archive_processor"
15
+ gem.homepage = "http://github.com/sleistner/paperclip_archive_processor"
16
+ gem.license = "MIT"
17
+ gem.summary = %Q{
18
+ extract paperclip attachments and save them to file or s3 storage
19
+ }
20
+ gem.description = %Q{
21
+ extract paperclip attachments and save them to file or s3 storage
22
+ }
23
+ gem.email = "sleistner@gmail.com"
24
+ gem.authors = ["sleistner"]
25
+ gem.add_dependency 'paperclip', '~> 2.3.3'
26
+ gem.add_dependency 'zip', '>= 2.0.2'
27
+ gem.add_dependency 'aws-s3'
28
+ end
29
+ Jeweler::RubygemsDotOrgTasks.new
30
+
31
+ require 'rake/testtask'
32
+ Rake::TestTask.new(:test) do |test|
33
+ test.libs << 'lib' << 'test'
34
+ test.pattern = 'test/**/*_test.rb'
35
+ test.verbose = true
36
+ end
37
+
38
+ require 'rcov/rcovtask'
39
+ Rcov::RcovTask.new do |test|
40
+ test.libs << 'test'
41
+ test.pattern = 'test/**/*_test.rb'
42
+ test.verbose = true
43
+ end
44
+
45
+ task :default => :test
46
+
47
+ require 'rake/rdoctask'
48
+ Rake::RDocTask.new do |rdoc|
49
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "paperclip_archive_processor #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
56
+
57
+ desc 'Start an IRB session with all necessary files required.'
58
+ task :shell do |t|
59
+ chdir File.dirname(__FILE__)
60
+ exec 'irb -I lib/ -I lib/paperclip_archive_processor -r rubygems -r tempfile -r test/helper'
61
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.1
@@ -0,0 +1,11 @@
1
+ require 'paperclip'
2
+ require 'paperclip_archive_processor/integration'
3
+ require 'paperclip_archive_processor/processor'
4
+ require 'paperclip_archive_processor/extractor'
5
+ require 'paperclip_archive_processor/zip_util'
6
+
7
+ if defined?(Paperclip::InstanceMethods)
8
+ Paperclip::ClassMethods.send(:extend, PaperclipArchiveProcessor::Integration)
9
+ end
10
+
11
+
@@ -0,0 +1,27 @@
1
+ module PaperclipArchiveProcessor
2
+
3
+ module Extractor
4
+
5
+ class ExtractorNotFound < StandardError; end
6
+
7
+ def self.extractor_for(content_type)
8
+ case content_type
9
+ when /zip/ then Zip
10
+ else
11
+ raise ExtractorNotFound, "Cannot load extractor for content type '#{content_type}'"
12
+ end
13
+ end
14
+
15
+ class Zip
16
+
17
+ def self.extract(file, destination)
18
+ PaperclipArchiveProcessor::ZipUtil.unpack(file, destination).map do |entry|
19
+ entry.name
20
+ end
21
+ end
22
+
23
+ end
24
+
25
+ end
26
+
27
+ end
@@ -0,0 +1,66 @@
1
+ module PaperclipArchiveProcessor
2
+
3
+ module Integration
4
+ def self.extended(base)
5
+ base.extend ClassMethods
6
+ end
7
+ end
8
+
9
+ module ClassMethods
10
+
11
+ def self.extended(base)
12
+ base.module_eval do
13
+
14
+ private
15
+
16
+ def create_callback(name, &block)
17
+ class_eval do
18
+ define_method(name, &block)
19
+ end
20
+ end
21
+
22
+ def define_before_save_callback(variable_name, attachment_name)
23
+ name = "before_save_filter_for_#{attachment_name}"
24
+ create_callback(name) do
25
+ instance_variable_set(variable_name, try(attachment_name).dirty?)
26
+ true
27
+ end
28
+ send(:before_save, name)
29
+ end
30
+
31
+ def define_after_save_callback(variable_name, attachment_name)
32
+ name = "after_save_filter_for_#{attachment_name}"
33
+ create_callback(name) do
34
+ if instance_variable_get(variable_name)
35
+ PaperclipArchiveProcessor::Processor.extract(try(attachment_name))
36
+ true
37
+ end
38
+ end
39
+ send(:after_save, name)
40
+ end
41
+
42
+ def define_processor_callbacks(attachment_name)
43
+ variable_name = "@_#{attachment_name}_has_changed"
44
+ define_before_save_callback(variable_name, attachment_name)
45
+ define_after_save_callback(variable_name, attachment_name)
46
+ end
47
+
48
+ public
49
+
50
+ alias has_attached_file_original has_attached_file
51
+
52
+ def has_attached_file(name, options = {})
53
+ has_attached_file_original(name, options)
54
+
55
+ if options.delete(:extract_archive)
56
+ define_processor_callbacks(name)
57
+ end
58
+ end
59
+ end
60
+
61
+ end
62
+
63
+ end
64
+
65
+ end
66
+
@@ -0,0 +1,63 @@
1
+ module PaperclipArchiveProcessor
2
+
3
+ module Processor
4
+
5
+ class StorageMethodNotFound < StandardError; end
6
+
7
+ def self.extract(attachment)
8
+ extractor = PaperclipArchiveProcessor::Extractor.extractor_for(attachment.content_type)
9
+ processor = processor_for(attachment)
10
+ processor.extract(attachment, extractor)
11
+ end
12
+
13
+ private
14
+
15
+ def self.processor_for(attachment)
16
+ storage_class_name = attachment.instance_variable_get(:@storage).to_s.capitalize
17
+ begin
18
+ PaperclipArchiveProcessor::Processor.const_get(storage_class_name)
19
+ rescue NameError
20
+ raise StorageMethodNotFound, "Cannot load storage module '#{storage_class_name}'"
21
+ end
22
+ end
23
+
24
+ class Filesystem
25
+ def self.extract(attachment, extractor)
26
+ path = attachment.to_file.path
27
+ extractor.extract(path, File.dirname(path))
28
+ end
29
+ end
30
+
31
+ class S3
32
+
33
+ def self.extract(attachment, extractor)
34
+ extract_path = Dir.mktmpdir
35
+ s3_path_prefix = File.dirname(attachment.path)
36
+ bucket_name = attachment.instance_variable_get(:@options)[:bucket]
37
+ options = options_for(attachment)
38
+
39
+ extractor.extract(attachment.to_file.path, extract_path).each do |name|
40
+ file = File.join(extract_path, name)
41
+ write(File.join(s3_path_prefix, name), File.new(file), bucket_name, options) if File.file?(file)
42
+ end
43
+ end
44
+
45
+ def self.options_for(attachment)
46
+ { :content_type => attachment.content_type,
47
+ :access => attachment.instance_variable_get(:@s3_permissions)
48
+ }.merge(attachment.instance_variable_get(:@s3_headers))
49
+ end
50
+
51
+ def self.write(path, file, bucket_name, options)
52
+ begin
53
+ AWS::S3::S3Object.store(path, file, bucket_name, options)
54
+ rescue AWS::S3::ResponseError
55
+ raise
56
+ end
57
+ end
58
+
59
+ end
60
+
61
+ end
62
+
63
+ end
@@ -0,0 +1,74 @@
1
+ require 'zip/zipfilesystem'
2
+ require 'fileutils'
3
+
4
+ module PaperclipArchiveProcessor
5
+
6
+ class ZipUtil #:nodoc:
7
+
8
+ # Pack up an archive from a directory on disk
9
+ def self.pack(input, archive, excludes)
10
+ Zip::ZipFile.open(archive, Zip::ZipFile::CREATE) do |zip|
11
+ add_file_to_zip(zip, input, excludes)
12
+ end
13
+ end
14
+
15
+ def self.pack_single_file(input, archive)
16
+ Zip::ZipFile.open(archive, Zip::ZipFile::CREATE) do |zip|
17
+ file_name = File.basename(input)
18
+ zip.add(file_name, input)
19
+ end
20
+ end
21
+
22
+ # Unpack an archive to a directory on disk
23
+ def self.unpack(archive, destination)
24
+ Zip::ZipFile.open(archive) do |zip|
25
+ unpack_file(zip, destination)
26
+ end
27
+ end
28
+
29
+ def self.unpack_file(zip, destination, path='')
30
+ if(zip.file.file?(path))
31
+ FileUtils.mkdir_p(destination)
32
+ File.open(File.join(destination, path), 'w') do |dest|
33
+ zip.file.open(path) do |src|
34
+ dest.write src.read
35
+ end
36
+ end
37
+ else
38
+ dir = File.join(destination, path)
39
+ Dir.mkdir(dir) rescue puts("Directory Exists #{dir}")
40
+ zip.dir.foreach(path) do |dir|
41
+ unpack_file(zip, destination, File.join(path, dir))
42
+ end
43
+ end
44
+ end
45
+
46
+ def self.add_file_to_zip(zip, path, excludes)
47
+ if(File.directory?(path))
48
+ zip.dir.mkdir(path)
49
+ Dir.open(path).each do |child|
50
+ if(!excluded?(child, excludes))
51
+ add_file_to_zip(zip, File.join(path, child), excludes)
52
+ end
53
+ end
54
+ else
55
+ File.open(path) do |src|
56
+ zip.file.open(path, 'w') do |dest|
57
+ dest.write src.read
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+ def self.excluded?(str, excludes)
64
+ excludes.each do |exc|
65
+ if(str == exc)
66
+ return true
67
+ end
68
+ end
69
+ return false
70
+ end
71
+
72
+ end
73
+
74
+ end
@@ -0,0 +1,120 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{paperclip_archive_processor}
8
+ s.version = "0.3.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["sleistner"]
12
+ s.date = %q{2011-01-20}
13
+ s.description = %q{
14
+ extract paperclip attachments and save them to file or s3 storage
15
+ }
16
+ s.email = %q{sleistner@gmail.com}
17
+ s.extra_rdoc_files = [
18
+ "LICENSE.txt",
19
+ "README.rdoc"
20
+ ]
21
+ s.files = [
22
+ ".document",
23
+ "Gemfile",
24
+ "Gemfile.lock",
25
+ "LICENSE.txt",
26
+ "README.rdoc",
27
+ "Rakefile",
28
+ "VERSION",
29
+ "lib/paperclip_archive_processor.rb",
30
+ "lib/paperclip_archive_processor/extractor.rb",
31
+ "lib/paperclip_archive_processor/integration.rb",
32
+ "lib/paperclip_archive_processor/processor.rb",
33
+ "lib/paperclip_archive_processor/zip_util.rb",
34
+ "paperclip_archive_processor.gemspec",
35
+ "public/system/archives/1/original/1",
36
+ "public/system/archives/1/original/2",
37
+ "public/system/archives/1/original/3",
38
+ "public/system/archives/1/original/4",
39
+ "public/system/archives/1/original/5",
40
+ "public/system/archives/1/original/5x.zip",
41
+ "test/database.yml",
42
+ "test/debug.log",
43
+ "test/extractor_test.rb",
44
+ "test/fixtures/1x.zip",
45
+ "test/fixtures/2x.zip",
46
+ "test/fixtures/3x.zip",
47
+ "test/fixtures/4x.zip",
48
+ "test/fixtures/5x.zip",
49
+ "test/helper.rb",
50
+ "test/integration_test.rb",
51
+ "test/processor_test.rb"
52
+ ]
53
+ s.homepage = %q{http://github.com/sleistner/paperclip_archive_processor}
54
+ s.licenses = ["MIT"]
55
+ s.require_paths = ["lib"]
56
+ s.rubygems_version = %q{1.3.7}
57
+ s.summary = %q{extract paperclip attachments and save them to file or s3 storage}
58
+ s.test_files = [
59
+ "test/extractor_test.rb",
60
+ "test/helper.rb",
61
+ "test/integration_test.rb",
62
+ "test/processor_test.rb"
63
+ ]
64
+
65
+ if s.respond_to? :specification_version then
66
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
67
+ s.specification_version = 3
68
+
69
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
70
+ s.add_runtime_dependency(%q<paperclip>, ["~> 2.3.3"])
71
+ s.add_runtime_dependency(%q<zip>, [">= 2.0.2"])
72
+ s.add_runtime_dependency(%q<aws-s3>, [">= 0"])
73
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
74
+ s.add_development_dependency(%q<mocha>, [">= 0"])
75
+ s.add_development_dependency(%q<redgreen>, [">= 0"])
76
+ s.add_development_dependency(%q<activerecord>, [">= 0"])
77
+ s.add_development_dependency(%q<activesupport>, [">= 0"])
78
+ s.add_development_dependency(%q<sqlite3-ruby>, ["~> 1.3.0"])
79
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
80
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
81
+ s.add_development_dependency(%q<rcov>, [">= 0"])
82
+ s.add_runtime_dependency(%q<paperclip>, ["~> 2.3.3"])
83
+ s.add_runtime_dependency(%q<zip>, [">= 2.0.2"])
84
+ s.add_runtime_dependency(%q<aws-s3>, [">= 0"])
85
+ else
86
+ s.add_dependency(%q<paperclip>, ["~> 2.3.3"])
87
+ s.add_dependency(%q<zip>, [">= 2.0.2"])
88
+ s.add_dependency(%q<aws-s3>, [">= 0"])
89
+ s.add_dependency(%q<shoulda>, [">= 0"])
90
+ s.add_dependency(%q<mocha>, [">= 0"])
91
+ s.add_dependency(%q<redgreen>, [">= 0"])
92
+ s.add_dependency(%q<activerecord>, [">= 0"])
93
+ s.add_dependency(%q<activesupport>, [">= 0"])
94
+ s.add_dependency(%q<sqlite3-ruby>, ["~> 1.3.0"])
95
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
96
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
97
+ s.add_dependency(%q<rcov>, [">= 0"])
98
+ s.add_dependency(%q<paperclip>, ["~> 2.3.3"])
99
+ s.add_dependency(%q<zip>, [">= 2.0.2"])
100
+ s.add_dependency(%q<aws-s3>, [">= 0"])
101
+ end
102
+ else
103
+ s.add_dependency(%q<paperclip>, ["~> 2.3.3"])
104
+ s.add_dependency(%q<zip>, [">= 2.0.2"])
105
+ s.add_dependency(%q<aws-s3>, [">= 0"])
106
+ s.add_dependency(%q<shoulda>, [">= 0"])
107
+ s.add_dependency(%q<mocha>, [">= 0"])
108
+ s.add_dependency(%q<redgreen>, [">= 0"])
109
+ s.add_dependency(%q<activerecord>, [">= 0"])
110
+ s.add_dependency(%q<activesupport>, [">= 0"])
111
+ s.add_dependency(%q<sqlite3-ruby>, ["~> 1.3.0"])
112
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
113
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
114
+ s.add_dependency(%q<rcov>, [">= 0"])
115
+ s.add_dependency(%q<paperclip>, ["~> 2.3.3"])
116
+ s.add_dependency(%q<zip>, [">= 2.0.2"])
117
+ s.add_dependency(%q<aws-s3>, [">= 0"])
118
+ end
119
+ end
120
+