aboutsummaryrefslogtreecommitdiffstats
path: root/lib/attachment_to_html/adapters
diff options
context:
space:
mode:
Diffstat (limited to 'lib/attachment_to_html/adapters')
-rw-r--r--lib/attachment_to_html/adapters/pdf.rb30
-rw-r--r--lib/attachment_to_html/adapters/rtf.rb30
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