diff options
Diffstat (limited to 'app/models/foi_attachment.rb')
-rw-r--r-- | app/models/foi_attachment.rb | 64 |
1 files changed, 38 insertions, 26 deletions
diff --git a/app/models/foi_attachment.rb b/app/models/foi_attachment.rb index a8d105f52..37a9c9827 100644 --- a/app/models/foi_attachment.rb +++ b/app/models/foi_attachment.rb @@ -1,4 +1,4 @@ -# encoding: UTF-8 +# -*- encoding : utf-8 -*- # == Schema Information # # Table name: foi_attachments @@ -62,19 +62,18 @@ class FoiAttachment < ActiveRecord::Base } update_display_size! @cached_body = d + if String.method_defined?(:encode) + @cached_body = @cached_body.force_encoding("ASCII-8BIT") + end end + # raw body, encoded as binary def body if @cached_body.nil? tries = 0 delay = 1 begin - binary_data = File.open(self.filepath, "rb" ){ |file| file.read } - if self.content_type =~ /^text/ - @cached_body = convert_string_to_utf8_or_binary(binary_data, 'UTF-8') - else - @cached_body = binary_data - end + @cached_body = File.open(filepath, "rb" ){ |file| file.read } rescue Errno::ENOENT # we've lost our cached attachments for some reason. Reparse them. if tries > BODY_MAX_TRIES @@ -93,6 +92,17 @@ class FoiAttachment < ActiveRecord::Base return @cached_body end + # body as UTF-8 text, with scrubbing of invalid chars if needed + def body_as_text + convert_string_to_utf8(body, 'UTF-8') + end + + # for text types, the scrubbed UTF-8 text. For all other types, the + # raw binary + def default_body + text_type? ? body_as_text.string : body + end + # List of DSN codes taken from RFC 3463 # http://tools.ietf.org/html/rfc3463 DsnToMessage = { @@ -244,36 +254,32 @@ class FoiAttachment < ActiveRecord::Base # The full list of supported types can be found at # https://docs.google.com/support/bin/answer.py?hl=en&answer=1189935 def has_google_docs_viewer? - return !! { - "application/pdf" => true, # .pdf - "image/tiff" => true, # .tiff - - "application/vnd.ms-word" => true, # .doc - "application/vnd.openxmlformats-officedocument.wordprocessingml.document" => true, # .docx + [ + "application/pdf", # .pdf + "image/tiff", # .tiff - "application/vnd.ms-powerpoint" => true, # .ppt - "application/vnd.openxmlformats-officedocument.presentationml.presentation" => true, # .pptx + "application/vnd.ms-word", # .doc + "application/vnd.openxmlformats-officedocument.wordprocessingml.document", # .docx - "application/vnd.ms-excel" => true, # .xls - "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" => true, # .xlsx + "application/vnd.ms-powerpoint", # .ppt + "application/vnd.openxmlformats-officedocument.presentationml.presentation", # .pptx - } [self.content_type] + "application/vnd.ms-excel", # .xls + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", # .xlsx + ].include?(content_type) end # Whether this type has a "View as HTML" def has_body_as_html? - return ( - !!{ - "text/plain" => true, - "application/rtf" => true, - }[self.content_type] or - self.has_google_docs_viewer? - ) + [ + "text/plain", + "application/rtf", + ].include?(content_type) || has_google_docs_viewer? end # Name of type of attachment type - only valid for things that has_body_as_html? def name_of_content_type - return { + { "text/plain" => "Text file", 'application/rtf' => "RTF file", @@ -298,5 +304,11 @@ class FoiAttachment < ActiveRecord::Base AttachmentToHTML.to_html(self, to_html_opts) end + private + + def text_type? + AlaveteliTextMasker::TextMask.include?(content_type) + end + end |