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