aboutsummaryrefslogtreecommitdiffstats
path: root/app/models
diff options
context:
space:
mode:
Diffstat (limited to 'app/models')
-rw-r--r--app/models/foi_attachment.rb33
-rw-r--r--app/models/incoming_message.rb86
-rw-r--r--app/models/request_mailer.rb1
3 files changed, 65 insertions, 55 deletions
diff --git a/app/models/foi_attachment.rb b/app/models/foi_attachment.rb
index a7bc690ea..057dcdb69 100644
--- a/app/models/foi_attachment.rb
+++ b/app/models/foi_attachment.rb
@@ -7,6 +7,8 @@
# Email: francis@mysociety.org; WWW: http://www.mysociety.org/
# This is the type which is used to send data about attachments to the view
+require 'digest'
+
class FoiAttachment < ActiveRecord::Base
belongs_to :incoming_message
validates_presence_of :content_type
@@ -14,41 +16,38 @@ class FoiAttachment < ActiveRecord::Base
validates_presence_of :display_size
before_validation :ensure_filename!, :only => [:filename]
+ before_destroy :delete_cached_file!
def directory
- if ENV["RAILS_ENV"] == "test"
- base_dir = File.join('cache', 'attachments_test')
- else
- base_dir = File.join('cache', 'attachments')
- end
- request_id = self.incoming_message.info_request.id.to_s
- return File.join(base_dir, request_id[0..2], request_id, self.incoming_message.id.to_s)
+ base_dir = File.join("cache", "attachments_#{ENV['RAILS_ENV']}")
+ return File.join(base_dir, self.hexdigest[0..2])
end
def filepath
- part_number = self.url_part_number.nil? ? "1" : self.url_part_number.to_s
- File.join(self.directory, part_number)
+ File.join(self.directory, self.hexdigest)
+ end
+
+ def delete_cached_file!
+ begin
+ File.delete(self.filepath)
+ rescue
+ end
end
def body=(d)
+ self.hexdigest = Digest::MD5.hexdigest(d)
if !File.exists?(self.directory)
FileUtils.mkdir_p self.directory
end
File.open(self.filepath, "wb") { |file|
file.write d
}
- self.update_display_size!
+ update_display_size!
end
def body
if @cached_body.nil?
- if !File.exists?(self.filepath)
- # For some reason, we've lost the cache; extract everything again
- self.incoming_message.extract_attachments!
- @cached_body = self.body
- else
- @cached_body = File.open(self.filepath, "rb" ).read
- end
+ @cached_body = File.open(self.filepath, "rb" ).read
end
return @cached_body
end
diff --git a/app/models/incoming_message.rb b/app/models/incoming_message.rb
index 6fa08b261..d8e2891e5 100644
--- a/app/models/incoming_message.rb
+++ b/app/models/incoming_message.rb
@@ -386,7 +386,12 @@ class IncomingMessage < ActiveRecord::Base
if !prefix.nil? && prefix.downcase.match(/^(postmaster|mailer-daemon|auto_reply|donotreply|no.reply)$/)
return false
end
-
+ if !self.mail['return-path'].nil? && self.mail['return-path'].addr == "<>"
+ return false
+ end
+ if !self.mail['auto-submitted'].nil? && !self.mail['auto-submitted'].keys.empty?
+ return false
+ end
return true
end
@@ -1007,16 +1012,7 @@ class IncomingMessage < ActiveRecord::Base
return p
end
# Returns attachments that are uuencoded in main body part
- def get_main_body_text_uudecode_attachments
- # we don't use get_main_body_text_internal, as we want to avoid charset
- # conversions, since /usr/bin/uudecode needs to deal with those.
- # e.g. for https://secure.mysociety.org/admin/foi/request/show_raw_email/24550
- main_part = get_main_body_text_part
- if main_part.nil?
- return []
- end
- text = main_part.body
-
+ def _uudecode_and_save_attachments(text)
# Find any uudecoded things buried in it, yeuchly
uus = text.scan(/^begin.+^`\n^end\n/sm)
attachments = []
@@ -1039,11 +1035,16 @@ class IncomingMessage < ActiveRecord::Base
else
content_type = 'application/octet-stream'
end
- attachment = self.foi_attachments.create(:body => content,
- :filename => filename,
- :content_type => content_type)
+ hexdigest = Digest::MD5.hexdigest(content)
+ attachment = self.foi_attachments.find_or_create_by_hexdigest(:hexdigest => hexdigest)
+ attachment.update_attributes(:filename => filename,
+ :content_type => content_type,
+ :body => content,
+ :display_size => "0K")
+ attachment.save!
+ attachments << attachment
end
- return self.foi_attachments
+ return attachments
end
def get_attachments_for_display
@@ -1059,16 +1060,11 @@ class IncomingMessage < ActiveRecord::Base
return attachments
end
-
def extract_attachments!
leaves = get_attachment_leaves # XXX check where else this is called from
-
# XXX we have to call ensure_parts_counted after get_attachment_leaves
# which is really messy.
ensure_parts_counted
-
- self.foi_attachments.clear
-
attachments = []
for leaf in leaves
body = leaf.body
@@ -1106,24 +1102,36 @@ class IncomingMessage < ActiveRecord::Base
#attachment.body = leaf.within_rfc822_attachment.port.to_s
end
end
- self.foi_attachments.create(:content_type => leaf.content_type,
- :url_part_number => leaf.url_part_number,
- :filename => _get_part_file_name(leaf),
- :body => body,
- :charset => leaf.charset,
- :within_rfc822_attachment => within_rfc822_attachment)
-
- end
-
- uudecode_attachments = get_main_body_text_uudecode_attachments
- c = @count_first_uudecode_count
- for uudecode_attachment in uudecode_attachments
- c += 1
- uudecode_attachment.url_part_number = c
- uudecode_attachment.save!
- end
- return self.foi_attachments
- end
+ hexdigest = Digest::MD5.hexdigest(body)
+ attachment = self.foi_attachments.find_or_create_by_hexdigest(:hexdigest => hexdigest)
+ attachment.update_attributes(:url_part_number => leaf.url_part_number,
+ :content_type => leaf.content_type,
+ :filename => _get_part_file_name(leaf),
+ :charset => leaf.charset,
+ :within_rfc822_subject => within_rfc822_subject,
+ :display_size => "0K",
+ :body => body)
+ attachment.save!
+ attachments << attachment.id
+ end
+ main_part = get_main_body_text_part
+ # we don't use get_main_body_text_internal, as we want to avoid charset
+ # conversions, since /usr/bin/uudecode needs to deal with those.
+ # e.g. for https://secure.mysociety.org/admin/foi/request/show_raw_email/24550
+ if !main_part.nil?
+ uudecoded_attachments = _uudecode_and_save_attachments(main_part.body)
+ c = @count_first_uudecode_count
+ for uudecode_attachment in uudecoded_attachments
+ c += 1
+ uudecode_attachment.url_part_number = c
+ uudecode_attachment.save!
+ attachments << uudecode_attachment.id
+ end
+ end
+
+ # now get rid of any attachments we no longer have
+ FoiAttachment.destroy_all("id NOT IN (#{attachments.join(',')}) AND incoming_message_id = #{self.id}")
+ end
# Returns body text as HTML with quotes flattened, and emails removed.
def get_body_for_html_display(collapse_quoted_sections = true)
@@ -1164,6 +1172,7 @@ class IncomingMessage < ActiveRecord::Base
return text
end
+
# Returns text of email for using in quoted section when replying
def get_body_for_quoting
# Get the body text with emails and quoted sections removed
@@ -1301,6 +1310,7 @@ class IncomingMessage < ActiveRecord::Base
return text
end
+
# Returns text for indexing
def get_text_for_indexing_full
return get_body_for_quoting + "\n\n" + get_attachment_text_full
diff --git a/app/models/request_mailer.rb b/app/models/request_mailer.rb
index 75dc58447..272f2ea83 100644
--- a/app/models/request_mailer.rb
+++ b/app/models/request_mailer.rb
@@ -10,6 +10,7 @@ require 'alaveteli_file_types'
class RequestMailer < ApplicationMailer
+
# Used when an FOI officer uploads a response from their web browser - this is
# the "fake" email used to store in the same format in the database as if they
# had emailed it.