Chris@0
|
1 # Redmine - project management software
|
Chris@441
|
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
|
Chris@0
|
3 #
|
Chris@0
|
4 # This program is free software; you can redistribute it and/or
|
Chris@0
|
5 # modify it under the terms of the GNU General Public License
|
Chris@0
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@0
|
7 # of the License, or (at your option) any later version.
|
Chris@441
|
8 #
|
Chris@0
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@0
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
12 # GNU General Public License for more details.
|
Chris@441
|
13 #
|
Chris@0
|
14 # You should have received a copy of the GNU General Public License
|
Chris@0
|
15 # along with this program; if not, write to the Free Software
|
Chris@0
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@0
|
17
|
Chris@0
|
18 class AttachmentsController < ApplicationController
|
chris@257
|
19
|
Chris@0
|
20 before_filter :find_project
|
Chris@0
|
21 before_filter :file_readable, :read_authorize, :except => :destroy
|
Chris@0
|
22 before_filter :delete_authorize, :only => :destroy
|
chris@257
|
23 before_filter :active_authorize, :only => :toggle_active
|
Chris@441
|
24
|
Chris@0
|
25 verify :method => :post, :only => :destroy
|
Chris@441
|
26
|
Chris@0
|
27 def show
|
Chris@0
|
28 if @attachment.is_diff?
|
Chris@0
|
29 @diff = File.new(@attachment.diskfile, "rb").read
|
Chris@0
|
30 render :action => 'diff'
|
Chris@0
|
31 elsif @attachment.is_text? && @attachment.filesize <= Setting.file_max_size_displayed.to_i.kilobyte
|
Chris@0
|
32 @content = File.new(@attachment.diskfile, "rb").read
|
Chris@0
|
33 render :action => 'file'
|
Chris@0
|
34 else
|
Chris@0
|
35 download
|
Chris@0
|
36 end
|
Chris@0
|
37 end
|
Chris@441
|
38
|
Chris@0
|
39 def download
|
Chris@0
|
40 if @attachment.container.is_a?(Version) || @attachment.container.is_a?(Project)
|
Chris@0
|
41 @attachment.increment_download
|
Chris@0
|
42 end
|
Chris@441
|
43
|
Chris@0
|
44 # images are sent inline
|
Chris@0
|
45 send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename),
|
Chris@441
|
46 :type => detect_content_type(@attachment),
|
Chris@0
|
47 :disposition => (@attachment.image? ? 'inline' : 'attachment')
|
Chris@441
|
48
|
Chris@0
|
49 end
|
Chris@441
|
50
|
Chris@0
|
51 def destroy
|
Chris@0
|
52 # Make sure association callbacks are called
|
Chris@0
|
53 @attachment.container.attachments.delete(@attachment)
|
Chris@0
|
54 redirect_to :back
|
Chris@0
|
55 rescue ::ActionController::RedirectBackError
|
Chris@0
|
56 redirect_to :controller => 'projects', :action => 'show', :id => @project
|
Chris@0
|
57 end
|
Chris@441
|
58
|
chris@257
|
59 def toggle_active
|
chris@257
|
60 @attachment.active = !@attachment.active?
|
chris@257
|
61 @attachment.save!
|
chris@257
|
62 render :layout => false
|
chris@257
|
63 end
|
chris@257
|
64
|
Chris@0
|
65 private
|
Chris@0
|
66 def find_project
|
Chris@0
|
67 @attachment = Attachment.find(params[:id])
|
Chris@0
|
68 # Show 404 if the filename in the url is wrong
|
Chris@0
|
69 raise ActiveRecord::RecordNotFound if params[:filename] && params[:filename] != @attachment.filename
|
Chris@0
|
70 @project = @attachment.project
|
Chris@0
|
71 rescue ActiveRecord::RecordNotFound
|
Chris@0
|
72 render_404
|
Chris@0
|
73 end
|
Chris@441
|
74
|
Chris@0
|
75 # Checks that the file exists and is readable
|
Chris@0
|
76 def file_readable
|
Chris@0
|
77 @attachment.readable? ? true : render_404
|
Chris@0
|
78 end
|
Chris@441
|
79
|
Chris@0
|
80 def read_authorize
|
Chris@0
|
81 @attachment.visible? ? true : deny_access
|
Chris@0
|
82 end
|
Chris@441
|
83
|
Chris@0
|
84 def delete_authorize
|
Chris@0
|
85 @attachment.deletable? ? true : deny_access
|
Chris@0
|
86 end
|
Chris@441
|
87
|
chris@257
|
88 def active_authorize
|
chris@257
|
89 true
|
chris@257
|
90 end
|
chris@257
|
91
|
Chris@0
|
92 def detect_content_type(attachment)
|
Chris@0
|
93 content_type = attachment.content_type
|
Chris@0
|
94 if content_type.blank?
|
Chris@0
|
95 content_type = Redmine::MimeType.of(attachment.filename)
|
Chris@0
|
96 end
|
Chris@0
|
97 content_type.to_s
|
Chris@0
|
98 end
|
Chris@0
|
99 end
|