aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/raw_email.rb
blob: 77705b0f60558893a8bf3ff07008761f645eb1e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# == Schema Information
# Schema version: 114
#
# Table name: raw_emails
#
#  id :integer         not null, primary key
#

# models/raw_email.rb:
# The fat part of models/incoming_message.rb
#
# Copyright (c) 2008 UK Citizens Online Democracy. All rights reserved.
# Email: francis@mysociety.org; WWW: http://www.mysociety.org/

class RawEmail < ActiveRecord::Base
    # deliberately don't strip_attributes, so keeps raw email properly

    has_one :incoming_message

    def directory
        request_id = self.incoming_message.info_request.id.to_s
        if request_id.empty?
            raise "Failed to find the id number of the associated request: has it been saved?"
        end

        if Rails.env.test?
            return File.join(Rails.root, 'files/raw_email_test')
        else
            return File.join(Configuration::raw_emails_location,
                             request_id[0..2], request_id)
        end
    end

    def filepath
        incoming_message_id = self.incoming_message.id.to_s
        if incoming_message_id.empty?
            raise "Failed to find the id number of the associated incoming message: has it been saved?"
        end
        File.join(self.directory, incoming_message_id)
    end

    def data=(d)
        if !File.exists?(self.directory)
            FileUtils.mkdir_p self.directory
        end
        File.atomic_write(self.filepath) { |file|
            file.write d
        }
    end

    def data
        File.open(self.filepath, "r").read
    end

    def destroy_file_representation!
        File.delete(self.filepath)
    end

end