aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/models/foi_attachment.rb16
-rw-r--r--spec/models/foi_attachment_spec.rb18
2 files changed, 24 insertions, 10 deletions
diff --git a/app/models/foi_attachment.rb b/app/models/foi_attachment.rb
index d12df688a..20c40abea 100644
--- a/app/models/foi_attachment.rb
+++ b/app/models/foi_attachment.rb
@@ -34,6 +34,9 @@ class FoiAttachment < ActiveRecord::Base
before_validation :ensure_filename!, :only => [:filename]
before_destroy :delete_cached_file!
+ BODY_MAX_TRIES = 3
+ BODY_MAX_DELAY = 5
+
def directory
base_dir = File.join(File.dirname(__FILE__), "../../cache", "attachments_#{ENV['RAILS_ENV']}")
return File.join(base_dir, self.hexdigest[0..2])
@@ -45,6 +48,7 @@ class FoiAttachment < ActiveRecord::Base
def delete_cached_file!
begin
+ @cached_body = nil
File.delete(self.filepath)
rescue
end
@@ -57,7 +61,6 @@ class FoiAttachment < ActiveRecord::Base
end
File.open(self.filepath, "wb") { |file|
file.write d
- file.fsync
}
update_display_size!
@cached_body = d
@@ -65,12 +68,23 @@ class FoiAttachment < ActiveRecord::Base
def body
if @cached_body.nil?
+ tries = 0
+ delay = 1
begin
@cached_body = File.open(self.filepath, "rb" ).read
rescue Errno::ENOENT
# we've lost our cached attachments for some reason. Reparse them.
+ if tries > BODY_MAX_TRIES
+ raise
+ else
+ sleep delay
+ end
+ tries += 1
+ delay *= 2
+ delay = BODY_MAX_DELAY if delay > BODY_MAX_DELAY
force = true
self.incoming_message.parse_raw_email!(force)
+ retry
end
end
return @cached_body
diff --git a/spec/models/foi_attachment_spec.rb b/spec/models/foi_attachment_spec.rb
index d8166dddc..05c4fc5fd 100644
--- a/spec/models/foi_attachment_spec.rb
+++ b/spec/models/foi_attachment_spec.rb
@@ -20,17 +20,17 @@ describe FoiAttachment, " when calculating due date" do
attachment.display_size.should == "0K"
end
it "reparses the body if it disappears" do
- mail_body = load_file_fixture('incoming-request-attach-attachments.email')
- mail = TMail::Mail.parse(mail_body)
- mail.base64_decode
im = incoming_messages(:useless_incoming_message)
- im.stub!(:mail).and_return(mail)
- #im.extract_attachments!
- attachments = im.get_attachments_for_display
- FileUtils.rm attachments[0].filepath
+ im.extract_attachments!
+ main = im.get_main_body_text_part
+ orig_body = main.body
+ main.delete_cached_file!
lambda {
- attachments = im.get_attachments_for_display
- body = attachments[0].body
+ im.get_main_body_text_part.body
}.should_not raise_error(Errno::ENOENT)
+ main.delete_cached_file!
+ main = im.get_main_body_text_part
+ main.body.should == orig_body
+
end
end