@@ -1,154 +1,154 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2014 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2014 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, :except => :upload |
|
19 | before_filter :find_project, :except => :upload | |
20 | before_filter :file_readable, :read_authorize, :only => [:show, :download, :thumbnail] |
|
20 | before_filter :file_readable, :read_authorize, :only => [:show, :download, :thumbnail] | |
21 | before_filter :delete_authorize, :only => :destroy |
|
21 | before_filter :delete_authorize, :only => :destroy | |
22 | before_filter :authorize_global, :only => :upload |
|
22 | before_filter :authorize_global, :only => :upload | |
23 |
|
23 | |||
24 | accept_api_auth :show, :download, :upload |
|
24 | accept_api_auth :show, :download, :upload | |
25 |
|
25 | |||
26 | def show |
|
26 | def show | |
27 | respond_to do |format| |
|
27 | respond_to do |format| | |
28 | format.html { |
|
28 | format.html { | |
29 | if @attachment.is_diff? |
|
29 | if @attachment.is_diff? | |
30 | @diff = File.new(@attachment.diskfile, "rb").read |
|
30 | @diff = File.new(@attachment.diskfile, "rb").read | |
31 | @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline' |
|
31 | @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline' | |
32 | @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type) |
|
32 | @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type) | |
33 | # Save diff type as user preference |
|
33 | # Save diff type as user preference | |
34 | if User.current.logged? && @diff_type != User.current.pref[:diff_type] |
|
34 | if User.current.logged? && @diff_type != User.current.pref[:diff_type] | |
35 | User.current.pref[:diff_type] = @diff_type |
|
35 | User.current.pref[:diff_type] = @diff_type | |
36 | User.current.preference.save |
|
36 | User.current.preference.save | |
37 | end |
|
37 | end | |
38 | render :action => 'diff' |
|
38 | render :action => 'diff' | |
39 | elsif @attachment.is_text? && @attachment.filesize <= Setting.file_max_size_displayed.to_i.kilobyte |
|
39 | elsif @attachment.is_text? && @attachment.filesize <= Setting.file_max_size_displayed.to_i.kilobyte | |
40 | @content = File.new(@attachment.diskfile, "rb").read |
|
40 | @content = File.new(@attachment.diskfile, "rb").read | |
41 | render :action => 'file' |
|
41 | render :action => 'file' | |
42 | else |
|
42 | else | |
43 | download |
|
43 | download | |
44 | end |
|
44 | end | |
45 | } |
|
45 | } | |
46 | format.api |
|
46 | format.api | |
47 | end |
|
47 | end | |
48 | end |
|
48 | end | |
49 |
|
49 | |||
50 | def download |
|
50 | def download | |
51 | if @attachment.container.is_a?(Version) || @attachment.container.is_a?(Project) |
|
51 | if @attachment.container.is_a?(Version) || @attachment.container.is_a?(Project) | |
52 | @attachment.increment_download |
|
52 | @attachment.increment_download | |
53 | end |
|
53 | end | |
54 |
|
54 | |||
55 | if stale?(:etag => @attachment.digest) |
|
55 | if stale?(:etag => @attachment.digest) | |
56 | # images are sent inline |
|
56 | # images are sent inline | |
57 | send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename), |
|
57 | send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename), | |
58 | :type => detect_content_type(@attachment), |
|
58 | :type => detect_content_type(@attachment), | |
59 | :disposition => (@attachment.image? ? 'inline' : 'attachment') |
|
59 | :disposition => (@attachment.image? ? 'inline' : 'attachment') | |
60 | end |
|
60 | end | |
61 | end |
|
61 | end | |
62 |
|
62 | |||
63 | def thumbnail |
|
63 | def thumbnail | |
64 |
if @attachment.thumbnailable? && t |
|
64 | if @attachment.thumbnailable? && tbnail = @attachment.thumbnail(:size => params[:size]) | |
65 |
if stale?(:etag => t |
|
65 | if stale?(:etag => tbnail) | |
66 |
send_file t |
|
66 | send_file tbnail, | |
67 | :filename => filename_for_content_disposition(@attachment.filename), |
|
67 | :filename => filename_for_content_disposition(@attachment.filename), | |
68 | :type => detect_content_type(@attachment), |
|
68 | :type => detect_content_type(@attachment), | |
69 | :disposition => 'inline' |
|
69 | :disposition => 'inline' | |
70 | end |
|
70 | end | |
71 | else |
|
71 | else | |
72 | # No thumbnail for the attachment or thumbnail could not be created |
|
72 | # No thumbnail for the attachment or thumbnail could not be created | |
73 | render :nothing => true, :status => 404 |
|
73 | render :nothing => true, :status => 404 | |
74 | end |
|
74 | end | |
75 | end |
|
75 | end | |
76 |
|
76 | |||
77 | def upload |
|
77 | def upload | |
78 | # Make sure that API users get used to set this content type |
|
78 | # Make sure that API users get used to set this content type | |
79 | # as it won't trigger Rails' automatic parsing of the request body for parameters |
|
79 | # as it won't trigger Rails' automatic parsing of the request body for parameters | |
80 | unless request.content_type == 'application/octet-stream' |
|
80 | unless request.content_type == 'application/octet-stream' | |
81 | render :nothing => true, :status => 406 |
|
81 | render :nothing => true, :status => 406 | |
82 | return |
|
82 | return | |
83 | end |
|
83 | end | |
84 |
|
84 | |||
85 | @attachment = Attachment.new(:file => request.raw_post) |
|
85 | @attachment = Attachment.new(:file => request.raw_post) | |
86 | @attachment.author = User.current |
|
86 | @attachment.author = User.current | |
87 | @attachment.filename = params[:filename].presence || Redmine::Utils.random_hex(16) |
|
87 | @attachment.filename = params[:filename].presence || Redmine::Utils.random_hex(16) | |
88 | saved = @attachment.save |
|
88 | saved = @attachment.save | |
89 |
|
89 | |||
90 | respond_to do |format| |
|
90 | respond_to do |format| | |
91 | format.js |
|
91 | format.js | |
92 | format.api { |
|
92 | format.api { | |
93 | if saved |
|
93 | if saved | |
94 | render :action => 'upload', :status => :created |
|
94 | render :action => 'upload', :status => :created | |
95 | else |
|
95 | else | |
96 | render_validation_errors(@attachment) |
|
96 | render_validation_errors(@attachment) | |
97 | end |
|
97 | end | |
98 | } |
|
98 | } | |
99 | end |
|
99 | end | |
100 | end |
|
100 | end | |
101 |
|
101 | |||
102 | def destroy |
|
102 | def destroy | |
103 | if @attachment.container.respond_to?(:init_journal) |
|
103 | if @attachment.container.respond_to?(:init_journal) | |
104 | @attachment.container.init_journal(User.current) |
|
104 | @attachment.container.init_journal(User.current) | |
105 | end |
|
105 | end | |
106 | if @attachment.container |
|
106 | if @attachment.container | |
107 | # Make sure association callbacks are called |
|
107 | # Make sure association callbacks are called | |
108 | @attachment.container.attachments.delete(@attachment) |
|
108 | @attachment.container.attachments.delete(@attachment) | |
109 | else |
|
109 | else | |
110 | @attachment.destroy |
|
110 | @attachment.destroy | |
111 | end |
|
111 | end | |
112 |
|
112 | |||
113 | respond_to do |format| |
|
113 | respond_to do |format| | |
114 | format.html { redirect_to_referer_or project_path(@project) } |
|
114 | format.html { redirect_to_referer_or project_path(@project) } | |
115 | format.js |
|
115 | format.js | |
116 | end |
|
116 | end | |
117 | end |
|
117 | end | |
118 |
|
118 | |||
119 | private |
|
119 | private | |
120 | def find_project |
|
120 | def find_project | |
121 | @attachment = Attachment.find(params[:id]) |
|
121 | @attachment = Attachment.find(params[:id]) | |
122 | # Show 404 if the filename in the url is wrong |
|
122 | # Show 404 if the filename in the url is wrong | |
123 | raise ActiveRecord::RecordNotFound if params[:filename] && params[:filename] != @attachment.filename |
|
123 | raise ActiveRecord::RecordNotFound if params[:filename] && params[:filename] != @attachment.filename | |
124 | @project = @attachment.project |
|
124 | @project = @attachment.project | |
125 | rescue ActiveRecord::RecordNotFound |
|
125 | rescue ActiveRecord::RecordNotFound | |
126 | render_404 |
|
126 | render_404 | |
127 | end |
|
127 | end | |
128 |
|
128 | |||
129 | # Checks that the file exists and is readable |
|
129 | # Checks that the file exists and is readable | |
130 | def file_readable |
|
130 | def file_readable | |
131 | if @attachment.readable? |
|
131 | if @attachment.readable? | |
132 | true |
|
132 | true | |
133 | else |
|
133 | else | |
134 | logger.error "Cannot send attachment, #{@attachment.diskfile} does not exist or is unreadable." |
|
134 | logger.error "Cannot send attachment, #{@attachment.diskfile} does not exist or is unreadable." | |
135 | render_404 |
|
135 | render_404 | |
136 | end |
|
136 | end | |
137 | end |
|
137 | end | |
138 |
|
138 | |||
139 | def read_authorize |
|
139 | def read_authorize | |
140 | @attachment.visible? ? true : deny_access |
|
140 | @attachment.visible? ? true : deny_access | |
141 | end |
|
141 | end | |
142 |
|
142 | |||
143 | def delete_authorize |
|
143 | def delete_authorize | |
144 | @attachment.deletable? ? true : deny_access |
|
144 | @attachment.deletable? ? true : deny_access | |
145 | end |
|
145 | end | |
146 |
|
146 | |||
147 | def detect_content_type(attachment) |
|
147 | def detect_content_type(attachment) | |
148 | content_type = attachment.content_type |
|
148 | content_type = attachment.content_type | |
149 | if content_type.blank? |
|
149 | if content_type.blank? | |
150 | content_type = Redmine::MimeType.of(attachment.filename) |
|
150 | content_type = Redmine::MimeType.of(attachment.filename) | |
151 | end |
|
151 | end | |
152 | content_type.to_s |
|
152 | content_type.to_s | |
153 | end |
|
153 | end | |
154 | end |
|
154 | end |
General Comments 0
You need to be logged in to leave comments.
Login now