tiny_png_checker 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 304e6a5733b8d3b668fa5aaabbcb82a9715c4385b6e0bac579d83d7cfa93bec3
4
- data.tar.gz: 1312e4a35a32bd0e618b5d5dd5ad42c3f0dabccf16e2ada2a7d25829aabd70fa
3
+ metadata.gz: ca9a7b7016aed1349fd019297ef691347fb165279ad43a29e1bf723d353cc828
4
+ data.tar.gz: e354da683db83a618d8c063717afda45290df44458112a000372f8f77fa49da9
5
5
  SHA512:
6
- metadata.gz: a8a9f08853da21ec98e0c7065598fb74e7cfa3f8cbc3dc347f1ca345ad21fdae758d61450972554ce75be0b9a71d0e18b03b2cb45b0d0d23448dfe16d75f4124
7
- data.tar.gz: e533a8ed0ea8c2bc20b0c0c6e2a8d7a8818bcd1127fec8e9cb693b6812ef732f0a4d5142e479738b185bed21a4e12d29b4f834b1547257bbeca5ade7c428c44b
6
+ metadata.gz: cb8371052ac769f1ef052c2e383937e55f70a4128a73ee9cfaa13cca565fcaf2ac7839260a50b7cbde71a7a86e011c503e6f7f1e982ca1b9fcb1bf90a35f9084
7
+ data.tar.gz: f96ce51770273358a8d4b4c0c387b27853f6edfadaa72b56b4ca831933aa4f6a195699268494fa09e0e6941b39d3a25db315e162904c77381f903de0c01fed64
@@ -1,15 +1,17 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- tiny_png_checker (0.1.1)
4
+ tiny_png_checker (0.1.2)
5
5
  chunky_png (~> 1.3, >= 1.3.10)
6
6
  commander (~> 4.4, >= 4.4.6)
7
7
  figaro (~> 1.1, >= 1.1.1)
8
+ parallel (~> 1.12, >= 1.12.1)
8
9
  tinify (~> 1.5)
9
10
 
10
11
  GEM
11
12
  remote: https://rubygems.org/
12
13
  specs:
14
+ benchmark_methods (0.7)
13
15
  chunky_png (1.3.10)
14
16
  commander (4.4.6)
15
17
  highline (~> 1.7.2)
@@ -18,6 +20,7 @@ GEM
18
20
  highline (1.7.10)
19
21
  httpclient (2.8.3)
20
22
  minitest (5.11.3)
23
+ parallel (1.12.1)
21
24
  rake (10.5.0)
22
25
  thor (0.20.0)
23
26
  tinify (1.5.0)
@@ -27,6 +30,7 @@ PLATFORMS
27
30
  ruby
28
31
 
29
32
  DEPENDENCIES
33
+ benchmark_methods (~> 0.7)
30
34
  bundler (~> 1.16)
31
35
  minitest (~> 5.0)
