Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue where certain files would cause Errno::EPIPE exception to be raised #9

Merged
merged 5 commits into from
Dec 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.