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