32
36
  rake (~> 10.0)
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # TinyPngChecker
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/tiny_png_checker`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ Tiny png checker works over [tinypng.com](http://www.tinypng.com) compressor, since TinyPng converter is not deterministic and does not provide a way to know if a file was already compressed, this gem provides some tools to workaround those issues
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,7 +20,28 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ Three commands are available. All of them accept a --folders param as a comma separated string list being the folder paths to process:
24
+
25
+ check Checks recursively into specified folders for PNG files marked by TinyPngProcessor
26
+ compress Compress recursively PNGs inside specified folders using TinyPNG and mark files with TinyPngProcessor flag
27
+ help Display global or [command] help documentation
28
+
29
+ Example
30
+
31
+ tiny_png_checker check --files ~/Documents,~/Images
32
+
33
+ There are some global options:
34
+
35
+ -h, --help
36
+ Display help documentation
37
+
38
+ -v, --version
39
+ Display version information
40
+
41
+ -t, --trace
42
+ Display backtrace when an error occurs
43
+
44
+
26
45
 
27
46
  ## Development
28
47
 
@@ -14,7 +14,19 @@ command :check do |c|
14
14
  c.action do |args, options|
15
15
  checker = TinyPngChecker::Checker.new
16
16
  say "Running under folders: #{options.folders}"
17
- checker.check_tiny_png_on_folders(options.folders)
17
+ checker.process_pngs_on_folders(options.folders)
18
+ say "Done!"
19
+ end
20
+ end
21
+
22
+ command :mark do |c|
23
+ c.syntax = 'tiny_png_checker mark [folders]'
24
+ c.description = 'Mark PNGs files with TinyPngProcessor flag looking recursively in folders provided'
25
+ c.option '--folders FOLDERS' , Array, 'List of folders to check for PNGs to mark'
26
+ c.action do |args, options|
27
+ say "Running under folders: #{options.folders}"
28
+ marking = TinyPngChecker::Marker.new
29
+ marking.process_pngs_on_folders(options.folders)
18
30
  say "Done!"
19
31
  end
20
32
  end
@@ -26,7 +38,7 @@ command :compress do |c|
26
38
  c.action do |args, options|
27
39
  say "Running under folders: #{options.folders}"
28
40
  compressor = TinyPngChecker::Compressor.new
29
- compressor.compress_pngs_on_folders(options.folders)
41
+ compressor.process_pngs_on_folders(options.folders)
30
42
  say "Done!"
31
43
  end
32
44
  end
@@ -1,3 +1,3 @@
1
1
  # Add configuration values here, as shown below.
2
2
 
3
- tinify_api_key: "e3axBwb1wgc7egz6q5IX2wOIxrW9HP6H"
3
+ tinify_api_key: "QeOe28LwtnR5NXKYDFM0gBP9Rkn6IX3r"
@@ -1,6 +1,7 @@
1
1
  require 'tiny_png_checker/version'
2
2
  require 'tiny_png_checker/checker'
3
3
  require 'tiny_png_checker/compressor'
4
+ require 'tiny_png_checker/marker'
4
5
 
5
6
  require 'figaro'
6
7
  Figaro.application = Figaro::Application.new(environment: 'production', path: File.expand_path('../config/application.yml', __FILE__))
@@ -0,0 +1,27 @@
1
+
2
+ module TinyPngChecker
3
+ class BaseProcessor
4
+
5
+ attr_accessor :errors, :processed
6
+
7
+ def run_process
8
+
9
+ self.errors ||= []
10
+ self.processed ||= []
11
+
12
+ begin
13
+ yield
14
+ rescue => e
15
+ $stderr.puts("Unrecoverable error #{e}")
16
+ end
17
+
18
+ $stdout.puts("Compressed files:")
19
+ $stdout.puts(processed)
20
+
21
+ if errors.any?
22
+ $stderr.puts(errors)
23
+ abort
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,20 +1,24 @@
1
1
  require 'tiny_png_checker/utils/image_files_retriever'
2
2
  require 'tiny_png_checker/utils/tiny_png'
3
+ require 'tiny_png_checker/base_processor'
3
4
 
4
5
  module TinyPngChecker
5
- class Checker
6
+ class Checker < BaseProcessor
6
7
 
7
- def check_tiny_png_on_folders(folders = [])
8
- png_files = Utils::ImageFilesRetriever.get_png_files_from_folders(folders)
8
+ def process_pngs_on_folders(folders = [])
9
+ run_process do
10
+ png_files = Utils::ImageFilesRetriever.get_png_files_from_folders(folders)
9
11
 
10
- not_processed_files = Utils::TinyPng.not_processed_files(png_files)
12
+ not_processed_files = Utils::TinyPng.not_processed_files(png_files)
11
13
 
12
- if not_processed_files.any?
13
- $stderr.puts("Files not processed by TinyPNG:")
14
- $stderr.puts(not_processed_files)
15
- abort
16
- else
17
- $stdout.puts("No files to process")
14
+ if not_processed_files.any?
15
+ $stderr.puts("Files not processed by TinyPNG:")
16
+ $stderr.puts(not_processed_files)
17
+ abort
18
+ else
19
+ $stdout.puts("No files to process")
20
+ exit
21
+ end
18
22
  end
19
23
  end
20
24
 
@@ -1,61 +1,40 @@
1
1
  require 'tinify'
2
2
  require 'figaro'
3
3
  require 'tiny_png_checker/utils/tiny_png'
4
- require 'tiny_png_checker/utils/image_files_retriever'
4
+ require 'tiny_png_checker/unmarked_files_processor'
5
5
 
6
6
  module TinyPngChecker
7
- class Compressor
7
+ class Compressor < UnmarkedFilesProcessor
8
8
 
9
- def initialize
10
- @errors = []
11
- @processed = []
12
- end
13
-
14
- def compress_pngs_on_folders(folders = [])
9
+ def setup
15
10
  begin
16
11
  Tinify.key = Figaro.env.tinify_api_key
17
12
  Tinify.validate!
18
13
  rescue Tinify::Error => e
19
14
  abort("Error: Tinify invalid api key #{e}")
20
15
  end
21
-
22
- @errors.clear
23
- @processed.clear
24
-
25
- png_files = Utils::ImageFilesRetriever.get_png_files_from_folders(folders)
26
-
27
- not_processed_files = Utils::TinyPng.not_processed_files(png_files)
28
-
29
- not_processed_files.each { |not_processed_file| process_file(not_processed_file) }
30
-
31
- $stdout.puts("Compressed files:")
32
- $stdout.puts(@processed)
33
-
34
- if @errors.any?
35
- $stderr.puts(@errors)
36
- abort
37
- end
38
16
  end
39
17
 
40
18
  def process_file(file)
41
19
  begin
42
20
  Tinify.from_file(file).to_file(file)
43
21
  Utils::TinyPng.mark_files(file)
44
- @processed << file
22
+ processed << file
45
23
  rescue Tinify::AccountError => e
46
- @errors << "The error message is: #{e.message} with file #{file}, monthly conversion limit #{Tinify.compression_count}"
24
+ errors << "The error message is: #{e.message} with file #{file}, monthly conversion limit #{Tinify.compression_count}"
25
+ raise e
47
26
  # Verify your API key and account limit.
48
27
  rescue Tinify::ClientError => e
49
- @errors << "Check your source image and request options, the error message is: #{e.message} with file #{file}"
28
+ errors << "Check your source image and request options, the error message is: #{e.message} with file #{file}"
50
29
  # Check your source image and request options.
51
30
  rescue Tinify::ServerError => e
52
- @errors << "Temporary issue with the Tinify API, the error message is: #{e.message} with file #{file}"
31
+ errors << "Temporary issue with the Tinify API, the error message is: #{e.message} with file #{file}"
53
32
  # Temporary issue with the Tinify API.
54
33
  rescue Tinify::ConnectionError => e
55
- @errors << "A network connection error occurred, the error message is: #{e.message} with file #{file}"
34
+ errors << "A network connection error occurred, the error message is: #{e.message} with file #{file}"
56
35
  # A network connection error occurred.
57
36
  rescue => e
58
- @errors << "An error occurred, the error message is: #{e.message} with file #{file}"
37
+ errors << "An error occurred, the error message is: #{e.message} with file #{file}"
59
38
  # Something else went wrong, unrelated to the Tinify API.
60
39
  end
61
40
  end
@@ -0,0 +1,17 @@
1
+ require 'tiny_png_checker/utils/tiny_png'
2
+ require 'tiny_png_checker/unmarked_files_processor'
3
+
4
+ module TinyPngChecker
5
+ class Marker < UnmarkedFilesProcessor
6
+
7
+ def process_file(file)
8
+ begin
9
+ Utils::TinyPng.mark_files(file)
10
+ processed << file
11
+ rescue => e
12
+ errors << "An error occurred, the error message is: #{e.message} with file #{file}"
13
+ end
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,27 @@
1
+ require 'tiny_png_checker/base_processor'
2
+ require 'tiny_png_checker/utils/tiny_png'
3
+ require 'tiny_png_checker/utils/image_files_retriever'
4
+
5
+ module TinyPngChecker
6
+ class UnmarkedFilesProcessor < BaseProcessor
7
+
8
+ def process_pngs_on_folders(folders = [])
9
+ run_process do
10
+ setup
11
+
12
+ png_files = Utils::ImageFilesRetriever.get_png_files_from_folders(folders)
13
+
14
+ not_processed_files = Utils::TinyPng.not_processed_files(png_files)
15
+
16
+ not_processed_files.each { |not_processed_file| process_file(not_processed_file) }
17
+ end
18
+ end
19
+
20
+ def process_file(_)
21
+ end
22
+
23
+ def setup
24
+ end
25
+
26
+ end
27
+ end
@@ -1,4 +1,5 @@
1
1
  require 'chunky_png'
2
+ require 'parallel'
2
3
 
3
4
  module Utils
4
5
  module TinyPng
@@ -8,13 +9,28 @@ module Utils
8
9
 
9
10
  def self.not_processed_files(png_files = [])
10
11
 
11
- png_files.select do |file|
12
- image = ChunkyPNG::Image.from_file(file)
13
- image.metadata[PNG_PROCESSED_FLAG].nil?
14
- end
12
+ Parallel.map(png_files) do |file|
13
+ begin
14
+ # image = ChunkyPNG::Image.from_file(file)
15
+ # image.metadata[PNG_PROCESSED_FLAG].nil? ? file : nil
16
+
17
+ if !look_for_metadata_flag_from_io(File.open(file)) { |chunk| is_tinypng_metadata_chunk(chunk) }
18
+ file
19
+ end
20
+ rescue => e
21
+ $stdout.puts("WARNING: File is not a PNG: #{file}, error: #{e}")
22
+ nil
23
+ end
24
+ end.compact
15
25
 
16
26
  end
17
27
 
28
+ def self.is_tinypng_metadata_chunk(chunk)
29
+ if chunk.respond_to?(:keyword) && chunk.respond_to?(:value)
30
+ chunk.keyword == PNG_PROCESSED_FLAG && chunk.value.nil?
31
+ end
32
+ end
33
+
18
34
  def self.mark_files(*png_files)
19
35
 
20
36
  png_files.compact!
@@ -27,5 +43,24 @@ module Utils
27
43
 
28
44
  end
29
45
 
46
+ def self.look_for_metadata_flag_from_io(io)
47
+ ChunkyPNG::Datastream.verify_signature!(io)
48
+
49
+ end_chunk = false
50
+ found = false
51
+
52
+ while !end_chunk && !found
53
+ chunk = ChunkyPNG::Chunk.read(io)
54
+ case chunk
55
+ when ChunkyPNG::Chunk::End
56
+ end_chunk = true
57
+ else
58
+ found = !yield(chunk).nil?
59
+ end
60
+ end
61
+
62
+ found
63
+ end
64
+
30
65
  end
31
66
  end
@@ -1,3 +1,3 @@
1
1
  module TinyPngChecker
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -38,7 +38,9 @@ CI know that the file has been compressed, so a check can be done per build}
38
38
  spec.add_dependency "tinify", "~> 1.5"
39
39
  spec.add_dependency "figaro", "~> 1.1", ">= 1.1.1"
40
40
  spec.add_dependency "commander", "~> 4.4", ">= 4.4.6"
41
+ spec.add_dependency "parallel", "~> 1.12", ">= 1.12.1"
41
42
 
43
+ spec.add_development_dependency "benchmark_methods", "~> 0.7"
42
44
  spec.add_development_dependency "bundler", "~> 1.16"
43
45
  spec.add_development_dependency "rake", "~> 10.0"
44
46
  spec.add_development_dependency "minitest", "~> 5.0"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tiny_png_checker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Damian Arrillaga
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-09-06 00:00:00.000000000 Z
11
+ date: 2018-09-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: chunky_png
@@ -84,6 +84,40 @@ dependencies:
84
84
  - - ">="
85
85
  - !ruby/object:Gem::Version
86
86
  version: 4.4.6
87
+ - !ruby/object:Gem::Dependency
88
+ name: parallel
89
+ requirement: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - "~>"
92
+ - !ruby/object:Gem::Version
93
+ version: '1.12'
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 1.12.1
97
+ type: :runtime
98
+ prerelease: false
99
+ version_requirements: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.12'
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: 1.12.1
107
+ - !ruby/object:Gem::Dependency
108
+ name: benchmark_methods
109
+ requirement: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - "~>"
112
+ - !ruby/object:Gem::Version
113
+ version: '0.7'
114
+ type: :development
115
+ prerelease: false
116
+ version_requirements: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - "~>"
119
+ - !ruby/object:Gem::Version
120
+ version: '0.7'
87
121
  - !ruby/object:Gem::Dependency
88
122
  name: bundler
89
123
  requirement: !ruby/object:Gem::Requirement
@@ -150,8 +184,11 @@ files:
150
184
  - exe/tiny_png_checker
151
185
  - lib/config/application.yml
152
186
  - lib/tiny_png_checker.rb
187
+ - lib/tiny_png_checker/base_processor.rb
153
188
  - lib/tiny_png_checker/checker.rb
154
189
  - lib/tiny_png_checker/compressor.rb
190
+ - lib/tiny_png_checker/marker.rb
191
+ - lib/tiny_png_checker/unmarked_files_processor.rb
155
192
  - lib/tiny_png_checker/utils/image_files_retriever.rb
156
193
  - lib/tiny_png_checker/utils/tiny_png.rb
157
194
  - lib/tiny_png_checker/version.rb