paperclip-compression 0.3.12 → 0.3.13

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,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MWFjMDJkYzczMGVjOWE1OWFhZDgwMzQyMWU4ZTEyZGNkMDkwMmNjOQ==
4
+ MGQ1MTkyNjBkZGY5NWY4ZjU3NjBiMmExM2MxY2Y3ZmMyZjJiNjc1Yg==
5
5
  data.tar.gz: !binary |-
6
- YTJkNzAwZTgzM2I5M2JjZWRiN2VhNjBiZDY4ZjgwOWU4Mjk3ZTY2Mg==
6
+ ODhmNTRmMjk0MmUzMGYwOTJjMTAyM2YxN2M4YzMyMzIyMTdlNzdkMQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- MjUzMjllZjVlNTBiNzk2ZGM2ZTk2YjQ4ZjEzYjRhOTM4ZGY5ZTQwODQxNWZi
10
- MTc0MWJjNDJhNTI1NDYyYjI3ZWYxMjk2MjNkODVmYTZhMjRlMDAzY2M4OGYx
11
- MWYwNDVhNmExZTUyNDc5OTFhNDJjNDAyNzg1NmE0MWNlOWRmNmU=
9
+ NDI5MTkzZjZjNzlmODkzOTk4OTA3YjNlYmRiZDIzZDlmNmU0ZDljODVmYmY4
10
+ ZDcxNzQ2ODNjNmYxY2E3NjUzZGZiZDk0MTg5MGE3ZGYyZTFlN2Y1M2Y3Y2I4
11
+ OWE4Y2RmNjlmMTMwNjM0Y2QwNzVmN2IwZTU3MTFhYWVmMzhiNGU=
12
12
  data.tar.gz: !binary |-
13
- M2Q1ODg3MWJkYzIzMDYzYjUyNzI1MWI1ZGZkZDBhMmFiZjEyZTdlY2U5YTAw
14
- MTk1NTA3NzMxM2M0NDQzMmViNjY0NDA0YTM5YWFkYWRiODdmYTBlYTE2MmYx
15
- MWExNTNlM2U0ZWEyMzRmNzk5Yzk3MDlkYTcwMTc5OWUwODY2YzM=
13
+ NjQ4Zjk4MzdmODhlZTg4NzAyZjdmNTE0MzFkZDIzZWJlYjc1NDQ4ZWQzYjJl
14
+ MDQ0NDM4NjQ4MjQ3ZjEzMTc4YjE2ZDVkYzBmZWUyN2IyNzJlOWVkMGU1NzM0
15
+ OTUyMjg5YzM2NDA4YTVlMmVkNjU0YmEyN2JmMmJiNzhhODQxZDU=
@@ -1,20 +1,33 @@
1
1
  module PaperclipCompression
2
2
  class Base
3
3
 
4
- def initialize(file, options = {})
4
+ def initialize(file, first_processor, options = {})
5
5
  @file = file
6
6
  @options = options
7
7
  @whiny = options.has_key?(:whiny) ? options[:whiny] : true
8
8
  current_extension = File.extname(file.path)
9
9
  @basename = File.basename(file.path, current_extension)
10
+ @dst = Paperclip::TempfileFactory.new.generate("#{@basename}.png")
11
+ @dst_path = File.expand_path(@dst.path)
12
+ @src_path = File.expand_path(@file.path)
13
+ @first_processor = first_processor
10
14
  end
11
15
 
12
- def self.make(file, options = {})
13
- new(file, options).make
16
+ def self.make(file, first_processor, options = {})
17
+ new(file, first_processor, options).make
14
18
  end
15
19
 
16
20
  protected
17
21
 
22
+ def process_file?
23
+ @cli_opts
24
+ end
25
+
26
+ def unprocessed_tempfile
27
+ copy_to_tempfile
28
+ first_processor? ? @dst : @file
29
+ end
30
+
18
31
  def init_cli_opts(type, default_opts)
19
32
  # use default options in the papeclip config if exists, otherwise use gem defaults.
20
33
  default_opts = init_default_opts(Paperclip::Attachment.default_options, type, default_opts)
@@ -25,7 +38,7 @@ module PaperclipCompression
25
38
  def command_path(command)
26
39
  folder = if OS.osx?
27
40
  'osx'
28
- elsif OS.linux? || RUBY_PLATFORM =~ /freebsd/
41
+ elsif OS.linux?
29
42
  File.join('linux', OS.bits.eql?(64) ? 'x64' : 'x86')
30
43
  elsif OS.windows?
31
44
  OS.bits.eql?(64) ? 'win64' : 'win32'
@@ -36,6 +49,10 @@ module PaperclipCompression
36
49
 
37
50
  private
38
51
 
52
+ def first_processor?
53
+ @first_processor
54
+ end
55
+
39
56
  def init_default_opts(opts, type, default_opts)
40
57
  if opts && (compression_opts = opts[:compression])
41
58
  if compression_opts.has_key?(type)
@@ -52,5 +69,8 @@ module PaperclipCompression
52
69
  end
53
70
  end
54
71
 
72
+ def copy_to_tempfile
73
+ FileUtils.cp(@src_path, @dst_path)
74
+ end
55
75
  end
