diff --git a/CHANGELOG.md b/CHANGELOG.md index 38b2c03..4c9ed4f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,10 @@ # Changes +## v3.1.3 + +#### Added + +* Temporary hocr file is deleted after the file is processed. + ## v3.1.2 #### Added diff --git a/lib/rtesseract/base.rb b/lib/rtesseract/base.rb index f5748f5..3fb9b43 100644 --- a/lib/rtesseract/base.rb +++ b/lib/rtesseract/base.rb @@ -9,5 +9,14 @@ module Base def temp_file_path Pathname.new(Dir.tmpdir).join("rtesseract_#{SecureRandom.uuid}").to_s end + + def remove_tmp_file(output_path) + Dir["#{Dir.tmpdir}/*"].each do |filename| + if filename.include?(output_path) + File.delete(filename) + break + end + end + end end end diff --git a/lib/rtesseract/box.rb b/lib/rtesseract/box.rb index a8296d9..5504d37 100644 --- a/lib/rtesseract/box.rb +++ b/lib/rtesseract/box.rb @@ -9,7 +9,10 @@ def run(source, errors, options) options.tessedit_create_hocr = 1 RTesseract::Command.new(source, temp_file_path, errors, options).run do |output_path| - parse(File.read("#{output_path}.hocr")) + filename = "#{output_path}.hocr" + content = File.read(filename) + remove_tmp_file(filename) + parse(content) end end diff --git a/lib/rtesseract/version.rb b/lib/rtesseract/version.rb index 1e5808a..11c6af8 100644 --- a/lib/rtesseract/version.rb +++ b/lib/rtesseract/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true class RTesseract - VERSION = '3.1.2' + VERSION = '3.1.3' end diff --git a/spec/rtesseract/box_spec.rb b/spec/rtesseract/box_spec.rb index 391d554..21ae9bc 100644 --- a/spec/rtesseract/box_spec.rb +++ b/spec/rtesseract/box_spec.rb @@ -13,4 +13,10 @@ it 'bounding box' do expect(instance.to_box).to include(word: 'you', x_start: 69, y_start: 17, x_end: 100, y_end: 31, confidence: 96) end + + it 'removes the temp hocr file' do + initial_count = Dir["#{Dir.tmpdir}/*"].length + instance.to_box + expect(initial_count).to eql(Dir["#{Dir.tmpdir}/*"].length) + end end