Class: Epuber::FromFileExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/epuber/from_file/from_file_executor.rb

Constant Summary collapse

MIMETYPE_PATH =
'mimetype'
ENCRYPTION_PATH =
'META-INF/encryption.xml'
CONTAINER_PATH =
'META-INF/container.xml'

Instance Method Summary collapse

Constructor Details

#initialize(filepath) ⇒ FromFileExecutor

Returns a new instance of FromFileExecutor.

Parameters:

  • filepath (String)

    path to EPUB file



19
20
21
# File 'lib/epuber/from_file/from_file_executor.rb', line 19

def initialize(filepath)
  @filepath = filepath
end

Instance Method Details

#content_opf_pathString

Parameters:

  • zip_file (Zip::File)

Returns:

  • (String)


76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/epuber/from_file/from_file_executor.rb', line 76

def content_opf_path
  entry = @zip_file.find_entry(CONTAINER_PATH)
  UI.error! "This is not valid EPUB file (#{CONTAINER_PATH} file is missing)" if entry.nil?

  doc = Nokogiri::XML(@zip_file.read(entry))
  doc.remove_namespaces!

  rootfile = doc.at_xpath('//rootfile')
  if rootfile.nil?
    UI.error! "This is not valid EPUB file (#{CONTAINER_PATH} file does not contain any <rootfile> element)"
  end

  rootfile['full-path']
end

#export_filesObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/epuber/from_file/from_file_executor.rb', line 108

def export_files
  @opf.manifest_items.each_value do |item|
    # ignore text files which are not in spine
    text_file_extensions = %w[.xhtml .html]
    extension = File.extname(item.href).downcase
    if text_file_extensions.include?(extension) &&
       @opf.spine_items.none? { |spine_item| spine_item.idref == item.id }
      UI.puts "  Skipping #{item.href} (not in spine)"
      next
    end

    # ignore ncx file
    if item.media_type == 'application/x-dtbncx+xml'
      UI.puts "  Skipping #{item.href} (ncx file)"
      next
    end

    full_path = Pathname.new(@opf_path)
                        .dirname
                        .join(item.href)
                        .to_s

    UI.puts "  Exporting #{item.href} (from #{full_path})"

    contents = @zip_file.read(full_path)
    contents = @encryption_handler.process_file(full_path, contents) if @encryption_handler

    FileUtils.mkdir_p(File.dirname(item.href))
    File.write(item.href, contents)
  end
end

#generate_bookspecString

Parameters:

  • opf (Nokogiri::XML::Document)
  • zip_file (Zip::File)

Returns:

  • (String)


95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/epuber/from_file/from_file_executor.rb', line 95

def generate_bookspec
  nav_node, nav_mode = @opf.find_nav
  if nav_node
    nav_path = Pathname.new(@opf_path)
                       .dirname
                       .join(nav_node.href)
                       .to_s
    nav = NavFile.new(@zip_file.read(nav_path), nav_mode)
  end

  BookspecGenerator.new(@opf, nav).generate_bookspec
end

#runObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/epuber/from_file/from_file_executor.rb', line 23

def run
  UI.puts "📖 Loading EPUB file #{@filepath}"

  Zip::File.open(@filepath) do |zip_file|
    @zip_file = zip_file

    validate_mimetype

    @opf_path = content_opf_path
    UI.puts "  Parsing OPF file at #{@opf_path}"
    @opf = OpfFile.new(zip_file.read(@opf_path))

    if zip_file.find_entry(ENCRYPTION_PATH)
      UI.puts '  Parsing encryption.xml file'
      @encryption_handler = EncryptionHandler.new(zip_file.read(ENCRYPTION_PATH), @opf)
    end

    UI.puts '  Generating bookspec file'
    basename = File.basename(@filepath, File.extname(@filepath))
    File.write("#{basename}.bookspec", generate_bookspec)

    export_files

    UI.puts '' # empty line
    UI.puts <<~TEXT.rstrip.ansi.green
      🎉 Project initialized.
      Please review generated #{basename}.bookspec file and start using Epuber.

      For more information about Epuber, please visit https://github.com/epuber-io/epuber/tree/master/docs.
    TEXT
  end
end

#validate_mimetypevoid

This method returns an undefined value.

Parameters:

  • zip_file (Zip::File)


60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/epuber/from_file/from_file_executor.rb', line 60

def validate_mimetype
  entry = @zip_file.find_entry(MIMETYPE_PATH)
  UI.error! "This is not valid EPUB file (#{MIMETYPE_PATH} file is missing)" if entry.nil?

  mimetype = @zip_file.read(entry)

  return if mimetype == 'application/epub+zip'

  UI.error! <<~MSG
    This is not valid EPUB file (#{MIMETYPE_PATH} file does not contain required application/epub+zip, it is #{mimetype} instead)
  MSG
end