Chris@0
|
1 # Redmine - project management software
|
Chris@1494
|
2 # Copyright (C) 2006-2014 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@1115
|
19 before_filter :find_project, :except => :upload
|
Chris@1115
|
20 before_filter :file_readable, :read_authorize, :only => [:show, :download, :thumbnail]
|
Chris@0
|
21 before_filter :delete_authorize, :only => :destroy
|
Chris@1115
|
22 before_filter :authorize_global, :only => :upload
|
Chris@1116
|
23 before_filter :active_authorize, :only => :toggle_active
|
chris@257
|
24
|
chris@967
|
25 include AttachmentsHelper
|
chris@967
|
26 helper :attachments
|
Chris@441
|
27
|
Chris@1115
|
28 accept_api_auth :show, :download, :upload
|
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@0
|
58 @attachment.increment_download
|
Chris@0
|
59 end
|
Chris@441
|
60
|
Chris@1115
|
61 if stale?(:etag => @attachment.digest)
|
Chris@1115
|
62 # images are sent inline
|
Chris@1115
|
63 send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename),
|
Chris@1115
|
64 :type => detect_content_type(@attachment),
|
Chris@1115
|
65 :disposition => (@attachment.image? ? 'inline' : 'attachment')
|
Chris@1115
|
66 end
|
Chris@0
|
67 end
|
Chris@441
|
68
|
Chris@1115
|
69 def thumbnail
|
Chris@1115
|
70 if @attachment.thumbnailable? && thumbnail = @attachment.thumbnail(:size => params[:size])
|
Chris@1115
|
71 if stale?(:etag => thumbnail)
|
Chris@1115
|
72 send_file thumbnail,
|
Chris@1115
|
73 :filename => filename_for_content_disposition(@attachment.filename),
|
Chris@1115
|
74 :type => detect_content_type(@attachment),
|
Chris@1115
|
75 :disposition => 'inline'
|
Chris@1115
|
76 end
|
Chris@1115
|
77 else
|
Chris@1115
|
78 # No thumbnail for the attachment or thumbnail could not be created
|
Chris@1115
|
79 render :nothing => true, :status => 404
|
Chris@1115
|
80 end
|
Chris@1115
|
81 end
|
Chris@1115
|
82
|
Chris@1115
|
83 def upload
|
Chris@1115
|
84 # Make sure that API users get used to set this content type
|
Chris@1115
|
85 # as it won't trigger Rails' automatic parsing of the request body for parameters
|
Chris@1115
|
86 unless request.content_type == 'application/octet-stream'
|
Chris@1115
|
87 render :nothing => true, :status => 406
|
Chris@1115
|
88 return
|
Chris@1115
|
89 end
|
Chris@1115
|
90
|
Chris@1115
|
91 @attachment = Attachment.new(:file => request.raw_post)
|
Chris@1115
|
92 @attachment.author = User.current
|
Chris@1115
|
93 @attachment.filename = params[:filename].presence || Redmine::Utils.random_hex(16)
|
Chris@1464
|
94 saved = @attachment.save
|
Chris@1115
|
95
|
Chris@1464
|
96 respond_to do |format|
|
Chris@1464
|
97 format.js
|
Chris@1464
|
98 format.api {
|
Chris@1464
|
99 if saved
|
Chris@1464
|
100 render :action => 'upload', :status => :created
|
Chris@1464
|
101 else
|
Chris@1464
|
102 render_validation_errors(@attachment)
|
Chris@1464
|
103 end
|
Chris@1464
|
104 }
|
Chris@1115
|
105 end
|
Chris@1115
|
106 end
|
Chris@1115
|
107
|
Chris@0
|
108 def destroy
|
Chris@1115
|
109 if @attachment.container.respond_to?(:init_journal)
|
Chris@1115
|
110 @attachment.container.init_journal(User.current)
|
Chris@1115
|
111 end
|
Chris@1464
|
112 if @attachment.container
|
Chris@1464
|
113 # Make sure association callbacks are called
|
Chris@1464
|
114 @attachment.container.attachments.delete(@attachment)
|
Chris@1464
|
115 else
|
Chris@1464
|
116 @attachment.destroy
|
Chris@1464
|
117 end
|
Chris@1464
|
118
|
Chris@1464
|
119 respond_to do |format|
|
Chris@1464
|
120 format.html { redirect_to_referer_or project_path(@project) }
|
Chris@1464
|
121 format.js
|
Chris@1464
|
122 end
|
Chris@0
|
123 end
|
Chris@441
|
124
|
chris@257
|
125 def toggle_active
|
chris@257
|
126 @attachment.active = !@attachment.active?
|
chris@257
|
127 @attachment.save!
|
chris@1156
|
128 respond_to do |format|
|
chris@1156
|
129 format.js
|
chris@1156
|
130 end
|
chris@257
|
131 end
|
chris@257
|
132
|
Chris@0
|
133 private
|
Chris@0
|
134 def find_project
|
Chris@0
|
135 @attachment = Attachment.find(params[:id])
|
Chris@0
|
136 # Show 404 if the filename in the url is wrong
|
Chris@0
|
137 raise ActiveRecord::RecordNotFound if params[:filename] && params[:filename] != @attachment.filename
|
Chris@0
|
138 @project = @attachment.project
|
Chris@0
|
139 rescue ActiveRecord::RecordNotFound
|
Chris@0
|
140 render_404
|
Chris@0
|
141 end
|
Chris@441
|
142
|
Chris@0
|
143 # Checks that the file exists and is readable
|
Chris@0
|
144 def file_readable
|
Chris@1464
|
145 if @attachment.readable?
|
Chris@1464
|
146 true
|
Chris@1464
|
147 else
|
Chris@1464
|
148 logger.error "Cannot send attachment, #{@attachment.diskfile} does not exist or is unreadable."
|
Chris@1464
|
149 render_404
|
Chris@1464
|
150 end
|
Chris@0
|
151 end
|
Chris@441
|
152
|
Chris@0
|
153 def read_authorize
|
Chris@0
|
154 @attachment.visible? ? true : deny_access
|
Chris@0
|
155 end
|
Chris@441
|
156
|
Chris@0
|
157 def delete_authorize
|
Chris@0
|
158 @attachment.deletable? ? true : deny_access
|
Chris@0
|
159 end
|
Chris@441
|
160
|
chris@257
|
161 def active_authorize
|
chris@257
|
162 true
|
chris@257
|
163 end
|
chris@257
|
164
|
Chris@0
|
165 def detect_content_type(attachment)
|
Chris@0
|
166 content_type = attachment.content_type
|
Chris@0
|
167 if content_type.blank?
|
Chris@0
|
168 content_type = Redmine::MimeType.of(attachment.filename)
|
Chris@0
|
169 end
|
Chris@0
|
170 content_type.to_s
|
Chris@0
|
171 end
|
Chris@0
|
172 end
|