Permalink
Browse files

Add support for stream output of ePubs (without creating temporary file)

  • Loading branch information...
1 parent 71e20a2 commit e8f81fc267f70db3444ff1baeab0e7ac209eec2b @blaine committed Feb 27, 2012
Showing with 19 additions and 20 deletions.
  1. +15 −17 lib/eeepub/ocf.rb
  2. +4 −3 spec/eeepub/ocf_spec.rb
View
@@ -90,30 +90,28 @@ def container=(arg)
# @param [String] output_path the output file path of ePub
def save(output_path)
output_path = File.expand_path(output_path)
-
- create_epub do
- mimetype = Zip::ZipOutputStream::open(output_path) do |os|
- os.put_next_entry("mimetype", nil, nil, Zip::ZipEntry::STORED, Zlib::NO_COMPRESSION)
- os << "application/epub+zip"
- end
- zipfile = Zip::ZipFile.open(output_path)
- Dir.glob('**/*').each do |path|
- zipfile.add(path, path)
- end
- zipfile.commit
- end
- FileUtils.remove_entry_secure dir
+ File.open(output_path, 'w') { |f| f.write render.read }
end
# Stream OCF
#
# @return [String] streaming output of the zip/epub file.
def render
- create_epub do
- temp_file = Tempfile.new("ocf")
- self.save(temp_file.path)
- return temp_file.read
+ zip = Zip::ZipOutputStream.write_buffer do |os|
+
+ os.put_next_entry("mimetype", nil, nil, Zip::ZipEntry::STORED, Zlib::NO_COMPRESSION)
+ os << "application/epub+zip"
+
+ Dir.glob('**/*').each do |path|
+ next if File.directory?(path)
+ os.put_next_entry(path)
+ os << File.read(path)
+ end
end
+
+ FileUtils.remove_entry_secure dir
+
+ return zip
end
private
View
@@ -42,9 +42,10 @@
end
it 'should stream epub' do
- pending
output = @ocf.render
- output.size.should == 134
- output.is_binary_data?.should be_true
+
+ # epub size won't always be the same
+ output.size.should be_between(171000, 172000)
+ output.should be_a_kind_of StringIO
end
end

0 comments on commit e8f81fc

Please sign in to comment.