aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/foi_attachment.rb
diff options
context:
space:
mode:
authorLouise Crow <louise.crow@gmail.com>2015-06-11 17:27:49 +0100
committerLouise Crow <louise.crow@gmail.com>2015-06-22 17:43:22 +0100
commit19c5e549b8529b10faa36307efceab207afd3224 (patch)
treee2823f98304cfb41d583e07b07b5d1f842387c3b /app/models/foi_attachment.rb
parent3d8f0cc6b70b55aad20ab4d86642f0e6d605c921 (diff)
Add body_as_text, default_body.
Main intent here is to be more explicit about what form you're getting the attachment body in - valid UTF-8 or binary, and has it been cleaned up in order to be UTF-8?
Diffstat (limited to 'app/models/foi_attachment.rb')
-rw-r--r--app/models/foi_attachment.rb21
1 files changed, 15 insertions, 6 deletions
diff --git a/app/models/foi_attachment.rb b/app/models/foi_attachment.rb
index 978e11a17..37a9c9827 100644
--- a/app/models/foi_attachment.rb
+++ b/app/models/foi_attachment.rb
@@ -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 text_type?
- @cached_body = convert_string_to_utf8(binary_data, 'UTF-8').string
- 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,16 @@ 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