Kouhei Sutou 2019-03-04 06:32:37 +0900 (Mon, 04 Mar 2019) Revision: 498058015d5fe44adb1dfeae1178b895b1866cea https://github.com/ranguba/chupa-text/commit/498058015d5fe44adb1dfeae1178b895b1866cea Message: virtual-content: support inline content Modified files: lib/chupa-text/decomposers/tar.rb lib/chupa-text/decomposers/zip.rb lib/chupa-text/virtual-content.rb Modified: lib/chupa-text/decomposers/tar.rb (+3 -12) =================================================================== --- lib/chupa-text/decomposers/tar.rb 2019-03-04 06:15:11 +0900 (e0a8ea4) +++ lib/chupa-text/decomposers/tar.rb 2019-03-04 06:32:37 +0900 (6f37913) @@ -40,18 +40,9 @@ module ChupaText path_converter = PathConverter.new(entry.full_name, uri_escape: true) entry_uri.path = "#{base_path}/#{path_converter.convert}" - size = entry.header.size - if size < (32 * 1024) - entry_data = Data.new(source_data: data) - entry_data.uri = entry_uri - body = entry.read || "" - entry_data.body = body - entry_data.size = body.bytesize - else - entry_data = VirtualFileData.new(entry_uri, - entry, - :source_data => data) - end + entry_data = VirtualFileData.new(entry_uri, + entry, + :source_data => data) yield(entry_data) end end Modified: lib/chupa-text/decomposers/zip.rb (+3 -11) =================================================================== --- lib/chupa-text/decomposers/zip.rb 2019-03-04 06:15:11 +0900 (8f75115) +++ lib/chupa-text/decomposers/zip.rb 2019-03-04 06:32:37 +0900 (d708736) @@ -50,17 +50,9 @@ module ChupaText uri_escape: true) entry_uri.path = "#{base_path}/#{path_converter.convert}" size = entry.raw_data.window_size - if size < (8 * 1024) - entry_data = Data.new(source_data: data) - entry_data.uri = entry_uri - body = entry.file_data.read - entry_data.body = body - entry_data.size = body.bytesize - else - entry_data = VirtualFileData.new(entry_uri, - entry.file_data, - source_data: data) - end + entry_data = VirtualFileData.new(entry_uri, + entry.file_data, + source_data: data) yield(entry_data) end end Modified: lib/chupa-text/virtual-content.rb (+37 -10) =================================================================== --- lib/chupa-text/virtual-content.rb 2019-03-04 06:15:11 +0900 (1f76cce) +++ lib/chupa-text/virtual-content.rb 2019-03-04 06:32:37 +0900 (50ec931) @@ -20,12 +20,10 @@ require "tempfile" module ChupaText class VirtualContent - KILO_BYTE = 1024 - BUFFER_SIZE = 64 * KILO_BYTE + INLINE_MAX_SIZE = 64 * 1024 attr_reader :size def initialize(input, original_path=nil) - @file = nil if original_path.is_a?(String) if original_path.empty? original_path = nil @@ -34,28 +32,56 @@ module ChupaText end end @original_path = original_path - setup_file do |file| - @size = IO.copy_stream(input, file) + body = input.read(INLINE_MAX_SIZE) || "" + if body.bytesize < INLINE_MAX_SIZE + @body = body + @size =****@body***** + @file = nil + @path = nil + else + @body = nil + setup_file do |file| + file.write(body) + @size = body.bytesize + @size += IO.copy_stream(input, file) + end end end def open(&block) - File.open(path, "rb", &block) + if @body + super + else + File.open(path, "rb", &block) + end end def body - open do |file| - file.read + if @body + @body + else + open do |file| + file.read + end end end def peek_body(size) - open do |file| - file.read(size) + if @body + super + else + open do |file| + file.read(size) + end end end def path + if****@path*****? + setup_file do |file| + file.write(@body) + end + end @path end @@ -77,6 +103,7 @@ module ChupaText def setup_file basename = compute_tempfile_basename @file = Tempfile.new(basename) + @file.binmode @path =****@file***** yield(@file) @file.close -------------- next part -------------- An HTML attachment was scrubbed... URL: <https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20190304/84cc3d32/attachment-0001.html>