Class: MailsViewer::HomeController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
app/controllers/mails_viewer/home_controller.rb

Instance Method Summary collapse

Instance Method Details

#attachmentObject



46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/controllers/mails_viewer/home_controller.rb', line 46

def attachment
  if @filename
    mail = Mail.read(@filename)
    attachment = mail.attachments[params[:attachment]]

    if attachment
      send_data attachment.body.decoded, filename: params[:attachment], type: attachment.content_type and return
    end
  end

  head :not_found
end

#htmlObject



27
28
29
30
31
32
33
34
35
# File 'app/controllers/mails_viewer/home_controller.rb', line 27

def html
  if @filename
    mail = Mail.read(@filename)
    body = mail.html_part ? mail.html_part.body : mail.body
    render text: body
  else
    head :not_found
  end
end

#indexObject



7
8
9
10
11
12
13
14
15
16
17
# File 'app/controllers/mails_viewer/home_controller.rb', line 7

def index
  if File.exist?(mails_path)
    Dir.chdir(mails_path) do
      @mails = Dir["**/*"]
        .select{|f| File.file?(f)}
        .map{|f| [Mail.read(f), f]}
    end
  else
    @mails = []
  end
end

#plainObject



37
38
39
40
41
42
43
44
# File 'app/controllers/mails_viewer/home_controller.rb', line 37

def plain
  if @filename
    mail = Mail.read(@filename)
    @body = mail.text_part ? mail.text_part.body.to_s : mail.body.to_s
  else
    head :not_found
  end
end

#rawObject



19
20
21
22
23
24
25
# File 'app/controllers/mails_viewer/home_controller.rb', line 19

def raw
  if @filename
    render text: File.read(@filename)
  else
    head :not_found
  end
end