Skip to content

Commit

Permalink
Merge pull request #135 from manovasanth1227/file_not_supported_fix
Browse files Browse the repository at this point in the history
File Not Supported Error fix
  • Loading branch information
satoryu authored Jun 30, 2023
2 parents e7cd41f + c6cd250 commit 564a364
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/docx/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def initialize(path_or_io, options = {})

# if path-or_io is string && does not contain a null byte
if (path_or_io.instance_of?(String) && !/\u0000/.match?(path_or_io))
raise Errno::EIO.new('Invalid file format') if !File.extname(path_or_io).eql?('.docx')
@zip = Zip::File.open(path_or_io)
else
@zip = Zip::File.open_buffer(path_or_io)
Expand All @@ -38,7 +39,7 @@ def initialize(path_or_io, options = {})
load_styles
yield(self) if block_given?
ensure
@zip.close
@zip.close unless @zip.nil?
end

# This stores the current global document properties, for now
Expand Down
15 changes: 15 additions & 0 deletions spec/docx/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@
end.to_not raise_error
end
end

context 'When reading a un-supported file' do
it 'should throw file not supported error' do
expect do
Docx::Document.open(@fixtures_path + '/invalid_format.pdf')
end.to raise_error(Errno::EIO, 'Input/output error - Invalid file format')
end

it 'should throw file not found error' do
invalid_path = @fixtures_path + '/invalid_file_path.docx'
expect do
Docx::Document.open(invalid_path)
end.to raise_error(Zip::Error, "File #{invalid_path} not found")
end
end
end

describe 'reading' do
Expand Down

0 comments on commit 564a364

Please sign in to comment.