smushkid 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/smushkid.rb +43 -7
  2. metadata +1 -1
@@ -6,16 +6,23 @@ require 'fileutils'
6
6
 
7
7
  @quiet = ARGV.delete('-q')
8
8
  @make_backup = ARGV.delete('-b')
9
+ @exif_tag = ARGV.delete('-e')
9
10
  path = ARGV[0]
10
11
  @quality = ARGV[1]
11
12
 
12
- if File.directory?(path)
13
+ if File.directory?(path)
13
14
  totalsavings = 0
14
15
  File.open("images_processed.json", "a+") { |f| f << "{ \"image_results\" : [ " }
15
16
  @multi = true
16
17
  else
17
18
  end
18
19
 
20
+ def already_done?(file)
21
+ simg = Magick::Image::read(file).first
22
+ puts "checking for EXIF value"
23
+ simg.properties.has_value?("smushkid")
24
+ end
25
+
19
26
  def process_file(file)
20
27
  simg = Magick::Image::read(file).first
21
28
  @source_image = {
@@ -52,16 +59,23 @@ def process_file(file)
52
59
  if @savings < 0
53
60
  @savings = 0
54
61
  puts "no space savings acheived deleting #{@target_file}" unless @quiet
62
+ puts "tagging original file" unless @quiet
63
+ @quoted_filename = "\"#{file}\""
64
+ %x(/usr/local/bin/jhead -cl \"smushkid\" #{@quoted_filename}) if @exif_tag
55
65
  File.delete(@target_file)
56
66
  else
57
67
  if @make_backup
58
- puts "space savings! replacing with: #{file} and making backup or original: #{@backup_file}" unless @quiet
68
+ puts "space savings! replacing with: #{simg} and making backup or original: #{@backup_file}" unless @quiet
69
+ puts "tagging target file" unless @quiet
70
+ %x(/usr/local/bin/jhead -cl \"smushkid\" #{@quoted_filename}) if @exif_tag
59
71
  puts "move #{@target_file} to #{file}" unless @quiet
60
72
  FileUtils.cp file, @backup_file
61
73
  File.rename @target_file, file
62
74
  File.open("images_processed.json", "a+") { |f| f << @image_results.to_json + ','}
63
75
  else
64
76
  puts "space savings! replacing with #{simg}" unless @quiet
77
+ puts "tagging target file" unless @quiet
78
+ %x(/usr/local/bin/jhead -cl \"smushkid\" #{@quoted_filename}) if @exif_tag
65
79
  puts "move #{@target_file} to #{file}" unless @quiet
66
80
  File.rename @target_file, file
67
81
  File.open("images_processed.json", "a+") { |f| f << @image_results.to_json + ','}
@@ -72,15 +86,37 @@ end
72
86
 
73
87
  Find.find(path) do |file|
74
88
  @target_file = File.dirname(file) + "/" + "smaller-" + File.basename(file)
89
+ @quoted_filename = "\"#{@target_file}\""
75
90
  @backup_file = File.dirname(file) + "/" + "original-" + File.basename(file)
91
+ puts "processing #{file}" unless @quiet
76
92
  if @multi
77
- pattern = '**' '/' '*.jpg'
78
- process_file(file) if File.fnmatch(pattern, file, File::FNM_CASEFOLD)
79
- File.open("images_processed_list.txt", "a+") { |f| f << file + "\n"}
93
+ pattern1 = "**" "/" "*.jpg"
94
+ pattern2 = "**" "/" "*.jpeg"
95
+ if File.fnmatch(pattern1, file, File::FNM_CASEFOLD) || File.fnmatch(pattern2, file, File::FNM_CASEFOLD)
96
+ if already_done?(file)
97
+ puts "EXIF signature detected, skipping." unless @quiet
98
+ else
99
+ puts "EXIF data not found attempting compression..." unless @quiet
100
+ process_file(file)
101
+ end
102
+ File.open("images_processed_list.txt", "a+") { |f| f << file + "\n"}
103
+ else
104
+ puts "no file match" unless @quiet
105
+ end
80
106
  else
81
- pattern = '*.jpg'
82
- process_file(file) if File.fnmatch(pattern, file, File::FNM_CASEFOLD)
107
+ pattern1 = '*.jpg'
108
+ pattern2 = '*.jpeg'
109
+ if File.fnmatch(pattern1, file, File::FNM_CASEFOLD) || File.fnmatch(pattern2, file, File::FNM_CASEFOLD)
110
+ if already_done?(file)
111
+ puts "EXIF signature detected, skipping." unless @quiet
112
+ else
113
+ puts "EXIF data not found attempting compression..." unless @quiet
114
+ process_file(file)
115
+ end
83
116
  puts @image_results.to_json
117
+ else
118
+ puts "no file match" unless @quiet
119
+ end
84
120
  end
85
121
  totalsavings = totalsavings.to_i + @savings.to_i
86
122
  puts "Total savings (in bytes): " + totalsavings.to_s unless @quiet
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smushkid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: