Class: ImageCrush::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/image_crush/base.rb

Instance Method Summary collapse

Instance Method Details

#copy_to_tempdir(path) ⇒ Object



41
42
43
44
45
# File 'lib/image_crush/base.rb', line 41

def copy_to_tempdir(path)
  wdir = tmpdir
  FileUtils.cp_r path, wdir
  File.join(wdir, File.basename(path))
end

#crush(path) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/image_crush/base.rb', line 5

def crush(path)
  raise ImageCrush::InputFileNotFound unless File.exist?(path)

  if File.file?(path)
    crush_file(path)
  elsif File.directory?(path)
    crush_dir(path)
  else
    raise "I don't know what to do with #{path}"
  end
end

#crush_dir(path) ⇒ Object



25
26
27
28
29
30
# File 'lib/image_crush/base.rb', line 25

def crush_dir(path)
  Dir.foreach(path) do |entry|
    next if %w{. ..}.include?(entry)
    crush(File.join(path, entry))
  end
end

#crush_file(path) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/image_crush/base.rb', line 17

def crush_file(path)
  processor = select_processor(path) or return nil
  source_path = copy_to_tempdir(path)
  crushed_path = source_path + '.crushed'
  processor.crush(source_path, crushed_path)
  FileUtils.cp(crushed_path, path) if File.exist?(crushed_path)
end

#select_processor(path) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/image_crush/base.rb', line 32

def select_processor(path)
  case path
  when /\.png$/i
    Pngcrush
  when /\.jpe?g$/i
    Jpegtran
  end
end

#tmpdirObject



47
48
49
50
51
# File 'lib/image_crush/base.rb', line 47

def tmpdir
  ret = File.join(Dir.tmpdir, 'image_crush')
  FileUtils.mkdir_p(ret)
  ret
end