kt-paperclip-compression 1.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
+ SHA256:
3
+ metadata.gz: 388c0d55aefababf29de64ca183f7150ed864c3bd8c32bea1be5bdd96a0c2ac0
4
+ data.tar.gz: ac1c7bd982f52d1242ceeec563403a9b2646c6974d36686d11fa2f36b7860d19
5
+ SHA512:
6
+ metadata.gz: 94d1c13bfc17cab2647ffe1a81a85ab1923842b8d8792f1e85d1ea1721d2c0bce73505e1efb57bd65235c2a23b9fa834eec58fe9d63307a5db9d442cf755d9e6
7
+ data.tar.gz: a6a9bc7bffb5c9ad747ee73f4f73278231135e5b69ab2947b9fa13eaaa26da88b878e272c85230dd0372f28c7410d5b918259bf6695b26bbd6dc9d888e8e3f70
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1 @@
1
+ require 'paperclip-compression'
@@ -0,0 +1,13 @@
1
+ require 'kt-paperclip'
2
+ require 'os'
3
+ require 'paperclip-compression/paperclip/compression'
4
+ require 'paperclip-compression/base'
5
+ require 'paperclip-compression/config'
6
+ require 'paperclip-compression/jpeg'
7
+ require 'paperclip-compression/png'
8
+
9
+ module PaperclipCompression
10
+ def self.root
11
+ Gem::Specification.find_by_name('kt-paperclip-compression').gem_dir
12
+ end
13
+ end
@@ -0,0 +1,85 @@
1
+ module PaperclipCompression
2
+
3
+ ExitStatusError = defined?(Cocaine) ? Cocaine::ExitStatusError : Terrapin::ExitStatusError
4
+ CommandNotFoundError = defined?(Cocaine) ? Cocaine::CommandNotFoundError : Terrapin::CommandNotFoundError
5
+
6
+ class Base
7
+
8
+ def initialize(file, first_processor)
9
+ @file = file
10
+ current_extension = File.extname(file.path)
11
+ @basename = File.basename(file.path, current_extension)
12
+ @dst = Paperclip::TempfileFactory.new.generate(@basename)
13
+ @dst_path = File.expand_path(@dst.path)
14
+ @src_path = File.expand_path(@file.path)
15
+ @first_processor = first_processor
16
+ end
17
+
18
+ def self.make(file, first_processor, options = {})
19
+ new(file, first_processor, options).make
20
+ end
21
+
22
+ def process_file
23
+ # Close output file so compressors which require exclusive file rights
24
+ # work.
25
+ @dst.close
26
+
27
+ # Execute the child-compressor classes implementation of how to compress
28
+ # the output
29
+ compress
30
+
31
+ # Re-open the output file so downstream paperclip-middleware may
32
+ # read/write/etc. without having to re-open the file.
33
+ @dst.open
34
+
35
+ # Return the destination file for downstream paperclip processors.
36
+ @dst
37
+ end
38
+
39
+ protected
40
+
41
+ def process_file?
42
+ @cli_opts
43
+ end
44
+
45
+ def unprocessed_tempfile
46
+ copy_to_tempfile
47
+ first_processor? ? @dst : @file
48
+ end
49
+
50
+ def command_path(command)
51
+ folder = if OS.osx?
52
+ File.join('osx', catalina? ? '64bit' : '32bit')
53
+ elsif OS.linux?
54
+ File.join('linux', OS.bits.eql?(64) ? 'x64' : 'x86')
55
+ elsif OS.windows?
56
+ OS.bits.eql?(64) ? 'win64' : 'win32'
57
+ end
58
+
59
+ File.join(PaperclipCompression.root, 'bin', folder, command)
60
+ end
61
+
62
+ private
63
+
64
+ def compress
65
+ fail MustImplementInSubClassesException,
66
+ 'compress is overridden on a per compressor basis.'
67
+ end
68
+
69
+ def first_processor?
70
+ @first_processor
71
+ end
72
+
73
+ def copy_to_tempfile
74
+ FileUtils.cp(@src_path, @dst_path)
75
+ end
76
+
77
+ def catalina?
78
+ major = OS.host_os.match(/darwin(\d+)\./)[1].to_i
79
+ major >= 19
80
+ end
81
+ end
82
+
83
+ # Informs developers when a method is intended to be defined in # sub-classes.
84
+ class MustImplementInSubClassesException < Exception; end
85
+ end
@@ -0,0 +1,85 @@
1
+ module PaperclipCompression
2
+ class Config
3
+
4
+ PROCESSOR_OPTIONS_KEY = :processor_options
5
+ KEY = :compression
6
+
7
+ def self.create_with_fallbacks(style_options, type_key, gem_defaults)
8
+ gem_config = new({ KEY => { type_key => gem_defaults } }, type_key, nil, nil)
9
+ defaults_config = new(Paperclip::Attachment.default_options, type_key, gem_config, Paperclip::Attachment.default_options[:whiny])
10
+ new(style_options[PROCESSOR_OPTIONS_KEY], type_key, defaults_config, style_options[:whiny])
11
+ end
12
+
13
+ def initialize(options, type_key, fallback, whiny)
14
+ @whiny = whiny
15
+ @fallback = fallback
16
+ parse_options(options, type_key)
17
+ end
18
+
19
+ def process_file?
20
+ if defined?(@process_file)
21
+ @process_file
22
+ elsif @fallback
23
+ @fallback.process_file?
24
+ else
25
+ raise('options or fallback should have attributes')
26
+ end
27
+ end
28
+
29
+ def command
30
+ if defined?(@command)
31
+ @command
32
+ elsif @fallback
33
+ @fallback.command
34
+ else
35
+ raise("options or fallback should have 'command'")
36
+ end
37
+ end
38
+
39
+ def options
40
+ if defined?(@options)
41
+ @options
42
+ elsif @fallback
43
+ @fallback.options
44
+ else
45
+ raise("options or fallback should have 'options'")
46
+ end
47
+ end
48
+
49
+ def whiny
50
+ @whiny
51
+ end
52
+
53
+ private
54
+
55
+ def parse_options(options, type_key)
56
+ if options && options.has_key?(KEY)
57
+ compression_opts = options[KEY]
58
+
59
+ if compression_opts.eql?(true)
60
+ @process_file = true
61
+ elsif compression_opts.eql?(false) || compression_opts.eql?(nil)
62
+ @process_file = false
63
+ elsif compression_opts.is_a?(Hash) && compression_opts.has_key?(type_key)
64
+ parse_type_options(compression_opts[type_key])
65
+ end
66
+ end
67
+ end
68
+
69
+ def parse_type_options(type_opts)
70
+ if type_opts
71
+ @process_file = true
72
+
73
+ if type_opts.is_a?(String)
74
+ @options = type_opts
75
+ elsif type_opts.is_a?(Hash)
76
+ @command = type_opts[:command] if type_opts.has_key?(:command)
77
+ @options = type_opts[:options] if type_opts.has_key?(:options)
78
+ end
79
+ else
80
+ @process_file = false
81
+ end
82
+ end
83
+
84
+ end
85
+ end
@@ -0,0 +1,39 @@
1
+ module PaperclipCompression
2
+ class Jpeg < Base
3
+
4
+ KEY = :jpeg
5
+ JPEGTRAN_DEFAULT_OPTS = '-copy none -optimize -perfect'
6
+
7
+ def initialize(file, first_processor, options = {})
8
+ super(file, first_processor)
9
+ @config = PaperclipCompression::Config.create_with_fallbacks(options, KEY, gem_defaults)
10
+ end
11
+
12
+ def make
13
+ begin
14
+ @config.process_file? ? process_file : unprocessed_tempfile
15
+ rescue ExitStatusError
16
+ raise Paperclip::Error, "JPEGTRAN : There was an error processing #{@basename}" if @config.whiny
17
+ rescue CommandNotFoundError
18
+ raise Paperclip::Errors::CommandNotFoundError.new("Could not run 'jpegtran'. Please install jpegtran.")
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def gem_defaults
25
+ {
26
+ command: command_path('jpegtran'),
27
+ options: JPEGTRAN_DEFAULT_OPTS
28
+ }
29
+ end
30
+
31
+ def compress
32
+ Paperclip.run(
33
+ @config.command,
34
+ "#{@config.options} :src_path > :dst_path",
35
+ src_path: @src_path, dst_path: @dst_path
36
+ )
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,33 @@
1
+ module Paperclip
2
+ # Compresses the JPEG and PNG files
3
+ class Compression < Processor
4
+
5
+ def make
6
+ case content_type
7
+ when 'image/jpeg' then make_jpeg
8
+ when 'image/png' then make_png
9
+ else
10
+ @file
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def content_type
17
+ first_processor? ? @file.content_type : Paperclip::ContentTypeDetector.new(@file.path).detect
18
+ end
19
+
20
+ def first_processor?
21
+ @first_processor ||= @file.is_a?(Paperclip::AbstractAdapter)
22
+ end
23
+
24
+ def make_jpeg
25
+ PaperclipCompression::Jpeg.make(@file, first_processor?, @options)
26
+ end
27
+
28
+ def make_png
29
+ PaperclipCompression::Png.make(@file, first_processor?, @options)
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,39 @@
1
+ module PaperclipCompression
2
+ class Png < Base
3
+
4
+ KEY = :png
5
+ OPTIPNG_DEFAULT_OPTS = '-o 5 -quiet'
6
+
7
+ def initialize(file, first_processor, options = {})
8
+ super(file, first_processor)
9
+ @config = PaperclipCompression::Config.create_with_fallbacks(options, KEY, gem_defaults)
10
+ end
11
+
12
+ def make
13
+ begin
14
+ @config.process_file? ? process_file : unprocessed_tempfile
15
+ rescue ExitStatusError
16
+ raise Paperclip::Error, "OPTIPNG : There was an error processing #{@basename}" if @config.whiny
17
+ rescue CommandNotFoundError
18
+ raise Paperclip::Errors::CommandNotFoundError.new("Could not run 'optipng'. Please install optipng.")
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def gem_defaults
25
+ {
26
+ command: command_path('optipng'),
27
+ options: OPTIPNG_DEFAULT_OPTS
28
+ }
29
+ end
30
+
31
+ def compress
32
+ Paperclip.run(
33
+ @config.command,
34
+ "#{@config.options} -clobber :src_path -out :dst_path",
35
+ src_path: @src_path, dst_path: @dst_path
36
+ )
37
+ end
38
+ end
39
+ end
metadata ADDED
@@ -0,0 +1,152 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kt-paperclip-compression
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.0
5
+ platform: ruby
6
+ authors:
7
+ - İ. Emre Kutlu, Adam Anderson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-03-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: kt-paperclip
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '6.4'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 6.4.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '6.4'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 6.4.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: os
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 1.0.0
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 1.0.0
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '1.3'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '1.3'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rake
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 12.3.3
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 12.3.3
75
+ - !ruby/object:Gem::Dependency
76
+ name: rspec
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - '='
80
+ - !ruby/object:Gem::Version
81
+ version: 3.7.0
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - '='
87
+ - !ruby/object:Gem::Version
88
+ version: 3.7.0
89
+ - !ruby/object:Gem::Dependency
90
+ name: guard-rspec
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - '='
94
+ - !ruby/object:Gem::Version
95
+ version: 4.7.3
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - '='
101
+ - !ruby/object:Gem::Version
102
+ version: 4.7.3
103
+ description: JPEG and PNG compression for Paperclip gem
104
+ email: emrekutlu@gmail.com, adam@makeascene.com
105
+ executables: []
106
+ extensions: []
107
+ extra_rdoc_files: []
108
+ files:
109
+ - bin/linux/x64/jpegtran
110
+ - bin/linux/x64/optipng
111
+ - bin/linux/x86/jpegtran
112
+ - bin/linux/x86/optipng
113
+ - bin/osx/32bit/jpegtran
114
+ - bin/osx/32bit/optipng
115
+ - bin/osx/64bit/jpegtran
116
+ - bin/osx/64bit/optipng
117
+ - bin/win32/jpegtran.exe
118
+ - bin/win32/libjpeg-62.dll
119
+ - bin/win32/optipng.exe
120
+ - bin/win64/jpegtran.exe
121
+ - bin/win64/libjpeg-62.dll
122
+ - lib/kt-paperclip-compression.rb
123
+ - lib/paperclip-compression.rb
124
+ - lib/paperclip-compression/base.rb
125
+ - lib/paperclip-compression/config.rb
126
+ - lib/paperclip-compression/jpeg.rb
127
+ - lib/paperclip-compression/paperclip/compression.rb
128
+ - lib/paperclip-compression/png.rb
129
+ homepage: http://github.com/adamtao/kt-paperclip-compression
130
+ licenses:
131
+ - MIT
132
+ metadata: {}
133
+ post_install_message:
134
+ rdoc_options: []
135
+ require_paths:
136
+ - lib
137
+ required_ruby_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ required_rubygems_version: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ requirements: []
148
+ rubygems_version: 3.1.2
149
+ signing_key:
150
+ specification_version: 4
151
+ summary: Image compression for Paperclip
152
+ test_files: []