Skip to content

Commit

Permalink
Fix issue where certain files would cause Errno::EPIPE exception to b…
Browse files Browse the repository at this point in the history
…e raised (#9)

* Use Open3 instead of IO.popen for client_read
  • Loading branch information
abrom authored Dec 26, 2019
1 parent 393e007 commit 66807cc
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
8 changes: 8 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'bundler/setup'
require 'henkei'

require 'irb'
IRB.start
8 changes: 3 additions & 5 deletions lib/henkei.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
require 'socket'
require 'stringio'

require 'open3'

# Read text and metadata from files and documents using Apache Tika toolkit
class Henkei # rubocop:disable Metrics/ClassLength
GEM_PATH = File.dirname(File.dirname(__FILE__))
Expand Down Expand Up @@ -224,11 +226,7 @@ def self.java_path
# Internal helper for calling to Tika library directly
#
def self.client_read(type, data)
IO.popen tika_command(type), 'r+' do |io|
io.write data
io.close_write
io.read
end
Open3.capture2(tika_command(type), stdin_data: data).first
end
private_class_method :client_read

Expand Down
27 changes: 27 additions & 0 deletions spec/henkei_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@
)
expect(mimetype.extensions).to include 'docx'
end

context 'when passing in the `pipe-error.png` test file' do
let(:data) { File.read 'spec/samples/pipe-error.png' }

it 'returns an empty result' do
text = Henkei.read :text, data

expect(text).to eq ''
end
end
end

describe '.new' do
Expand Down Expand Up @@ -129,6 +139,23 @@
specify '#metadata reads metadata' do
expect(henkei.metadata['Content-Type']).to eq %w[application/vnd.apple.pages application/vnd.apple.pages]
end

context 'when passing in the `pipe-error.png` test file' do
let(:henkei) { Henkei.new 'spec/samples/pipe-error.png' }

it '#text returns an empty result' do
expect(henkei.text).to eq ''
end

it '#html returns an empty body' do
expect(henkei.html).to include '<body/>'
expect(henkei.html).to include '<meta name="tiff:ImageWidth" content="792"/>'
end

it '#mimetype returns an empty result' do
expect(henkei.mimetype.content_type).to eq 'image/png'
end
end
end

context 'initialized with a given URI' do
Expand Down
Binary file added spec/samples/pipe-error.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 66807cc

Please sign in to comment.