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 @ 958:352539ac7b43

History | View | Annotate | Download (3.69 KB)

1 0:513646585e45 Chris
# Redmine - project management software
2 441:cbce1fd3b1b7 Chris
# Copyright (C) 2006-2011  Jean-Philippe Lang
3 0:513646585e45 Chris
#
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 441:cbce1fd3b1b7 Chris
#
9 0:513646585e45 Chris
# 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 441:cbce1fd3b1b7 Chris
#
14 0:513646585e45 Chris
# 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 257:3ecf99348b9f chris
20 0:513646585e45 Chris
  before_filter :find_project
21
  before_filter :file_readable, :read_authorize, :except => :destroy
22
  before_filter :delete_authorize, :only => :destroy
23 257:3ecf99348b9f chris
  before_filter :active_authorize, :only => :toggle_active
24 441:cbce1fd3b1b7 Chris
25 909:cbb26bc654de Chris
  accept_api_auth :show, :download
26 441:cbce1fd3b1b7 Chris
27 0:513646585e45 Chris
  def show
28 909:cbb26bc654de Chris
    respond_to do |format|
29
      format.html {
30
        if @attachment.is_diff?
31
          @diff = File.new(@attachment.diskfile, "rb").read
32
          @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline'
33
          @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type)
34
          # Save diff type as user preference
35
          if User.current.logged? && @diff_type != User.current.pref[:diff_type]
36
            User.current.pref[:diff_type] = @diff_type
37
            User.current.preference.save
38
          end
39
          render :action => 'diff'
40
        elsif @attachment.is_text? && @attachment.filesize <= Setting.file_max_size_displayed.to_i.kilobyte
41
          @content = File.new(@attachment.diskfile, "rb").read
42
          render :action => 'file'
43
        else
44
          download
45
        end
46
      }
47
      format.api
48 0:513646585e45 Chris
    end
49
  end
50 441:cbce1fd3b1b7 Chris
51 0:513646585e45 Chris
  def download
52 957:35782bf2eb58 Chris
    # cc: formerly this happened only if "@attachment.container.is_a?(Version)"
53
    # or Project. Not good for us, we want to tally all downloads
54
    @attachment.increment_download
55 441:cbce1fd3b1b7 Chris
56 0:513646585e45 Chris
    # images are sent inline
57
    send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename),
58 441:cbce1fd3b1b7 Chris
                                    :type => detect_content_type(@attachment),
59 0:513646585e45 Chris
                                    :disposition => (@attachment.image? ? 'inline' : 'attachment')
60 441:cbce1fd3b1b7 Chris
61 0:513646585e45 Chris
  end
62 441:cbce1fd3b1b7 Chris
63 909:cbb26bc654de Chris
  verify :method => :delete, :only => :destroy
64 0:513646585e45 Chris
  def destroy
65
    # Make sure association callbacks are called
66
    @attachment.container.attachments.delete(@attachment)
67
    redirect_to :back
68
  rescue ::ActionController::RedirectBackError
69
    redirect_to :controller => 'projects', :action => 'show', :id => @project
70
  end
71 441:cbce1fd3b1b7 Chris
72 257:3ecf99348b9f chris
  def toggle_active
73
    @attachment.active = !@attachment.active?
74
    @attachment.save!
75
    render :layout => false
76
  end
77
78 0:513646585e45 Chris
private
79
  def find_project
80
    @attachment = Attachment.find(params[:id])
81
    # Show 404 if the filename in the url is wrong
82
    raise ActiveRecord::RecordNotFound if params[:filename] && params[:filename] != @attachment.filename
83
    @project = @attachment.project
84
  rescue ActiveRecord::RecordNotFound
85
    render_404
86
  end
87 441:cbce1fd3b1b7 Chris
88 0:513646585e45 Chris
  # Checks that the file exists and is readable
89
  def file_readable
90
    @attachment.readable? ? true : render_404
91
  end
92 441:cbce1fd3b1b7 Chris
93 0:513646585e45 Chris
  def read_authorize
94
    @attachment.visible? ? true : deny_access
95
  end
96 441:cbce1fd3b1b7 Chris
97 0:513646585e45 Chris
  def delete_authorize
98
    @attachment.deletable? ? true : deny_access
99
  end
100 441:cbce1fd3b1b7 Chris
101 257:3ecf99348b9f chris
  def active_authorize
102
    true
103
  end
104
105 0:513646585e45 Chris
  def detect_content_type(attachment)
106
    content_type = attachment.content_type
107
    if content_type.blank?
108
      content_type = Redmine::MimeType.of(attachment.filename)
109
    end
110
    content_type.to_s
111
  end
112
end