paperclip-content_type_processor 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cecc56af7e3b9b24b6a1c8a59191b37233747e1e
4
+ data.tar.gz: b5831c27b5aa1bc54705541aed86357574e51de0
5
+ SHA512:
6
+ metadata.gz: b11c1ab1283fb52bcc805a02fd7d3bd6216bd7725c12cedc96cdb40d992077798eb49b06ca9f417f3abdafd0277e9417119b0acaa1c9632c55674e68a08a3eca
7
+ data.tar.gz: 77f069367e16743248a40c6076b79fbed82cd3d9d22b0f9ccc4752406423b1500d285a8a4d0f18c641e994f745b40a0e2d51602508a46c532f668bc5f0596049
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in paperclip-content_type_processor.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 ingeniarius
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # Paperclip::ContentTypeProcessor
2
+
3
+ Allows to add file processing by content type for paperclip attachments.
4
+
5
+ ## Requirements
6
+
7
+ libmagic
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'paperclip-content_type_processor'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it by yourself as:
20
+
21
+ $ gem install paperclip-content_type_processor
22
+
23
+ ## Usage
24
+
25
+ 1. Specify file processing per content type:
26
+
27
+ ````ruby
28
+ Paperclip::ContentTypeProcessor.add_content_type_processor("content/type", "command", "command arguments")
29
+ ````
30
+
31
+ 2. Add content type processor for attachment:
32
+
33
+ ````ruby
34
+ has_attached_file :avatar,
35
+ styles: { medium: '96x96#' },
36
+ processors: [:thumbnail, :content_type_processor]
37
+ end
38
+ ````
39
+
40
+ ## Example
41
+
42
+ It can be used for image compression/optimizations.
43
+
44
+ 1. Install image optimizers:
45
+ * jpegoptim (http://freecode.com/projects/jpegoptim)
46
+ * optipng (http://optipng.sourceforge.net/)
47
+
48
+ 2. Add initializer in your rails app <code>config/initializers/content_type_processor.rb</code>,
49
+ and specify file processing per content type:
50
+
51
+ ````ruby
52
+ Paperclip::ContentTypeProcessor.add_content_type_processor("image/jpeg", "jpegoptim", "--strip-all --max=90")
53
+ Paperclip::ContentTypeProcessor.add_content_type_processor("image/png", "optipng", "-o6 -strip all")
54
+ ````
55
+
56
+ 3. Enable content type processing for user avatars:
57
+
58
+ ````ruby
59
+ class User < ActiveRecord::Base
60
+ has_attached_file :avatar,
61
+ styles: { medium: '96x96#' },
62
+ processors: [:thumbnail, :content_type_processor]
63
+ end
64
+ ````
65
+
66
+ So all user avatars will be processed by 'jpegoptim' or 'optipng' according to its content type.
67
+
68
+ *Note! If you want to generate thumbnails, you should also specify :thumbnails in processor list*
69
+
70
+
71
+ ## Contributing
72
+
73
+ 1. Fork it ( http://github.com/ingeniarius/paperclip-content_type_processor/fork )
74
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
75
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
76
+ 4. Push to the branch (`git push origin my-new-feature`)
77
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,7 @@
1
+ require "paperclip"
2
+
3
+ module Paperclip
4
+ class ContentTypeProcessor < Processor
5
+ VERSION = "0.0.1"
6
+ end
7
+ end
@@ -0,0 +1,51 @@
1
+ require "paperclip"
2
+ require "paperclip/content_type_processor/version"
3
+ require "filemagic"
4
+
5
+ module Paperclip
6
+ class ContentTypeProcessor < Processor
7
+
8
+ Processor = Struct.new(:command, :argumments)
9
+
10
+ def self.add_content_type_processor(content_type, command, argumments)
11
+ @processors ||= {}
12
+ @processors[content_type] = Processor.new(command, argumments)
13
+ end
14
+
15
+ def self.content_type_processor_for(content_type)
16
+ @processors ||= {}
17
+ @processors[content_type]
18
+ end
19
+
20
+ def make
21
+ content_type_processor = ContentTypeProcessor.content_type_processor_for(file_content_type)
22
+
23
+ if content_type_processor
24
+ exec(content_type_processor)
25
+ else
26
+ file
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def exec(content_type_processor)
33
+ whiny = options.has_key?(:whiny) ? options[:whiny] : true
34
+ src_path = File.expand_path(file.path)
35
+
36
+ begin
37
+ Paperclip.run(content_type_processor.command, "#{content_type_processor.argumments} :src_path", src_path: src_path)
38
+ file
39
+ rescue Cocaine::ExitStatusError => e
40
+ raise Paperclip::Error, "#{content_type_processor.command} : There was an error processing the thumbnail for #{src_path}" if whiny
41
+ rescue Cocaine::CommandNotFoundError => e
42
+ raise Paperclip::Errors::CommandNotFoundError.new("Could not run '#{content_type_processor.command}'. Please install #{content_type_processor.command}.")
43
+ end
44
+ end
45
+
46
+ def file_content_type
47
+ file.is_a?(Paperclip::AbstractAdapter) ? file.content_type : FileMagic.fm(FileMagic::MAGIC_MIME_TYPE).file(file.respond_to?(:path) ? file.path : file)
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'paperclip/content_type_processor/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "paperclip-content_type_processor"
8
+ spec.version = Paperclip::ContentTypeProcessor::VERSION
9
+ spec.date = "2014-02-17"
10
+ spec.authors = ["ingeniarius"]
11
+ spec.email = ["mail@ingeniarius.name"]
12
+ spec.summary = %q{Paperclip attachement processing by content type}
13
+ spec.description = %q{This gem is Paperclip processor which allow specify file processing based on his content type}
14
+
15
+ spec.homepage = "https://github.com/ingeniarius/paperclip-content_type_processor"
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0")
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_runtime_dependency "paperclip", ">= 3.3"
24
+ spec.add_runtime_dependency "ruby-filemagic", "~> 0.5.0"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.5"
27
+ spec.add_development_dependency "rake"
28
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paperclip-content_type_processor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - ingeniarius
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: paperclip
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: ruby-filemagic
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.5.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.5.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: This gem is Paperclip processor which allow specify file processing based
70
+ on his content type
71
+ email:
72
+ - mail@ingeniarius.name
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - lib/paperclip/content_type_processor.rb
83
+ - lib/paperclip/content_type_processor/version.rb
84
+ - paperclip-content_type_processor.gemspec
85
+ homepage: https://github.com/ingeniarius/paperclip-content_type_processor
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.2.0
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Paperclip attachement processing by content type
109
+ test_files: []