aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/raw_email.rb
blob: 3b466cb81251447a524866839e02f2dce2323dce (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
62
# == Schema Information
#
# 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: hello@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
        if request_id.empty?
            raise "Failed to find the id number of the associated request: has it been saved?"
        end

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

    def filepath
        if incoming_message_id.empty?
            raise "Failed to find the id number of the associated incoming message: has it been saved?"
        end

        File.join(directory, incoming_message_id)
    end

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

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

    def destroy_file_representation!
        File.delete(filepath)
    end

    private

    def request_id
        incoming_message.info_request.id.to_s
    end

    def incoming_message_id
        incoming_message.id.to_s
    end
end