To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / app / controllers / attachments_controller.rb @ 832:cbd0b4560105

History | View | Annotate | Download (3.1 KB)

1
# Redmine - project management software
2
# Copyright (C) 2006-2011  Jean-Philippe Lang
3
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU General Public License
6
# as published by the Free Software Foundation; either version 2
7
# of the License, or (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17

    
18
class AttachmentsController < ApplicationController
19

    
20
  before_filter :find_project
21
  before_filter :file_readable, :read_authorize, :except => :destroy
22
  before_filter :delete_authorize, :only => :destroy
23
  before_filter :active_authorize, :only => :toggle_active
24

    
25
  verify :method => :post, :only => :destroy
26

    
27
  def show
28
    if @attachment.is_diff?
29
      @diff = File.new(@attachment.diskfile, "rb").read
30
      render :action => 'diff'
31
    elsif @attachment.is_text? && @attachment.filesize <= Setting.file_max_size_displayed.to_i.kilobyte
32
      @content = File.new(@attachment.diskfile, "rb").read
33
      render :action => 'file'
34
    else
35
      download
36
    end
37
  end
38

    
39
  def download
40
    if @attachment.container.is_a?(Version) || @attachment.container.is_a?(Project)
41
      @attachment.increment_download
42
    end
43

    
44
    # images are sent inline
45
    send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename),
46
                                    :type => detect_content_type(@attachment),
47
                                    :disposition => (@attachment.image? ? 'inline' : 'attachment')
48

    
49
  end
50

    
51
  def destroy
52
    # Make sure association callbacks are called
53
    @attachment.container.attachments.delete(@attachment)
54
    redirect_to :back
55
  rescue ::ActionController::RedirectBackError
56
    redirect_to :controller => 'projects', :action => 'show', :id => @project
57
  end
58

    
59
  def toggle_active
60
    @attachment.active = !@attachment.active?
61
    @attachment.save!
62
    render :layout => false
63
  end
64

    
65
private
66
  def find_project
67
    @attachment = Attachment.find(params[:id])
68
    # Show 404 if the filename in the url is wrong
69
    raise ActiveRecord::RecordNotFound if params[:filename] && params[:filename] != @attachment.filename
70
    @project = @attachment.project
71
  rescue ActiveRecord::RecordNotFound
72
    render_404
73
  end
74

    
75
  # Checks that the file exists and is readable
76
  def file_readable
77
    @attachment.readable? ? true : render_404
78
  end
79

    
80
  def read_authorize
81
    @attachment.visible? ? true : deny_access
82
  end
83

    
84
  def delete_authorize
85
    @attachment.deletable? ? true : deny_access
86
  end
87

    
88
  def active_authorize
89
    true
90
  end
91

    
92
  def detect_content_type(attachment)
93
    content_type = attachment.content_type
94
    if content_type.blank?
95
      content_type = Redmine::MimeType.of(attachment.filename)
96
    end
97
    content_type.to_s
98
  end
99
end