Skip to content

Commit

Permalink
raise if docuemtn select result is nil. maybe its invalid.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkusaka committed Nov 4, 2022
1 parent ba03954 commit debbcca
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
4 changes: 4 additions & 0 deletions lib/docx_templater.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
module DocxTemplater
# Some docx, such as generated by non-microsoft word, sometime make invalid docx. Its input stream returns Zip::NullInputStream. When it detected, this error is raised.
InvalidInputError = Class.new(StandardError)


module_function

def log(str)
Expand Down
4 changes: 1 addition & 3 deletions lib/docx_templater/docx_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
module DocxTemplater
class DocxCreator
attr_reader :template_path, :template_processor
# Some docx, such as generated by non-microsoft word, sometime make invalid docx. Its input stream returns Zip::NullInputStream. When it detected, this error is raised.
InvalidInputError = Class.new(StandardError)

def initialize(template_path, data, escape_html = true)
@template_path = template_path
Expand All @@ -25,7 +23,7 @@ def generate_docx_bytes
entry_name = entry.name
out.put_next_entry(entry_name)
if entry.get_input_stream.is_a?(Zip::NullInputStream)
raise InvalidInputError
raise ::DocxTemplater::InvalidInputError, "stream is Zip::NullInputStream"
end
out.write(copy_or_template(entry_name, entry.get_input_stream.read))
end
Expand Down
6 changes: 6 additions & 0 deletions lib/docx_templater/template_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ def self.scan_params template_path
document = Zip::File.open(template_path)
.select { |entry| entry.name == DOCUMENT_XML }
.first

unless document
raise ::DocxTemplater::InvalidInputError, "There is no entry matching to #{DOCUMENT_XML}"
end

document = document
.get_input_stream
.read
document.force_encoding(Encoding::UTF_8) if document.respond_to?(:force_encoding)
Expand Down

0 comments on commit debbcca

Please sign in to comment.