Class: Cbr2cbz::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/cbr2cbz/converter.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Converter

Returns a new instance of Converter.



8
9
10
# File 'lib/cbr2cbz/converter.rb', line 8

def initialize(options = {})
  @options = options
end

Instance Method Details

#convert(entries = []) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cbr2cbz/converter.rb', line 12

def convert(entries = [])
  entries.each do |entry|
    if File.exists?(entry)
      if File.directory?(entry)
        convert_dir(entry)
      else
        convert_file(entry)
      end
    else
      $stderr.puts "Warning -- #{entry} does not exist!"
    end
  end
end

#convert_dir(dirname) ⇒ Object



26
27
28
29
30
31
# File 'lib/cbr2cbz/converter.rb', line 26

def convert_dir(dirname)
  puts "Processing directory #{dirname}" if @options[:verbose]
  Dir.glob(dirname + '/**').each do |filename|
    convert_file(filename)
  end
end

#convert_file(filename) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cbr2cbz/converter.rb', line 33

def convert_file(filename)
  filename = File.expand_path(filename)
  if File.extname(filename).downcase == '.cbr'
    puts "Processing file #{File.basename(filename)}" if @options[:verbose]
    FileUtils.cd(File.dirname(filename)) do

      filename_without_ext = File.basename(filename, '.*')
      new_filename = filename_without_ext + '.cbz'

      if File.exists?(new_filename)
        $stderr.puts "Warning -- #{new_filename} already exists! Skipping."
      else
        # Unrar CBR
        unrar filename, filename_without_ext

        # Create CBZ
        Zip::ZipFile.open(new_filename, 'w') do |zipfile|
          Dir[filename_without_ext + "/*"].each do |file|
            zipfile.add(File.basename(file),file)
          end
        end

        # Clean up
        FileUtils.rm_rf(filename_without_ext)
        FileUtils.rm(filename) unless @options[:keep]

        puts "Created new file #{new_filename}" if @options[:verbose]
      end
    end
  end
end

#unrar(filename, folder) ⇒ Object



65
66
67
# File 'lib/cbr2cbz/converter.rb', line 65

def unrar(filename, folder)
  `unrar e "#{filename}" "#{folder}/"`
end