@@ -1,206 +1,206 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2016 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_attachment, :only => [:show, :download, :thumbnail, :destroy] |
|
20 | 20 | before_filter :find_editable_attachments, :only => [:edit, :update] |
|
21 | 21 | before_filter :file_readable, :read_authorize, :only => [:show, :download, :thumbnail] |
|
22 | 22 | before_filter :delete_authorize, :only => :destroy |
|
23 | 23 | before_filter :authorize_global, :only => :upload |
|
24 | 24 | |
|
25 | 25 | # Disable check for same origin requests for JS files, i.e. attachments with |
|
26 | 26 | # MIME type text/javascript. |
|
27 | 27 | skip_after_filter :verify_same_origin_request, :only => :download |
|
28 | 28 | |
|
29 | 29 | accept_api_auth :show, :download, :thumbnail, :upload, :destroy |
|
30 | 30 | |
|
31 | 31 | def show |
|
32 | 32 | respond_to do |format| |
|
33 | 33 | format.html { |
|
34 | 34 | if @attachment.is_diff? |
|
35 | 35 | @diff = File.read(@attachment.diskfile, :mode => "rb") |
|
36 | 36 | @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline' |
|
37 | 37 | @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type) |
|
38 | 38 | # Save diff type as user preference |
|
39 | 39 | if User.current.logged? && @diff_type != User.current.pref[:diff_type] |
|
40 | 40 | User.current.pref[:diff_type] = @diff_type |
|
41 | 41 | User.current.preference.save |
|
42 | 42 | end |
|
43 | 43 | render :action => 'diff' |
|
44 | 44 | elsif @attachment.is_text? && @attachment.filesize <= Setting.file_max_size_displayed.to_i.kilobyte |
|
45 | 45 | @content = File.read(@attachment.diskfile, :mode => "rb") |
|
46 | 46 | render :action => 'file' |
|
47 | 47 | elsif @attachment.is_image? |
|
48 | 48 | render :action => 'image' |
|
49 | 49 | else |
|
50 | 50 | render :action => 'other' |
|
51 | 51 | end |
|
52 | 52 | } |
|
53 | 53 | format.api |
|
54 | 54 | end |
|
55 | 55 | end |
|
56 | 56 | |
|
57 | 57 | def download |
|
58 | 58 | if @attachment.container.is_a?(Version) || @attachment.container.is_a?(Project) |
|
59 | 59 | @attachment.increment_download |
|
60 | 60 | end |
|
61 | 61 | |
|
62 | 62 | if stale?(:etag => @attachment.digest) |
|
63 | 63 | # images are sent inline |
|
64 | 64 | send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename), |
|
65 | 65 | :type => detect_content_type(@attachment), |
|
66 | 66 | :disposition => disposition(@attachment) |
|
67 | 67 | end |
|
68 | 68 | end |
|
69 | 69 | |
|
70 | 70 | def thumbnail |
|
71 | 71 | if @attachment.thumbnailable? && tbnail = @attachment.thumbnail(:size => params[:size]) |
|
72 | 72 | if stale?(:etag => tbnail) |
|
73 | 73 | send_file tbnail, |
|
74 | 74 | :filename => filename_for_content_disposition(@attachment.filename), |
|
75 | 75 | :type => detect_content_type(@attachment), |
|
76 | 76 | :disposition => 'inline' |
|
77 | 77 | end |
|
78 | 78 | else |
|
79 | 79 | # No thumbnail for the attachment or thumbnail could not be created |
|
80 | 80 | render :nothing => true, :status => 404 |
|
81 | 81 | end |
|
82 | 82 | end |
|
83 | 83 | |
|
84 | 84 | def upload |
|
85 | 85 | # Make sure that API users get used to set this content type |
|
86 | 86 | # as it won't trigger Rails' automatic parsing of the request body for parameters |
|
87 | 87 | unless request.content_type == 'application/octet-stream' |
|
88 | 88 | render :nothing => true, :status => 406 |
|
89 | 89 | return |
|
90 | 90 | end |
|
91 | 91 | |
|
92 | 92 | @attachment = Attachment.new(:file => request.raw_post) |
|
93 | 93 | @attachment.author = User.current |
|
94 | 94 | @attachment.filename = params[:filename].presence || Redmine::Utils.random_hex(16) |
|
95 | 95 | @attachment.content_type = params[:content_type].presence |
|
96 | 96 | saved = @attachment.save |
|
97 | 97 | |
|
98 | 98 | respond_to do |format| |
|
99 | 99 | format.js |
|
100 | 100 | format.api { |
|
101 | 101 | if saved |
|
102 | 102 | render :action => 'upload', :status => :created |
|
103 | 103 | else |
|
104 | 104 | render_validation_errors(@attachment) |
|
105 | 105 | end |
|
106 | 106 | } |
|
107 | 107 | end |
|
108 | 108 | end |
|
109 | 109 | |
|
110 | 110 | def edit |
|
111 | 111 | end |
|
112 | 112 | |
|
113 | 113 | def update |
|
114 | 114 | if params[:attachments].is_a?(Hash) |
|
115 | 115 | if Attachment.update_attachments(@attachments, params[:attachments]) |
|
116 | 116 | redirect_back_or_default home_path |
|
117 | 117 | return |
|
118 | 118 | end |
|
119 | 119 | end |
|
120 | 120 | render :action => 'edit' |
|
121 | 121 | end |
|
122 | 122 | |
|
123 | 123 | def destroy |
|
124 | 124 | if @attachment.container.respond_to?(:init_journal) |
|
125 | 125 | @attachment.container.init_journal(User.current) |
|
126 | 126 | end |
|
127 | 127 | if @attachment.container |
|
128 | 128 | # Make sure association callbacks are called |
|
129 | 129 | @attachment.container.attachments.delete(@attachment) |
|
130 | 130 | else |
|
131 | 131 | @attachment.destroy |
|
132 | 132 | end |
|
133 | 133 | |
|
134 | 134 | respond_to do |format| |
|
135 | 135 | format.html { redirect_to_referer_or project_path(@project) } |
|
136 | 136 | format.js |
|
137 | 137 | format.api { render_api_ok } |
|
138 | 138 | end |
|
139 | 139 | end |
|
140 | 140 | |
|
141 | 141 | private |
|
142 | 142 | |
|
143 | 143 | def find_attachment |
|
144 | 144 | @attachment = Attachment.find(params[:id]) |
|
145 | 145 | # Show 404 if the filename in the url is wrong |
|
146 | 146 | raise ActiveRecord::RecordNotFound if params[:filename] && params[:filename] != @attachment.filename |
|
147 | 147 | @project = @attachment.project |
|
148 | 148 | rescue ActiveRecord::RecordNotFound |
|
149 | 149 | render_404 |
|
150 | 150 | end |
|
151 | 151 | |
|
152 | 152 | def find_editable_attachments |
|
153 | 153 | klass = params[:object_type].to_s.singularize.classify.constantize rescue nil |
|
154 | 154 | unless klass && klass.reflect_on_association(:attachments) |
|
155 | 155 | render_404 |
|
156 | 156 | return |
|
157 | 157 | end |
|
158 | 158 | |
|
159 | 159 | @container = klass.find(params[:object_id]) |
|
160 | 160 | if @container.respond_to?(:visible?) && !@container.visible? |
|
161 | 161 | render_403 |
|
162 | 162 | return |
|
163 | 163 | end |
|
164 | 164 | @attachments = @container.attachments.select(&:editable?) |
|
165 | 165 | if @container.respond_to?(:project) |
|
166 | 166 | @project = @container.project |
|
167 | 167 | end |
|
168 | 168 | render_404 if @attachments.empty? |
|
169 | 169 | rescue ActiveRecord::RecordNotFound |
|
170 | 170 | render_404 |
|
171 | 171 | end |
|
172 | 172 | |
|
173 | 173 | # Checks that the file exists and is readable |
|
174 | 174 | def file_readable |
|
175 | 175 | if @attachment.readable? |
|
176 | 176 | true |
|
177 | 177 | else |
|
178 | 178 | logger.error "Cannot send attachment, #{@attachment.diskfile} does not exist or is unreadable." |
|
179 | 179 | render_404 |
|
180 | 180 | end |
|
181 | 181 | end |
|
182 | 182 | |
|
183 | 183 | def read_authorize |
|
184 | 184 | @attachment.visible? ? true : deny_access |
|
185 | 185 | end |
|
186 | 186 | |
|
187 | 187 | def delete_authorize |
|
188 | 188 | @attachment.deletable? ? true : deny_access |
|
189 | 189 | end |
|
190 | 190 | |
|
191 | 191 | def detect_content_type(attachment) |
|
192 | 192 | content_type = attachment.content_type |
|
193 | 193 | if content_type.blank? || content_type == "application/octet-stream" |
|
194 | 194 | content_type = Redmine::MimeType.of(attachment.filename) |
|
195 | 195 | end |
|
196 | 196 | content_type.to_s |
|
197 | 197 | end |
|
198 | 198 | |
|
199 | 199 | def disposition(attachment) |
|
200 |
if attachment. |
|
|
200 | if attachment.is_pdf? | |
|
201 | 201 | 'inline' |
|
202 | 202 | else |
|
203 | 203 | 'attachment' |
|
204 | 204 | end |
|
205 | 205 | end |
|
206 | 206 | end |
@@ -1,452 +1,452 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2016 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 | require 'SVG/Graph/Bar' |
|
19 | 19 | require 'SVG/Graph/BarHorizontal' |
|
20 | 20 | require 'digest/sha1' |
|
21 | 21 | require 'redmine/scm/adapters' |
|
22 | 22 | |
|
23 | 23 | class ChangesetNotFound < Exception; end |
|
24 | 24 | class InvalidRevisionParam < Exception; end |
|
25 | 25 | |
|
26 | 26 | class RepositoriesController < ApplicationController |
|
27 | 27 | menu_item :repository |
|
28 | 28 | menu_item :settings, :only => [:new, :create, :edit, :update, :destroy, :committers] |
|
29 | 29 | default_search_scope :changesets |
|
30 | 30 | |
|
31 | 31 | before_filter :find_project_by_project_id, :only => [:new, :create] |
|
32 | 32 | before_filter :find_repository, :only => [:edit, :update, :destroy, :committers] |
|
33 | 33 | before_filter :find_project_repository, :except => [:new, :create, :edit, :update, :destroy, :committers] |
|
34 | 34 | before_filter :find_changeset, :only => [:revision, :add_related_issue, :remove_related_issue] |
|
35 | 35 | before_filter :authorize |
|
36 | 36 | accept_rss_auth :revisions |
|
37 | 37 | |
|
38 | 38 | rescue_from Redmine::Scm::Adapters::CommandFailed, :with => :show_error_command_failed |
|
39 | 39 | |
|
40 | 40 | def new |
|
41 | 41 | scm = params[:repository_scm] || (Redmine::Scm::Base.all & Setting.enabled_scm).first |
|
42 | 42 | @repository = Repository.factory(scm) |
|
43 | 43 | @repository.is_default = @project.repository.nil? |
|
44 | 44 | @repository.project = @project |
|
45 | 45 | end |
|
46 | 46 | |
|
47 | 47 | def create |
|
48 | 48 | attrs = pickup_extra_info |
|
49 | 49 | @repository = Repository.factory(params[:repository_scm]) |
|
50 | 50 | @repository.safe_attributes = params[:repository] |
|
51 | 51 | if attrs[:attrs_extra].keys.any? |
|
52 | 52 | @repository.merge_extra_info(attrs[:attrs_extra]) |
|
53 | 53 | end |
|
54 | 54 | @repository.project = @project |
|
55 | 55 | if request.post? && @repository.save |
|
56 | 56 | redirect_to settings_project_path(@project, :tab => 'repositories') |
|
57 | 57 | else |
|
58 | 58 | render :action => 'new' |
|
59 | 59 | end |
|
60 | 60 | end |
|
61 | 61 | |
|
62 | 62 | def edit |
|
63 | 63 | end |
|
64 | 64 | |
|
65 | 65 | def update |
|
66 | 66 | attrs = pickup_extra_info |
|
67 | 67 | @repository.safe_attributes = attrs[:attrs] |
|
68 | 68 | if attrs[:attrs_extra].keys.any? |
|
69 | 69 | @repository.merge_extra_info(attrs[:attrs_extra]) |
|
70 | 70 | end |
|
71 | 71 | @repository.project = @project |
|
72 | 72 | if @repository.save |
|
73 | 73 | redirect_to settings_project_path(@project, :tab => 'repositories') |
|
74 | 74 | else |
|
75 | 75 | render :action => 'edit' |
|
76 | 76 | end |
|
77 | 77 | end |
|
78 | 78 | |
|
79 | 79 | def pickup_extra_info |
|
80 | 80 | p = {} |
|
81 | 81 | p_extra = {} |
|
82 | 82 | params[:repository].each do |k, v| |
|
83 | 83 | if k =~ /^extra_/ |
|
84 | 84 | p_extra[k] = v |
|
85 | 85 | else |
|
86 | 86 | p[k] = v |
|
87 | 87 | end |
|
88 | 88 | end |
|
89 | 89 | {:attrs => p, :attrs_extra => p_extra} |
|
90 | 90 | end |
|
91 | 91 | private :pickup_extra_info |
|
92 | 92 | |
|
93 | 93 | def committers |
|
94 | 94 | @committers = @repository.committers |
|
95 | 95 | @users = @project.users.to_a |
|
96 | 96 | additional_user_ids = @committers.collect(&:last).collect(&:to_i) - @users.collect(&:id) |
|
97 | 97 | @users += User.where(:id => additional_user_ids).to_a unless additional_user_ids.empty? |
|
98 | 98 | @users.compact! |
|
99 | 99 | @users.sort! |
|
100 | 100 | if request.post? && params[:committers].is_a?(Hash) |
|
101 | 101 | # Build a hash with repository usernames as keys and corresponding user ids as values |
|
102 | 102 | @repository.committer_ids = params[:committers].values.inject({}) {|h, c| h[c.first] = c.last; h} |
|
103 | 103 | flash[:notice] = l(:notice_successful_update) |
|
104 | 104 | redirect_to settings_project_path(@project, :tab => 'repositories') |
|
105 | 105 | end |
|
106 | 106 | end |
|
107 | 107 | |
|
108 | 108 | def destroy |
|
109 | 109 | @repository.destroy if request.delete? |
|
110 | 110 | redirect_to settings_project_path(@project, :tab => 'repositories') |
|
111 | 111 | end |
|
112 | 112 | |
|
113 | 113 | def show |
|
114 | 114 | @repository.fetch_changesets if @project.active? && Setting.autofetch_changesets? && @path.empty? |
|
115 | 115 | |
|
116 | 116 | @entries = @repository.entries(@path, @rev) |
|
117 | 117 | @changeset = @repository.find_changeset_by_name(@rev) |
|
118 | 118 | if request.xhr? |
|
119 | 119 | @entries ? render(:partial => 'dir_list_content') : render(:nothing => true) |
|
120 | 120 | else |
|
121 | 121 | (show_error_not_found; return) unless @entries |
|
122 | 122 | @changesets = @repository.latest_changesets(@path, @rev) |
|
123 | 123 | @properties = @repository.properties(@path, @rev) |
|
124 | 124 | @repositories = @project.repositories |
|
125 | 125 | render :action => 'show' |
|
126 | 126 | end |
|
127 | 127 | end |
|
128 | 128 | |
|
129 | 129 | alias_method :browse, :show |
|
130 | 130 | |
|
131 | 131 | def changes |
|
132 | 132 | @entry = @repository.entry(@path, @rev) |
|
133 | 133 | (show_error_not_found; return) unless @entry |
|
134 | 134 | @changesets = @repository.latest_changesets(@path, @rev, Setting.repository_log_display_limit.to_i) |
|
135 | 135 | @properties = @repository.properties(@path, @rev) |
|
136 | 136 | @changeset = @repository.find_changeset_by_name(@rev) |
|
137 | 137 | end |
|
138 | 138 | |
|
139 | 139 | def revisions |
|
140 | 140 | @changeset_count = @repository.changesets.count |
|
141 | 141 | @changeset_pages = Paginator.new @changeset_count, |
|
142 | 142 | per_page_option, |
|
143 | 143 | params['page'] |
|
144 | 144 | @changesets = @repository.changesets. |
|
145 | 145 | limit(@changeset_pages.per_page). |
|
146 | 146 | offset(@changeset_pages.offset). |
|
147 | 147 | includes(:user, :repository, :parents). |
|
148 | 148 | to_a |
|
149 | 149 | |
|
150 | 150 | respond_to do |format| |
|
151 | 151 | format.html { render :layout => false if request.xhr? } |
|
152 | 152 | format.atom { render_feed(@changesets, :title => "#{@project.name}: #{l(:label_revision_plural)}") } |
|
153 | 153 | end |
|
154 | 154 | end |
|
155 | 155 | |
|
156 | 156 | def raw |
|
157 | 157 | entry_and_raw(true) |
|
158 | 158 | end |
|
159 | 159 | |
|
160 | 160 | def entry |
|
161 | 161 | entry_and_raw(false) |
|
162 | 162 | end |
|
163 | 163 | |
|
164 | 164 | def entry_and_raw(is_raw) |
|
165 | 165 | @entry = @repository.entry(@path, @rev) |
|
166 | 166 | (show_error_not_found; return) unless @entry |
|
167 | 167 | |
|
168 | 168 | # If the entry is a dir, show the browser |
|
169 | 169 | (show; return) if @entry.is_dir? |
|
170 | 170 | |
|
171 | 171 | if is_raw |
|
172 | 172 | # Force the download |
|
173 | 173 | send_opt = { :filename => filename_for_content_disposition(@path.split('/').last) } |
|
174 | 174 | send_type = Redmine::MimeType.of(@path) |
|
175 | 175 | send_opt[:type] = send_type.to_s if send_type |
|
176 | 176 | send_opt[:disposition] = disposition(@path) |
|
177 | 177 | send_data @repository.cat(@path, @rev), send_opt |
|
178 | 178 | else |
|
179 | 179 | if !@entry.size || @entry.size <= Setting.file_max_size_displayed.to_i.kilobyte |
|
180 | 180 | content = @repository.cat(@path, @rev) |
|
181 | 181 | (show_error_not_found; return) unless content |
|
182 | 182 | |
|
183 | 183 | if content.size <= Setting.file_max_size_displayed.to_i.kilobyte && |
|
184 | 184 | is_entry_text_data?(content, @path) |
|
185 | 185 | # TODO: UTF-16 |
|
186 | 186 | # Prevent empty lines when displaying a file with Windows style eol |
|
187 | 187 | # Is this needed? AttachmentsController simply reads file. |
|
188 | 188 | @content = content.gsub("\r\n", "\n") |
|
189 | 189 | end |
|
190 | 190 | end |
|
191 | 191 | @changeset = @repository.find_changeset_by_name(@rev) |
|
192 | 192 | end |
|
193 | 193 | end |
|
194 | 194 | private :entry_and_raw |
|
195 | 195 | |
|
196 | 196 | def is_entry_text_data?(ent, path) |
|
197 | 197 | # UTF-16 contains "\x00". |
|
198 | 198 | # It is very strict that file contains less than 30% of ascii symbols |
|
199 | 199 | # in non Western Europe. |
|
200 | 200 | return true if Redmine::MimeType.is_type?('text', path) |
|
201 | 201 | # Ruby 1.8.6 has a bug of integer divisions. |
|
202 | 202 | # http://apidock.com/ruby/v1_8_6_287/String/is_binary_data%3F |
|
203 | 203 | return false if ent.is_binary_data? |
|
204 | 204 | true |
|
205 | 205 | end |
|
206 | 206 | private :is_entry_text_data? |
|
207 | 207 | |
|
208 | 208 | def annotate |
|
209 | 209 | @entry = @repository.entry(@path, @rev) |
|
210 | 210 | (show_error_not_found; return) unless @entry |
|
211 | 211 | |
|
212 | 212 | @annotate = @repository.scm.annotate(@path, @rev) |
|
213 | 213 | if @annotate.nil? || @annotate.empty? |
|
214 | 214 | (render_error l(:error_scm_annotate); return) |
|
215 | 215 | end |
|
216 | 216 | ann_buf_size = 0 |
|
217 | 217 | @annotate.lines.each do |buf| |
|
218 | 218 | ann_buf_size += buf.size |
|
219 | 219 | end |
|
220 | 220 | if ann_buf_size > Setting.file_max_size_displayed.to_i.kilobyte |
|
221 | 221 | (render_error l(:error_scm_annotate_big_text_file); return) |
|
222 | 222 | end |
|
223 | 223 | @changeset = @repository.find_changeset_by_name(@rev) |
|
224 | 224 | end |
|
225 | 225 | |
|
226 | 226 | def revision |
|
227 | 227 | respond_to do |format| |
|
228 | 228 | format.html |
|
229 | 229 | format.js {render :layout => false} |
|
230 | 230 | end |
|
231 | 231 | end |
|
232 | 232 | |
|
233 | 233 | # Adds a related issue to a changeset |
|
234 | 234 | # POST /projects/:project_id/repository/(:repository_id/)revisions/:rev/issues |
|
235 | 235 | def add_related_issue |
|
236 | 236 | issue_id = params[:issue_id].to_s.sub(/^#/,'') |
|
237 | 237 | @issue = @changeset.find_referenced_issue_by_id(issue_id) |
|
238 | 238 | if @issue && (!@issue.visible? || @changeset.issues.include?(@issue)) |
|
239 | 239 | @issue = nil |
|
240 | 240 | end |
|
241 | 241 | |
|
242 | 242 | if @issue |
|
243 | 243 | @changeset.issues << @issue |
|
244 | 244 | end |
|
245 | 245 | end |
|
246 | 246 | |
|
247 | 247 | # Removes a related issue from a changeset |
|
248 | 248 | # DELETE /projects/:project_id/repository/(:repository_id/)revisions/:rev/issues/:issue_id |
|
249 | 249 | def remove_related_issue |
|
250 | 250 | @issue = Issue.visible.find_by_id(params[:issue_id]) |
|
251 | 251 | if @issue |
|
252 | 252 | @changeset.issues.delete(@issue) |
|
253 | 253 | end |
|
254 | 254 | end |
|
255 | 255 | |
|
256 | 256 | def diff |
|
257 | 257 | if params[:format] == 'diff' |
|
258 | 258 | @diff = @repository.diff(@path, @rev, @rev_to) |
|
259 | 259 | (show_error_not_found; return) unless @diff |
|
260 | 260 | filename = "changeset_r#{@rev}" |
|
261 | 261 | filename << "_r#{@rev_to}" if @rev_to |
|
262 | 262 | send_data @diff.join, :filename => "#{filename}.diff", |
|
263 | 263 | :type => 'text/x-patch', |
|
264 | 264 | :disposition => 'attachment' |
|
265 | 265 | else |
|
266 | 266 | @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline' |
|
267 | 267 | @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type) |
|
268 | 268 | |
|
269 | 269 | # Save diff type as user preference |
|
270 | 270 | if User.current.logged? && @diff_type != User.current.pref[:diff_type] |
|
271 | 271 | User.current.pref[:diff_type] = @diff_type |
|
272 | 272 | User.current.preference.save |
|
273 | 273 | end |
|
274 | 274 | @cache_key = "repositories/diff/#{@repository.id}/" + |
|
275 | 275 | Digest::MD5.hexdigest("#{@path}-#{@rev}-#{@rev_to}-#{@diff_type}-#{current_language}") |
|
276 | 276 | unless read_fragment(@cache_key) |
|
277 | 277 | @diff = @repository.diff(@path, @rev, @rev_to) |
|
278 | 278 | show_error_not_found unless @diff |
|
279 | 279 | end |
|
280 | 280 | |
|
281 | 281 | @changeset = @repository.find_changeset_by_name(@rev) |
|
282 | 282 | @changeset_to = @rev_to ? @repository.find_changeset_by_name(@rev_to) : nil |
|
283 | 283 | @diff_format_revisions = @repository.diff_format_revisions(@changeset, @changeset_to) |
|
284 | 284 | end |
|
285 | 285 | end |
|
286 | 286 | |
|
287 | 287 | def stats |
|
288 | 288 | end |
|
289 | 289 | |
|
290 | 290 | def graph |
|
291 | 291 | data = nil |
|
292 | 292 | case params[:graph] |
|
293 | 293 | when "commits_per_month" |
|
294 | 294 | data = graph_commits_per_month(@repository) |
|
295 | 295 | when "commits_per_author" |
|
296 | 296 | data = graph_commits_per_author(@repository) |
|
297 | 297 | end |
|
298 | 298 | if data |
|
299 | 299 | headers["Content-Type"] = "image/svg+xml" |
|
300 | 300 | send_data(data, :type => "image/svg+xml", :disposition => "inline") |
|
301 | 301 | else |
|
302 | 302 | render_404 |
|
303 | 303 | end |
|
304 | 304 | end |
|
305 | 305 | |
|
306 | 306 | private |
|
307 | 307 | |
|
308 | 308 | def find_repository |
|
309 | 309 | @repository = Repository.find(params[:id]) |
|
310 | 310 | @project = @repository.project |
|
311 | 311 | rescue ActiveRecord::RecordNotFound |
|
312 | 312 | render_404 |
|
313 | 313 | end |
|
314 | 314 | |
|
315 | 315 | REV_PARAM_RE = %r{\A[a-f0-9]*\Z}i |
|
316 | 316 | |
|
317 | 317 | def find_project_repository |
|
318 | 318 | @project = Project.find(params[:id]) |
|
319 | 319 | if params[:repository_id].present? |
|
320 | 320 | @repository = @project.repositories.find_by_identifier_param(params[:repository_id]) |
|
321 | 321 | else |
|
322 | 322 | @repository = @project.repository |
|
323 | 323 | end |
|
324 | 324 | (render_404; return false) unless @repository |
|
325 | 325 | @path = params[:path].is_a?(Array) ? params[:path].join('/') : params[:path].to_s |
|
326 | 326 | @rev = params[:rev].blank? ? @repository.default_branch : params[:rev].to_s.strip |
|
327 | 327 | @rev_to = params[:rev_to] |
|
328 | 328 | |
|
329 | 329 | unless @rev.to_s.match(REV_PARAM_RE) && @rev_to.to_s.match(REV_PARAM_RE) |
|
330 | 330 | if @repository.branches.blank? |
|
331 | 331 | raise InvalidRevisionParam |
|
332 | 332 | end |
|
333 | 333 | end |
|
334 | 334 | rescue ActiveRecord::RecordNotFound |
|
335 | 335 | render_404 |
|
336 | 336 | rescue InvalidRevisionParam |
|
337 | 337 | show_error_not_found |
|
338 | 338 | end |
|
339 | 339 | |
|
340 | 340 | def find_changeset |
|
341 | 341 | if @rev.present? |
|
342 | 342 | @changeset = @repository.find_changeset_by_name(@rev) |
|
343 | 343 | end |
|
344 | 344 | show_error_not_found unless @changeset |
|
345 | 345 | end |
|
346 | 346 | |
|
347 | 347 | def show_error_not_found |
|
348 | 348 | render_error :message => l(:error_scm_not_found), :status => 404 |
|
349 | 349 | end |
|
350 | 350 | |
|
351 | 351 | # Handler for Redmine::Scm::Adapters::CommandFailed exception |
|
352 | 352 | def show_error_command_failed(exception) |
|
353 | 353 | render_error l(:error_scm_command_failed, exception.message) |
|
354 | 354 | end |
|
355 | 355 | |
|
356 | 356 | def graph_commits_per_month(repository) |
|
357 | 357 | @date_to = User.current.today |
|
358 | 358 | @date_from = @date_to << 11 |
|
359 | 359 | @date_from = Date.civil(@date_from.year, @date_from.month, 1) |
|
360 | 360 | commits_by_day = Changeset. |
|
361 | 361 | where("repository_id = ? AND commit_date BETWEEN ? AND ?", repository.id, @date_from, @date_to). |
|
362 | 362 | group(:commit_date). |
|
363 | 363 | count |
|
364 | 364 | commits_by_month = [0] * 12 |
|
365 | 365 | commits_by_day.each {|c| commits_by_month[(@date_to.month - c.first.to_date.month) % 12] += c.last } |
|
366 | 366 | |
|
367 | 367 | changes_by_day = Change. |
|
368 | 368 | joins(:changeset). |
|
369 | 369 | where("#{Changeset.table_name}.repository_id = ? AND #{Changeset.table_name}.commit_date BETWEEN ? AND ?", repository.id, @date_from, @date_to). |
|
370 | 370 | group(:commit_date). |
|
371 | 371 | count |
|
372 | 372 | changes_by_month = [0] * 12 |
|
373 | 373 | changes_by_day.each {|c| changes_by_month[(@date_to.month - c.first.to_date.month) % 12] += c.last } |
|
374 | 374 | |
|
375 | 375 | fields = [] |
|
376 | 376 | today = User.current.today |
|
377 | 377 | 12.times {|m| fields << month_name(((today.month - 1 - m) % 12) + 1)} |
|
378 | 378 | |
|
379 | 379 | graph = SVG::Graph::Bar.new( |
|
380 | 380 | :height => 300, |
|
381 | 381 | :width => 800, |
|
382 | 382 | :fields => fields.reverse, |
|
383 | 383 | :stack => :side, |
|
384 | 384 | :scale_integers => true, |
|
385 | 385 | :step_x_labels => 2, |
|
386 | 386 | :show_data_values => false, |
|
387 | 387 | :graph_title => l(:label_commits_per_month), |
|
388 | 388 | :show_graph_title => true |
|
389 | 389 | ) |
|
390 | 390 | |
|
391 | 391 | graph.add_data( |
|
392 | 392 | :data => commits_by_month[0..11].reverse, |
|
393 | 393 | :title => l(:label_revision_plural) |
|
394 | 394 | ) |
|
395 | 395 | |
|
396 | 396 | graph.add_data( |
|
397 | 397 | :data => changes_by_month[0..11].reverse, |
|
398 | 398 | :title => l(:label_change_plural) |
|
399 | 399 | ) |
|
400 | 400 | |
|
401 | 401 | graph.burn |
|
402 | 402 | end |
|
403 | 403 | |
|
404 | 404 | def graph_commits_per_author(repository) |
|
405 | 405 | #data |
|
406 | 406 | stats = repository.stats_by_author |
|
407 | 407 | fields, commits_data, changes_data = [], [], [] |
|
408 | 408 | stats.each do |name, hsh| |
|
409 | 409 | fields << name |
|
410 | 410 | commits_data << hsh[:commits_count] |
|
411 | 411 | changes_data << hsh[:changes_count] |
|
412 | 412 | end |
|
413 | 413 | |
|
414 | 414 | #expand to 10 values if needed |
|
415 | 415 | fields = fields + [""]*(10 - fields.length) if fields.length<10 |
|
416 | 416 | commits_data = commits_data + [0]*(10 - commits_data.length) if commits_data.length<10 |
|
417 | 417 | changes_data = changes_data + [0]*(10 - changes_data.length) if changes_data.length<10 |
|
418 | 418 | |
|
419 | 419 | # Remove email address in usernames |
|
420 | 420 | fields = fields.collect {|c| c.gsub(%r{<.+@.+>}, '') } |
|
421 | 421 | |
|
422 | 422 | #prepare graph |
|
423 | 423 | graph = SVG::Graph::BarHorizontal.new( |
|
424 | 424 | :height => 30 * commits_data.length, |
|
425 | 425 | :width => 800, |
|
426 | 426 | :fields => fields, |
|
427 | 427 | :stack => :side, |
|
428 | 428 | :scale_integers => true, |
|
429 | 429 | :show_data_values => false, |
|
430 | 430 | :rotate_y_labels => false, |
|
431 | 431 | :graph_title => l(:label_commits_per_author), |
|
432 | 432 | :show_graph_title => true |
|
433 | 433 | ) |
|
434 | 434 | graph.add_data( |
|
435 | 435 | :data => commits_data, |
|
436 | 436 | :title => l(:label_revision_plural) |
|
437 | 437 | ) |
|
438 | 438 | graph.add_data( |
|
439 | 439 | :data => changes_data, |
|
440 | 440 | :title => l(:label_change_plural) |
|
441 | 441 | ) |
|
442 | 442 | graph.burn |
|
443 | 443 | end |
|
444 | 444 | |
|
445 | 445 | def disposition(path) |
|
446 |
if |
|
|
446 | if Redmine::MimeType.of(@path) == "application/pdf" | |
|
447 | 447 | 'inline' |
|
448 | 448 | else |
|
449 | 449 | 'attachment' |
|
450 | 450 | end |
|
451 | 451 | end |
|
452 | 452 | end |
General Comments 0
You need to be logged in to leave comments.
Login now