diff options
author | Gareth Rees <gareth@mysociety.org> | 2014-04-01 11:34:30 +0100 |
---|---|---|
committer | Gareth Rees <gareth@mysociety.org> | 2014-04-01 11:34:30 +0100 |
commit | 82c69083609ad14b127c0037ecc8c4df959654ac (patch) | |
tree | ce622e272a6c5ba4c5d5e1d07d5f8045622c1cee /lib/attachment_to_html/adapters | |
parent | b6561750b971030767a0e8146be1414471518086 (diff) |
Get attachment body outside of chdir
Diffstat (limited to 'lib/attachment_to_html/adapters')
-rw-r--r-- | lib/attachment_to_html/adapters/pdf.rb | 30 | ||||
-rw-r--r-- | lib/attachment_to_html/adapters/rtf.rb | 30 |
2 files changed, 36 insertions, 24 deletions
diff --git a/lib/attachment_to_html/adapters/pdf.rb b/lib/attachment_to_html/adapters/pdf.rb index 8f826b910..cc1bf06bc 100644 --- a/lib/attachment_to_html/adapters/pdf.rb +++ b/lib/attachment_to_html/adapters/pdf.rb @@ -83,9 +83,12 @@ module AttachmentToHTML end def convert + # Get the attachment body outside of the chdir call as getting + # the body may require opening files too + text = attachment_body + @converted ||= Dir.chdir(tmpdir) do - tempfile = create_tempfile - write_attachment_body_to_tempfile(tempfile) + tempfile = create_tempfile(text) html = AlaveteliExternalCommand.run("pdftohtml", "-nodrm", "-zoom", "1.0", "-stdout", "-enc", "UTF-8", @@ -98,17 +101,16 @@ module AttachmentToHTML end end - def create_tempfile - if RUBY_VERSION.to_f >= 1.9 - Tempfile.new('foiextract', '.', :encoding => attachment.body.encoding) - else - Tempfile.new('foiextract', '.') - end - end - - def write_attachment_body_to_tempfile(tempfile) - tempfile.print(attachment.body) + def create_tempfile(text) + tempfile = if RUBY_VERSION.to_f >= 1.9 + Tempfile.new('foiextract', '.', + :encoding => text.encoding) + else + Tempfile.new('foiextract', '.') + end + tempfile.print(text) tempfile.flush + tempfile end def cleanup_tempfile(tempfile) @@ -116,6 +118,10 @@ module AttachmentToHTML tempfile.delete end + def attachment_body + @attachment_body ||= attachment.body + end + end end end diff --git a/lib/attachment_to_html/adapters/rtf.rb b/lib/attachment_to_html/adapters/rtf.rb index f38e5e381..24987a975 100644 --- a/lib/attachment_to_html/adapters/rtf.rb +++ b/lib/attachment_to_html/adapters/rtf.rb @@ -83,9 +83,12 @@ module AttachmentToHTML end def convert + # Get the attachment body outside of the chdir call as getting + # the body may require opening files too + text = attachment_body + @converted ||= Dir.chdir(tmpdir) do - tempfile = create_tempfile - write_attachment_body_to_tempfile(tempfile) + tempfile = create_tempfile(text) html = AlaveteliExternalCommand.run("unrtf", "--html", tempfile.path, :timeout => 120 @@ -97,17 +100,16 @@ module AttachmentToHTML end end - def create_tempfile - if RUBY_VERSION.to_f >= 1.9 - Tempfile.new('foiextract', '.', :encoding => attachment.body.encoding) - else - Tempfile.new('foiextract', '.') - end - end - - def write_attachment_body_to_tempfile(tempfile) - tempfile.print(attachment.body) + def create_tempfile(text) + tempfile = if RUBY_VERSION.to_f >= 1.9 + Tempfile.new('foiextract', '.', + :encoding => text.encoding) + else + Tempfile.new('foiextract', '.') + end + tempfile.print(text) tempfile.flush + tempfile end def cleanup_tempfile(tempfile) @@ -115,6 +117,10 @@ module AttachmentToHTML tempfile.delete end + def attachment_body + @attachment_body ||= attachment.body + end + end end end |