##// END OF EJS Templates
Adds Etags on attachments....
Jean-Philippe Lang -
r9780:2dbabde7f40d
parent child
Show More
@@ -1,136 +1,139
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class AttachmentsController < ApplicationController
19 19 before_filter :find_project, :except => :upload
20 20 before_filter :file_readable, :read_authorize, :only => [:show, :download, :thumbnail]
21 21 before_filter :delete_authorize, :only => :destroy
22 22 before_filter :authorize_global, :only => :upload
23 23
24 24 accept_api_auth :show, :download, :upload
25 25
26 26 def show
27 27 respond_to do |format|
28 28 format.html {
29 29 if @attachment.is_diff?
30 30 @diff = File.new(@attachment.diskfile, "rb").read
31 31 @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline'
32 32 @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type)
33 33 # Save diff type as user preference
34 34 if User.current.logged? && @diff_type != User.current.pref[:diff_type]
35 35 User.current.pref[:diff_type] = @diff_type
36 36 User.current.preference.save
37 37 end
38 38 render :action => 'diff'
39 39 elsif @attachment.is_text? && @attachment.filesize <= Setting.file_max_size_displayed.to_i.kilobyte
40 40 @content = File.new(@attachment.diskfile, "rb").read
41 41 render :action => 'file'
42 42 else
43 43 download
44 44 end
45 45 }
46 46 format.api
47 47 end
48 48 end
49 49
50 50 def download
51 51 if @attachment.container.is_a?(Version) || @attachment.container.is_a?(Project)
52 52 @attachment.increment_download
53 53 end
54 54
55 if stale?(:etag => @attachment.digest)
55 56 # images are sent inline
56 57 send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename),
57 58 :type => detect_content_type(@attachment),
58 59 :disposition => (@attachment.image? ? 'inline' : 'attachment')
59
60 end
60 61 end
61 62
62 63 def thumbnail
63 64 if @attachment.thumbnailable? && Setting.thumbnails_enabled? && thumbnail = @attachment.thumbnail
65 if stale?(:etag => thumbnail)
64 66 send_file thumbnail,
65 67 :filename => filename_for_content_disposition(@attachment.filename),
66 68 :type => detect_content_type(@attachment),
67 69 :disposition => 'inline'
70 end
68 71 else
69 72 # No thumbnail for the attachment or thumbnail could not be created
70 73 render :nothing => true, :status => 404
71 74 end
72 75 end
73 76
74 77 def upload
75 78 # Make sure that API users get used to set this content type
76 79 # as it won't trigger Rails' automatic parsing of the request body for parameters
77 80 unless request.content_type == 'application/octet-stream'
78 81 render :nothing => true, :status => 406
79 82 return
80 83 end
81 84
82 85 @attachment = Attachment.new(:file => request.raw_post)
83 86 @attachment.author = User.current
84 87 @attachment.filename = Redmine::Utils.random_hex(16)
85 88
86 89 if @attachment.save
87 90 respond_to do |format|
88 91 format.api { render :action => 'upload', :status => :created }
89 92 end
90 93 else
91 94 respond_to do |format|
92 95 format.api { render_validation_errors(@attachment) }
93 96 end
94 97 end
95 98 end
96 99
97 100 def destroy
98 101 if @attachment.container.respond_to?(:init_journal)
99 102 @attachment.container.init_journal(User.current)
100 103 end
101 104 # Make sure association callbacks are called
102 105 @attachment.container.attachments.delete(@attachment)
103 106 redirect_to_referer_or project_path(@project)
104 107 end
105 108
106 109 private
107 110 def find_project
108 111 @attachment = Attachment.find(params[:id])
109 112 # Show 404 if the filename in the url is wrong
110 113 raise ActiveRecord::RecordNotFound if params[:filename] && params[:filename] != @attachment.filename
111 114 @project = @attachment.project
112 115 rescue ActiveRecord::RecordNotFound
113 116 render_404
114 117 end
115 118
116 119 # Checks that the file exists and is readable
117 120 def file_readable
118 121 @attachment.readable? ? true : render_404
119 122 end
120 123
121 124 def read_authorize
122 125 @attachment.visible? ? true : deny_access
123 126 end
124 127
125 128 def delete_authorize
126 129 @attachment.deletable? ? true : deny_access
127 130 end
128 131
129 132 def detect_content_type(attachment)
130 133 content_type = attachment.content_type
131 134 if content_type.blank?
132 135 content_type = Redmine::MimeType.of(attachment.filename)
133 136 end
134 137 content_type.to_s
135 138 end
136 139 end
General Comments 0
You need to be logged in to leave comments. Login now