Mercurial > hg > soundsoftware-site
comparison app/controllers/attachments_controller.rb @ 441:cbce1fd3b1b7 redmine-1.2
Update to Redmine 1.2-stable branch (Redmine SVN rev 6000)
author | Chris Cannam |
---|---|
date | Mon, 06 Jun 2011 14:24:13 +0100 |
parents | 513646585e45 |
children | 350acce374a2 cbb26bc654de |
comparison
equal
deleted
inserted
replaced
245:051f544170fe | 441:cbce1fd3b1b7 |
---|---|
1 # Redmine - project management software | 1 # Redmine - project management software |
2 # Copyright (C) 2006-2008 Jean-Philippe Lang | 2 # Copyright (C) 2006-2011 Jean-Philippe Lang |
3 # | 3 # |
4 # This program is free software; you can redistribute it and/or | 4 # This program is free software; you can redistribute it and/or |
5 # modify it under the terms of the GNU General Public License | 5 # modify it under the terms of the GNU General Public License |
6 # as published by the Free Software Foundation; either version 2 | 6 # as published by the Free Software Foundation; either version 2 |
7 # of the License, or (at your option) any later version. | 7 # of the License, or (at your option) any later version. |
8 # | 8 # |
9 # This program is distributed in the hope that it will be useful, | 9 # This program is distributed in the hope that it will be useful, |
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 # GNU General Public License for more details. | 12 # GNU General Public License for more details. |
13 # | 13 # |
14 # You should have received a copy of the GNU General Public License | 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 | 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. | 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | 17 |
18 class AttachmentsController < ApplicationController | 18 class AttachmentsController < ApplicationController |
19 before_filter :find_project | 19 before_filter :find_project |
20 before_filter :file_readable, :read_authorize, :except => :destroy | 20 before_filter :file_readable, :read_authorize, :except => :destroy |
21 before_filter :delete_authorize, :only => :destroy | 21 before_filter :delete_authorize, :only => :destroy |
22 | 22 |
23 verify :method => :post, :only => :destroy | 23 verify :method => :post, :only => :destroy |
24 | 24 |
25 def show | 25 def show |
26 if @attachment.is_diff? | 26 if @attachment.is_diff? |
27 @diff = File.new(@attachment.diskfile, "rb").read | 27 @diff = File.new(@attachment.diskfile, "rb").read |
28 render :action => 'diff' | 28 render :action => 'diff' |
29 elsif @attachment.is_text? && @attachment.filesize <= Setting.file_max_size_displayed.to_i.kilobyte | 29 elsif @attachment.is_text? && @attachment.filesize <= Setting.file_max_size_displayed.to_i.kilobyte |
31 render :action => 'file' | 31 render :action => 'file' |
32 else | 32 else |
33 download | 33 download |
34 end | 34 end |
35 end | 35 end |
36 | 36 |
37 def download | 37 def download |
38 if @attachment.container.is_a?(Version) || @attachment.container.is_a?(Project) | 38 if @attachment.container.is_a?(Version) || @attachment.container.is_a?(Project) |
39 @attachment.increment_download | 39 @attachment.increment_download |
40 end | 40 end |
41 | 41 |
42 # images are sent inline | 42 # images are sent inline |
43 send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename), | 43 send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename), |
44 :type => detect_content_type(@attachment), | 44 :type => detect_content_type(@attachment), |
45 :disposition => (@attachment.image? ? 'inline' : 'attachment') | 45 :disposition => (@attachment.image? ? 'inline' : 'attachment') |
46 | 46 |
47 end | 47 end |
48 | 48 |
49 def destroy | 49 def destroy |
50 # Make sure association callbacks are called | 50 # Make sure association callbacks are called |
51 @attachment.container.attachments.delete(@attachment) | 51 @attachment.container.attachments.delete(@attachment) |
52 redirect_to :back | 52 redirect_to :back |
53 rescue ::ActionController::RedirectBackError | 53 rescue ::ActionController::RedirectBackError |
54 redirect_to :controller => 'projects', :action => 'show', :id => @project | 54 redirect_to :controller => 'projects', :action => 'show', :id => @project |
55 end | 55 end |
56 | 56 |
57 private | 57 private |
58 def find_project | 58 def find_project |
59 @attachment = Attachment.find(params[:id]) | 59 @attachment = Attachment.find(params[:id]) |
60 # Show 404 if the filename in the url is wrong | 60 # Show 404 if the filename in the url is wrong |
61 raise ActiveRecord::RecordNotFound if params[:filename] && params[:filename] != @attachment.filename | 61 raise ActiveRecord::RecordNotFound if params[:filename] && params[:filename] != @attachment.filename |
62 @project = @attachment.project | 62 @project = @attachment.project |
63 rescue ActiveRecord::RecordNotFound | 63 rescue ActiveRecord::RecordNotFound |
64 render_404 | 64 render_404 |
65 end | 65 end |
66 | 66 |
67 # Checks that the file exists and is readable | 67 # Checks that the file exists and is readable |
68 def file_readable | 68 def file_readable |
69 @attachment.readable? ? true : render_404 | 69 @attachment.readable? ? true : render_404 |
70 end | 70 end |
71 | 71 |
72 def read_authorize | 72 def read_authorize |
73 @attachment.visible? ? true : deny_access | 73 @attachment.visible? ? true : deny_access |
74 end | 74 end |
75 | 75 |
76 def delete_authorize | 76 def delete_authorize |
77 @attachment.deletable? ? true : deny_access | 77 @attachment.deletable? ? true : deny_access |
78 end | 78 end |
79 | 79 |
80 def detect_content_type(attachment) | 80 def detect_content_type(attachment) |
81 content_type = attachment.content_type | 81 content_type = attachment.content_type |
82 if content_type.blank? | 82 if content_type.blank? |
83 content_type = Redmine::MimeType.of(attachment.filename) | 83 content_type = Redmine::MimeType.of(attachment.filename) |
84 end | 84 end |