Class: CbzTools::CbrConverter
- Inherits:
-
Object
- Object
- CbzTools::CbrConverter
- Defined in:
- lib/cbz_tools/cbr_converter.rb,
sig/cbz_tools.rbs
Overview
Converts a CBR (Comic Book RAR) archive into a CBZ (Comic Book ZIP) archive.
Extraction is handled internally using the unrar command-line tool,
which must be installed on the system. Consumers do not need to
install or configure any extraction backend.
Defined Under Namespace
Classes: ArchiveError, ExtractionError
Instance Attribute Summary collapse
-
#extractor ⇒ Object
readonly
Returns the value of attribute extractor.
Class Method Summary collapse
-
.convert(cbr_path, to:, overwrite: true, work_in: nil) ⇒ String
The destination CBZ path.
Instance Method Summary collapse
- #convert(cbr_path, to:, overwrite: true, work_in: nil) ⇒ Object
- #ensure_extractor! ⇒ void
- #ensure_target_slot!(to, overwrite:) ⇒ void
- #in_workdir(work_in) {|arg0| ... } ⇒ void
-
#initialize ⇒ CbrConverter
constructor
A new instance of CbrConverter.
- #repack(source_dir, target_path) ⇒ void
Constructor Details
#initialize ⇒ CbrConverter
Returns a new instance of CbrConverter.
24 |
# File 'sig/cbz_tools.rbs', line 24
def initialize: (?extractor: untyped) -> void
|
Instance Attribute Details
#extractor ⇒ Object (readonly)
Returns the value of attribute extractor.
14 15 16 |
# File 'sig/cbz_tools.rbs', line 14 def extractor @extractor end |
Class Method Details
.convert(cbr_path, to:, overwrite: true, work_in: nil) ⇒ String
Returns the destination CBZ path.
41 42 43 |
# File 'lib/cbz_tools/cbr_converter.rb', line 41 def self.convert(cbr_path, to:, overwrite: true, work_in: nil) new.convert(cbr_path, to: to, overwrite: overwrite, work_in: work_in) end |
Instance Method Details
#convert(cbr_path, to:, overwrite: true, work_in: nil) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/cbz_tools/cbr_converter.rb', line 45 def convert(cbr_path, to:, overwrite: true, work_in: nil) ensure_target_slot!(to, overwrite: overwrite) File.delete(to) if overwrite && File.exist?(to) in_workdir(work_in) do |temp_dir| extract(cbr_path, temp_dir) repack(temp_dir, to) end to end |
#ensure_extractor! ⇒ void
This method returns an undefined value.
35 |
# File 'sig/cbz_tools.rbs', line 35
def ensure_extractor!: -> void
|
#ensure_target_slot!(to, overwrite:) ⇒ void
This method returns an undefined value.
72 73 74 75 76 |
# File 'lib/cbz_tools/cbr_converter.rb', line 72 def ensure_target_slot!(to, overwrite:) return if overwrite || !File.exist?(to) raise ArchiveError, "Target #{to} already exists (pass overwrite: true to replace)" end |
#in_workdir(work_in) {|arg0| ... } ⇒ void
This method returns an undefined value.
78 79 80 81 82 83 84 85 |
# File 'lib/cbz_tools/cbr_converter.rb', line 78 def in_workdir(work_in, &block) if work_in FileUtils.mkdir_p(work_in) yield work_in else Dir.mktmpdir(&block) end end |
#repack(source_dir, target_path) ⇒ void
This method returns an undefined value.
87 88 89 90 91 92 93 |
# File 'lib/cbz_tools/cbr_converter.rb', line 87 def repack(source_dir, target_path) Zip::File.open(target_path, create: true) do |zipfile| each_file_in(source_dir) { |file| add_to_zip(zipfile, source_dir, file) } end rescue Zip::Error, SystemCallError => e raise ArchiveError, "Failed to write CBZ #{target_path}: #{e.class}: #{e.}" end |