summaryrefslogtreecommitdiff
path: root/app/controllers/trigger_controller.rb
blob: 6cc9fa7a4f2411098bc680d2d496e9a331c4378e (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
class TriggerController < ApplicationController

  def voicemail
    if !params[:sip_account_id].blank?
      sip_account = SipAccount.where(:id => params[:sip_account_id].to_i).first
      if sip_account
        sip_account.voicemail_messages.where(:notification => nil).each do |message|
          message.notification = false
          message.save
          if !File.exists?( message.file_path )
            next
          end

          user = sip_account.sip_accountable
          if user.class != User
            next
          end

          if  user.email.blank?
            next
          end

          voicemail_settings = sip_account.voicemail_setting
          if !voicemail_settings
            voicemail_settings = VoicemailSetting.new(:notify => user.send_voicemail_as_email_attachment, :attachment => user.send_voicemail_as_email_attachment, :mark_read => user.send_voicemail_as_email_attachment)
          end

          message.notification = voicemail_settings.notify
          if voicemail_settings.notify
            if Notifications.new_voicemail(message, voicemail_settings.attachment).deliver
              if voicemail_settings.purge
                message.delete
                next
              end
              message.save
              if voicemail_settings.mark_read
                message.mark_read
              end
            end
          else
            message.save
          end
        end

        render(
          :status => 200,
          :layout => false,
          :content_type => 'text/plain',
          :text => "<!-- OK -->",
        )
      else
        render(
          :status => 404,
          :layout => false,
          :content_type => 'text/plain',
          :text => "<!-- Account not found -->",
        )
      end
    end
  end

  def fax
    if !params[:fax_account_id].blank?
      fax_account = FaxAccount.where(:id => params[:fax_account_id].to_i).first
      if fax_account
        fax_account.fax_documents.where(:state => 'received').each do |fax_document|

          pdf_file = fax_document.tiff_to_pdf

          if !pdf_file
            fax_document.state = 'unsuccessful'
            fax_document.save
            next
          end

          working_path, tiff_file = File.split(fax_document.tiff)
          if fax_document.store_dir != working_path
            FileUtils.mkdir(fax_document.store_dir)
            FileUtils.mv(fax_document.tiff, fax_document.store_dir)
            fax_document.tiff = "#{fax_document.store_dir}/#{tiff_file}"
          end

          fax_document.document = File.open(pdf_file)
          fax_document.state = 'successful'
          
          if fax_document.save
            begin
              File.delete(pdf_file)
            rescue => e
              logger.error "PDF fax file could not be deleted: #{pdf_file} => #{e.inspect}"
            end
            fax_document.save
            fax_document.render_thumbnails
          else
            fax_document.state = 'unsuccessful'
            fax_document.save
          end
        end

        render(
          :status => 200,
          :layout => false,
          :content_type => 'text/plain',
          :text => "<!-- OK -->",
        )
      else
        render(
          :status => 404,
          :layout => false,
          :content_type => 'text/plain',
          :text => "<!-- Account not found -->",
        )
      end
    end
  end
end