Class: Odf::Utils::File

Inherits:
Object
  • Object
show all
Defined in:
lib/odf/utils/file.rb

Class Method Summary collapse

Class Method Details

.fetch_image(image) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/odf/utils/file.rb', line 34

def fetch_image(image)
  io           = open(image.uri)
  content      = io.read
  content_type = MimeMagic.by_magic(io).type
  [content, content_type]
ensure
  io.close
end

.output_stream(document) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/odf/utils/file.rb', line 19

def output_stream(document)
  Zip::OutputStream.write_buffer do |io|
    document.image_set.each do |image|
      io.put_next_entry(image.path)
      io.write(image.content)
    end

    document.components.each do |component_name|
      component = document.send(component_name)
      io.put_next_entry(component.name)
      io.write(component.content)
    end
  end
end

.serialize(name, document) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/odf/utils/file.rb', line 5

def serialize(name, document)
  Zip::File.open("#{name}.#{document.extension}", Zip::File::CREATE) do |zipfile|
    # Resolve images
    document.image_set.each do |image|
      zipfile.get_output_stream(image.path) { |stream| stream.write(image.content) }
    end

    document.components.each do |component_name|
      component = document.send(component_name)
      zipfile.get_output_stream(component.name) { |stream| stream.write(component.content) }
    end
  end
end