Module: EPUB::Maker

Defined in:
lib/epub/maker.rb,
lib/epub/maker/task.rb,
lib/epub/maker/version.rb

Defined Under Namespace

Classes: Error, Task

Constant Summary collapse

VERSION =
"0.0.4"

Class Method Summary collapse

Class Method Details

.make(path) ⇒ Object

TODO:

Add option whether mv blocks or not when file locked already

TODO:

Timeout when file shared-locked long time

Parameters:



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
55
56
57
58
59
60
61
62
# File 'lib/epub/maker.rb', line 26

def make(path)
  path = Pathname(path) unless path.kind_of? Pathname
  book = EPUB::Book.new
  dir = Pathname.mktmpdir 'epub-maker'
  temp_path = dir/path.basename
  mimetype = dir/'mimetype'
  mimetype.write EPUB::MediaType::EPUB
  Archive::Zip.open temp_path.to_path, :w do |archive|
    file = Archive::Zip::Entry.from_file(mimetype.to_path, compression_codec: Archive::Zip::Codec::Store)
    archive.add_entry file
  end

  book.epub_file = temp_path.to_path
  yield book if block_given?
  book.save

  path.open 'wb' do |file|
    raise Error, "File locked by other process: #{path}" unless file.flock File::LOCK_SH|File::LOCK_NB
    ($VERBOSE ? ::FileUtils::Verbose : ::FileUtils).move temp_path.to_path, path.to_path
  end
  dir.remove_entry_secure
  book.epub_file = path.to_path
  book

  # validate
  # build_xml
  # archive
rescue => error
  backtrace = error.backtrace
  error = error.exception([
            error.message,
            "[#{self}]Working directory remained at: #{dir}"
          ].join($RS))
  backtrace.unshift("#{__FILE__}:#{__LINE__}:in `rescue in #{__method__}'")
  error.set_backtrace backtrace
  raise error
end