56
76
  end
@@ -3,31 +3,16 @@ module PaperclipCompression
3
3
 
4
4
  JPEGTRAN_DEFAULT_OPTS = '-copy none -optimize -perfect'
5
5
 
6
- def initialize(file, options = {})
7
- super(file, options)
8
-
9
- @dst = Tempfile.new(@basename)
10
- @dst.binmode
11
-
12
- @src_path = File.expand_path(@file.path)
13
- @dst_path = File.expand_path(@dst.path)
14
-
6
+ def initialize(file, first_processor, options = {})
7
+ super(file, first_processor, options)
15
8
  @cli_opts = init_cli_opts(:jpeg, default_opts)
16
9
  end
17
10
 
18
11
  def make
19
12
  begin
20
- if @cli_opts
21
- # close dst file, so jpegtran can write it
22
- @dst.close
23
- Paperclip.run(command_path('jpegtran'), "#{@cli_opts} :src_path > :dst_path", src_path: @src_path, dst_path: @dst_path)
24
- @dst.open
25
- @dst
26
- else
27
- @file
28
- end
13
+ process_file? ? process_file : unprocessed_tempfile
29
14
  rescue Cocaine::ExitStatusError => e
30
- raise Paperclip::Error, "JPEGTRAN : There was an error processing the thumbnail for #{@basename}" if @whiny
15
+ raise Paperclip::Error, "JPEGTRAN : There was an error processing #{@basename}" if @whiny
31
16
  rescue Cocaine::CommandNotFoundError => e
32
17
  raise Paperclip::Errors::CommandNotFoundError.new("Could not run 'jpegtran'. Please install jpegtran.")
33
18
  end
@@ -39,5 +24,12 @@ module PaperclipCompression
39
24
  JPEGTRAN_DEFAULT_OPTS
40
25
  end
41
26
 
27
+ def process_file
28
+ # close dst file, so jpegtran can write it
29
+ @dst.close
30
+ Paperclip.run(command_path('jpegtran'), "#{@cli_opts} :src_path > :dst_path", src_path: @src_path, dst_path: @dst_path)
31
+ @dst.open
32
+ @dst
33
+ end
42
34
  end
43
35
  end
@@ -13,18 +13,21 @@ module Paperclip
13
13
 
14
14
  private
15
15
 
16
- def make_jpeg
17
- PaperclipCompression::Jpeg.make(@file, @options)
16
+ def content_type
17
+ first_processor? ? @file.content_type : Paperclip::ContentTypeDetector.new(@file.path).detect
18
18
  end
19
19
 
20
- def make_png
21
- PaperclipCompression::Png.make(@file, @options)
20
+ def first_processor?
21
+ @first_processor ||= @file.is_a?(Paperclip::AbstractAdapter)
22
22
  end
23
23
 
24
- def content_type
25
- @file.is_a?(Paperclip::AbstractAdapter) ? @file.content_type : Paperclip::ContentTypeDetector.new(@file.path).detect
24
+ def make_jpeg
25
+ PaperclipCompression::Jpeg.make(@file, first_processor?, @options)
26
26
  end
27
27
 
28
- end
28
+ def make_png
29
+ PaperclipCompression::Png.make(@file, first_processor?, @options)
30
+ end
29
31
 
32
+ end
30
33
  end
@@ -3,18 +3,16 @@ module PaperclipCompression
3
3
 
4
4
  OPTIPNG_DEFAULT_OPTS = '-o 5 -quiet'
5
5
 
6
- def initialize(file, options = {})
7
- super(file, options)
8
- @src_path = File.expand_path(@file.path)
6
+ def initialize(file, first_processor, options = {})
7
+ super(file, first_processor, options)
9
8
  @cli_opts = init_cli_opts(:png, default_opts)
10
9
  end
11
10
 
12
11
  def make
13
12
  begin
14
- Paperclip.run(command_path('optipng'), "#{@cli_opts} :src_path", src_path: @src_path) if @cli_opts
15
- @file
13
+ process_file? ? process_file : unprocessed_tempfile
16
14
  rescue Cocaine::ExitStatusError => e
17
- raise Paperclip::Error, "OPTIPNG : There was an error processing the thumbnail for #{@basename}" if @whiny
15
+ raise Paperclip::Error, "OPTIPNG : There was an error processing #{@basename}" if @whiny
18
16
  rescue Cocaine::CommandNotFoundError => e
19
17
  raise Paperclip::Errors::CommandNotFoundError.new("Could not run 'optipng'. Please install optipng.")
20
18
  end
@@ -26,5 +24,10 @@ module PaperclipCompression
26
24
  OPTIPNG_DEFAULT_OPTS
27
25
  end
28
26
 
27
+ def process_file
28
+ Paperclip.run(command_path('optipng'), "#{@cli_opts} -clobber :src_path -out :dst_path", src_path: @src_path, dst_path: @dst_path)
29
+ @dst
30
+ end
31
+
29
32
  end
30
33
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paperclip-compression
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.12
4
+ version: 0.3.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - İ. Emre Kutlu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-25 00:00:00.000000000 Z
11
+ date: 2015-10-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: paperclip