Skip to content

Commit 564a364

Browse files
authored
Merge pull request ruby-docx#135 from manovasanth1227/file_not_supported_fix
File Not Supported Error fix
2 parents e7cd41f + c6cd250 commit 564a364

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/docx/document.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def initialize(path_or_io, options = {})
2525

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

4445
# This stores the current global document properties, for now

spec/docx/document_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@
1818
end.to_not raise_error
1919
end
2020
end
21+
22+
context 'When reading a un-supported file' do
23+
it 'should throw file not supported error' do
24+
expect do
25+
Docx::Document.open(@fixtures_path + '/invalid_format.pdf')
26+
end.to raise_error(Errno::EIO, 'Input/output error - Invalid file format')
27+
end
28+
29+
it 'should throw file not found error' do
30+
invalid_path = @fixtures_path + '/invalid_file_path.docx'
31+
expect do
32+
Docx::Document.open(invalid_path)
33+
end.to raise_error(Zip::Error, "File #{invalid_path} not found")
34+
end
35+
end
2136
end
2237

2338
describe 'reading' do

0 commit comments

Comments
 (0)