##// END OF EJS Templates
Appends the filename to the attachment url so that clients that ignore content-disposition http header get the real filename (#1649)....
Jean-Philippe Lang -
r1669:9b579de9e234
parent child
Show More
@@ -1,53 +1,56
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 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 layout 'base'
20 20 before_filter :find_project
21 21
22 22 def show
23 23 if @attachment.is_diff?
24 24 @diff = File.new(@attachment.diskfile, "rb").read
25 25 render :action => 'diff'
26 26 elsif @attachment.is_text?
27 27 @content = File.new(@attachment.diskfile, "rb").read
28 28 render :action => 'file'
29 29 elsif
30 30 download
31 31 end
32 32 end
33 33
34 34 def download
35 35 @attachment.increment_download if @attachment.container.is_a?(Version)
36 36
37 37 # images are sent inline
38 38 send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename),
39 39 :type => @attachment.content_type,
40 40 :disposition => (@attachment.image? ? 'inline' : 'attachment')
41 41 end
42 42
43 43 private
44 44 def find_project
45 45 @attachment = Attachment.find(params[:id])
46 # Show 404 if the filename in the url is wrong
47 raise ActiveRecord::RecordNotFound if params[:filename] && params[:filename] != @attachment.filename
48
46 49 @project = @attachment.project
47 50 permission = @attachment.container.is_a?(Version) ? :view_files : "view_#{@attachment.container.class.name.underscore.pluralize}".to_sym
48 51 allowed = User.current.allowed_to?(permission, @project)
49 52 allowed ? true : (User.current.logged? ? render_403 : require_login)
50 53 rescue ActiveRecord::RecordNotFound
51 54 render_404
52 55 end
53 56 end
@@ -1,522 +1,533
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 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 'coderay'
19 19 require 'coderay/helpers/file_type'
20 20
21 21 module ApplicationHelper
22 22 include Redmine::WikiFormatting::Macros::Definitions
23 23
24 24 def current_role
25 25 @current_role ||= User.current.role_for_project(@project)
26 26 end
27 27
28 28 # Return true if user is authorized for controller/action, otherwise false
29 29 def authorize_for(controller, action)
30 30 User.current.allowed_to?({:controller => controller, :action => action}, @project)
31 31 end
32 32
33 33 # Display a link if user is authorized
34 34 def link_to_if_authorized(name, options = {}, html_options = nil, *parameters_for_method_reference)
35 35 link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(options[:controller] || params[:controller], options[:action])
36 36 end
37 37
38 38 # Display a link to user's account page
39 39 def link_to_user(user)
40 40 user ? link_to(user, :controller => 'account', :action => 'show', :id => user) : 'Anonymous'
41 41 end
42 42
43 43 def link_to_issue(issue, options={})
44 44 options[:class] ||= ''
45 45 options[:class] << ' issue'
46 46 options[:class] << ' closed' if issue.closed?
47 47 link_to "#{issue.tracker.name} ##{issue.id}", {:controller => "issues", :action => "show", :id => issue}, options
48 48 end
49 49
50 # Generates a link to an attachment.
51 # Options:
52 # * :text - Link text (default to attachment filename)
53 # * :download - Force download (default: false)
54 def link_to_attachment(attachment, options={})
55 text = options.delete(:text) || attachment.filename
56 action = options.delete(:download) ? 'download' : 'show'
57
58 link_to(h(text), {:controller => 'attachments', :action => action, :id => attachment, :filename => attachment.filename }, options)
59 end
60
50 61 def toggle_link(name, id, options={})
51 62 onclick = "Element.toggle('#{id}'); "
52 63 onclick << (options[:focus] ? "Form.Element.focus('#{options[:focus]}'); " : "this.blur(); ")
53 64 onclick << "return false;"
54 65 link_to(name, "#", :onclick => onclick)
55 66 end
56 67
57 68 def image_to_function(name, function, html_options = {})
58 69 html_options.symbolize_keys!
59 70 tag(:input, html_options.merge({
60 71 :type => "image", :src => image_path(name),
61 72 :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};"
62 73 }))
63 74 end
64 75
65 76 def prompt_to_remote(name, text, param, url, html_options = {})
66 77 html_options[:onclick] = "promptToRemote('#{text}', '#{param}', '#{url_for(url)}'); return false;"
67 78 link_to name, {}, html_options
68 79 end
69 80
70 81 def format_date(date)
71 82 return nil unless date
72 83 # "Setting.date_format.size < 2" is a temporary fix (content of date_format setting changed)
73 84 @date_format ||= (Setting.date_format.blank? || Setting.date_format.size < 2 ? l(:general_fmt_date) : Setting.date_format)
74 85 date.strftime(@date_format)
75 86 end
76 87
77 88 def format_time(time, include_date = true)
78 89 return nil unless time
79 90 time = time.to_time if time.is_a?(String)
80 91 zone = User.current.time_zone
81 92 local = zone ? time.in_time_zone(zone) : (time.utc? ? time.utc_to_local : time)
82 93 @date_format ||= (Setting.date_format.blank? || Setting.date_format.size < 2 ? l(:general_fmt_date) : Setting.date_format)
83 94 @time_format ||= (Setting.time_format.blank? ? l(:general_fmt_time) : Setting.time_format)
84 95 include_date ? local.strftime("#{@date_format} #{@time_format}") : local.strftime(@time_format)
85 96 end
86 97
87 98 # Truncates and returns the string as a single line
88 99 def truncate_single_line(string, *args)
89 100 truncate(string, *args).gsub(%r{[\r\n]+}m, ' ')
90 101 end
91 102
92 103 def html_hours(text)
93 104 text.gsub(%r{(\d+)\.(\d+)}, '<span class="hours hours-int">\1</span><span class="hours hours-dec">.\2</span>')
94 105 end
95 106
96 107 def authoring(created, author)
97 108 time_tag = content_tag('acronym', distance_of_time_in_words(Time.now, created), :title => format_time(created))
98 109 author_tag = (author.is_a?(User) && !author.anonymous?) ? link_to(h(author), :controller => 'account', :action => 'show', :id => author) : h(author || 'Anonymous')
99 110 l(:label_added_time_by, author_tag, time_tag)
100 111 end
101 112
102 113 def l_or_humanize(s)
103 114 l_has_string?("label_#{s}".to_sym) ? l("label_#{s}".to_sym) : s.to_s.humanize
104 115 end
105 116
106 117 def day_name(day)
107 118 l(:general_day_names).split(',')[day-1]
108 119 end
109 120
110 121 def month_name(month)
111 122 l(:actionview_datehelper_select_month_names).split(',')[month-1]
112 123 end
113 124
114 125 def syntax_highlight(name, content)
115 126 type = CodeRay::FileType[name]
116 127 type ? CodeRay.scan(content, type).html : h(content)
117 128 end
118 129
119 130 def to_path_param(path)
120 131 path.to_s.split(%r{[/\\]}).select {|p| !p.blank?}
121 132 end
122 133
123 134 def pagination_links_full(paginator, count=nil, options={})
124 135 page_param = options.delete(:page_param) || :page
125 136 url_param = params.dup
126 137 # don't reuse params if filters are present
127 138 url_param.clear if url_param.has_key?(:set_filter)
128 139
129 140 html = ''
130 141 html << link_to_remote(('&#171; ' + l(:label_previous)),
131 142 {:update => 'content',
132 143 :url => url_param.merge(page_param => paginator.current.previous),
133 144 :complete => 'window.scrollTo(0,0)'},
134 145 {:href => url_for(:params => url_param.merge(page_param => paginator.current.previous))}) + ' ' if paginator.current.previous
135 146
136 147 html << (pagination_links_each(paginator, options) do |n|
137 148 link_to_remote(n.to_s,
138 149 {:url => {:params => url_param.merge(page_param => n)},
139 150 :update => 'content',
140 151 :complete => 'window.scrollTo(0,0)'},
141 152 {:href => url_for(:params => url_param.merge(page_param => n))})
142 153 end || '')
143 154
144 155 html << ' ' + link_to_remote((l(:label_next) + ' &#187;'),
145 156 {:update => 'content',
146 157 :url => url_param.merge(page_param => paginator.current.next),
147 158 :complete => 'window.scrollTo(0,0)'},
148 159 {:href => url_for(:params => url_param.merge(page_param => paginator.current.next))}) if paginator.current.next
149 160
150 161 unless count.nil?
151 162 html << [" (#{paginator.current.first_item}-#{paginator.current.last_item}/#{count})", per_page_links(paginator.items_per_page)].compact.join(' | ')
152 163 end
153 164
154 165 html
155 166 end
156 167
157 168 def per_page_links(selected=nil)
158 169 url_param = params.dup
159 170 url_param.clear if url_param.has_key?(:set_filter)
160 171
161 172 links = Setting.per_page_options_array.collect do |n|
162 173 n == selected ? n : link_to_remote(n, {:update => "content", :url => params.dup.merge(:per_page => n)},
163 174 {:href => url_for(url_param.merge(:per_page => n))})
164 175 end
165 176 links.size > 1 ? l(:label_display_per_page, links.join(', ')) : nil
166 177 end
167 178
168 179 def breadcrumb(*args)
169 180 content_tag('p', args.join(' &#187; ') + ' &#187; ', :class => 'breadcrumb')
170 181 end
171 182
172 183 def html_title(*args)
173 184 if args.empty?
174 185 title = []
175 186 title << @project.name if @project
176 187 title += @html_title if @html_title
177 188 title << Setting.app_title
178 189 title.compact.join(' - ')
179 190 else
180 191 @html_title ||= []
181 192 @html_title += args
182 193 end
183 194 end
184 195
185 196 def accesskey(s)
186 197 Redmine::AccessKeys.key_for s
187 198 end
188 199
189 200 # Formats text according to system settings.
190 201 # 2 ways to call this method:
191 202 # * with a String: textilizable(text, options)
192 203 # * with an object and one of its attribute: textilizable(issue, :description, options)
193 204 def textilizable(*args)
194 205 options = args.last.is_a?(Hash) ? args.pop : {}
195 206 case args.size
196 207 when 1
197 208 obj = nil
198 209 text = args.shift
199 210 when 2
200 211 obj = args.shift
201 212 text = obj.send(args.shift).to_s
202 213 else
203 214 raise ArgumentError, 'invalid arguments to textilizable'
204 215 end
205 216 return '' if text.blank?
206 217
207 218 only_path = options.delete(:only_path) == false ? false : true
208 219
209 220 # when using an image link, try to use an attachment, if possible
210 221 attachments = options[:attachments] || (obj && obj.respond_to?(:attachments) ? obj.attachments : nil)
211 222
212 223 if attachments
213 224 text = text.gsub(/!((\<|\=|\>)?(\([^\)]+\))?(\[[^\]]+\])?(\{[^\}]+\})?)(\S+\.(gif|jpg|jpeg|png))!/) do |m|
214 225 style = $1
215 226 filename = $6
216 227 rf = Regexp.new(filename, Regexp::IGNORECASE)
217 228 # search for the picture in attachments
218 229 if found = attachments.detect { |att| att.filename =~ rf }
219 230 image_url = url_for :only_path => only_path, :controller => 'attachments', :action => 'download', :id => found
220 231 desc = found.description.to_s.gsub(/^([^\(\)]*).*$/, "\\1")
221 232 alt = desc.blank? ? nil : "(#{desc})"
222 233 "!#{style}#{image_url}#{alt}!"
223 234 else
224 235 "!#{style}#{filename}!"
225 236 end
226 237 end
227 238 end
228 239
229 240 text = (Setting.text_formatting == 'textile') ?
230 241 Redmine::WikiFormatting.to_html(text) { |macro, args| exec_macro(macro, obj, args) } :
231 242 simple_format(auto_link(h(text)))
232 243
233 244 # different methods for formatting wiki links
234 245 case options[:wiki_links]
235 246 when :local
236 247 # used for local links to html files
237 248 format_wiki_link = Proc.new {|project, title| "#{title}.html" }
238 249 when :anchor
239 250 # used for single-file wiki export
240 251 format_wiki_link = Proc.new {|project, title| "##{title}" }
241 252 else
242 253 format_wiki_link = Proc.new {|project, title| url_for(:only_path => only_path, :controller => 'wiki', :action => 'index', :id => project, :page => title) }
243 254 end
244 255
245 256 project = options[:project] || @project || (obj && obj.respond_to?(:project) ? obj.project : nil)
246 257
247 258 # Wiki links
248 259 #
249 260 # Examples:
250 261 # [[mypage]]
251 262 # [[mypage|mytext]]
252 263 # wiki links can refer other project wikis, using project name or identifier:
253 264 # [[project:]] -> wiki starting page
254 265 # [[project:|mytext]]
255 266 # [[project:mypage]]
256 267 # [[project:mypage|mytext]]
257 268 text = text.gsub(/(!)?(\[\[([^\]\n\|]+)(\|([^\]\n\|]+))?\]\])/) do |m|
258 269 link_project = project
259 270 esc, all, page, title = $1, $2, $3, $5
260 271 if esc.nil?
261 272 if page =~ /^([^\:]+)\:(.*)$/
262 273 link_project = Project.find_by_name($1) || Project.find_by_identifier($1)
263 274 page = $2
264 275 title ||= $1 if page.blank?
265 276 end
266 277
267 278 if link_project && link_project.wiki
268 279 # check if page exists
269 280 wiki_page = link_project.wiki.find_page(page)
270 281 link_to((title || page), format_wiki_link.call(link_project, Wiki.titleize(page)),
271 282 :class => ('wiki-page' + (wiki_page ? '' : ' new')))
272 283 else
273 284 # project or wiki doesn't exist
274 285 title || page
275 286 end
276 287 else
277 288 all
278 289 end
279 290 end
280 291
281 292 # Redmine links
282 293 #
283 294 # Examples:
284 295 # Issues:
285 296 # #52 -> Link to issue #52
286 297 # Changesets:
287 298 # r52 -> Link to revision 52
288 299 # commit:a85130f -> Link to scmid starting with a85130f
289 300 # Documents:
290 301 # document#17 -> Link to document with id 17
291 302 # document:Greetings -> Link to the document with title "Greetings"
292 303 # document:"Some document" -> Link to the document with title "Some document"
293 304 # Versions:
294 305 # version#3 -> Link to version with id 3
295 306 # version:1.0.0 -> Link to version named "1.0.0"
296 307 # version:"1.0 beta 2" -> Link to version named "1.0 beta 2"
297 308 # Attachments:
298 309 # attachment:file.zip -> Link to the attachment of the current object named file.zip
299 310 # Source files:
300 311 # source:some/file -> Link to the file located at /some/file in the project's repository
301 312 # source:some/file@52 -> Link to the file's revision 52
302 313 # source:some/file#L120 -> Link to line 120 of the file
303 314 # source:some/file@52#L120 -> Link to line 120 of the file's revision 52
304 315 # export:some/file -> Force the download of the file
305 316 text = text.gsub(%r{([\s\(,\-\>]|^)(!)?(attachment|document|version|commit|source|export)?((#|r)(\d+)|(:)([^"\s<>][^\s<>]*?|"[^"]+?"))(?=(?=[[:punct:]]\W)|\s|<|$)}) do |m|
306 317 leading, esc, prefix, sep, oid = $1, $2, $3, $5 || $7, $6 || $8
307 318 link = nil
308 319 if esc.nil?
309 320 if prefix.nil? && sep == 'r'
310 321 if project && (changeset = project.changesets.find_by_revision(oid))
311 322 link = link_to("r#{oid}", {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => oid},
312 323 :class => 'changeset',
313 324 :title => truncate_single_line(changeset.comments, 100))
314 325 end
315 326 elsif sep == '#'
316 327 oid = oid.to_i
317 328 case prefix
318 329 when nil
319 330 if issue = Issue.find_by_id(oid, :include => [:project, :status], :conditions => Project.visible_by(User.current))
320 331 link = link_to("##{oid}", {:only_path => only_path, :controller => 'issues', :action => 'show', :id => oid},
321 332 :class => (issue.closed? ? 'issue closed' : 'issue'),
322 333 :title => "#{truncate(issue.subject, 100)} (#{issue.status.name})")
323 334 link = content_tag('del', link) if issue.closed?
324 335 end
325 336 when 'document'
326 337 if document = Document.find_by_id(oid, :include => [:project], :conditions => Project.visible_by(User.current))
327 338 link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document},
328 339 :class => 'document'
329 340 end
330 341 when 'version'
331 342 if version = Version.find_by_id(oid, :include => [:project], :conditions => Project.visible_by(User.current))
332 343 link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version},
333 344 :class => 'version'
334 345 end
335 346 end
336 347 elsif sep == ':'
337 348 # removes the double quotes if any
338 349 name = oid.gsub(%r{^"(.*)"$}, "\\1")
339 350 case prefix
340 351 when 'document'
341 352 if project && document = project.documents.find_by_title(name)
342 353 link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document},
343 354 :class => 'document'
344 355 end
345 356 when 'version'
346 357 if project && version = project.versions.find_by_name(name)
347 358 link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version},
348 359 :class => 'version'
349 360 end
350 361 when 'commit'
351 362 if project && (changeset = project.changesets.find(:first, :conditions => ["scmid LIKE ?", "#{name}%"]))
352 363 link = link_to h("#{name}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => changeset.revision},
353 364 :class => 'changeset',
354 365 :title => truncate_single_line(changeset.comments, 100)
355 366 end
356 367 when 'source', 'export'
357 368 if project && project.repository
358 369 name =~ %r{^[/\\]*(.*?)(@([0-9a-f]+))?(#(L\d+))?$}
359 370 path, rev, anchor = $1, $3, $5
360 371 link = link_to h("#{prefix}:#{name}"), {:controller => 'repositories', :action => 'entry', :id => project,
361 372 :path => to_path_param(path),
362 373 :rev => rev,
363 374 :anchor => anchor,
364 375 :format => (prefix == 'export' ? 'raw' : nil)},
365 376 :class => (prefix == 'export' ? 'source download' : 'source')
366 377 end
367 378 when 'attachment'
368 379 if attachments && attachment = attachments.detect {|a| a.filename == name }
369 380 link = link_to h(attachment.filename), {:only_path => only_path, :controller => 'attachments', :action => 'download', :id => attachment},
370 381 :class => 'attachment'
371 382 end
372 383 end
373 384 end
374 385 end
375 386 leading + (link || "#{prefix}#{sep}#{oid}")
376 387 end
377 388
378 389 text
379 390 end
380 391
381 392 # Same as Rails' simple_format helper without using paragraphs
382 393 def simple_format_without_paragraph(text)
383 394 text.to_s.
384 395 gsub(/\r\n?/, "\n"). # \r\n and \r -> \n
385 396 gsub(/\n\n+/, "<br /><br />"). # 2+ newline -> 2 br
386 397 gsub(/([^\n]\n)(?=[^\n])/, '\1<br />') # 1 newline -> br
387 398 end
388 399
389 400 def error_messages_for(object_name, options = {})
390 401 options = options.symbolize_keys
391 402 object = instance_variable_get("@#{object_name}")
392 403 if object && !object.errors.empty?
393 404 # build full_messages here with controller current language
394 405 full_messages = []
395 406 object.errors.each do |attr, msg|
396 407 next if msg.nil?
397 408 msg = msg.first if msg.is_a? Array
398 409 if attr == "base"
399 410 full_messages << l(msg)
400 411 else
401 412 full_messages << "&#171; " + (l_has_string?("field_" + attr) ? l("field_" + attr) : object.class.human_attribute_name(attr)) + " &#187; " + l(msg) unless attr == "custom_values"
402 413 end
403 414 end
404 415 # retrieve custom values error messages
405 416 if object.errors[:custom_values]
406 417 object.custom_values.each do |v|
407 418 v.errors.each do |attr, msg|
408 419 next if msg.nil?
409 420 msg = msg.first if msg.is_a? Array
410 421 full_messages << "&#171; " + v.custom_field.name + " &#187; " + l(msg)
411 422 end
412 423 end
413 424 end
414 425 content_tag("div",
415 426 content_tag(
416 427 options[:header_tag] || "span", lwr(:gui_validation_error, full_messages.length) + ":"
417 428 ) +
418 429 content_tag("ul", full_messages.collect { |msg| content_tag("li", msg) }),
419 430 "id" => options[:id] || "errorExplanation", "class" => options[:class] || "errorExplanation"
420 431 )
421 432 else
422 433 ""
423 434 end
424 435 end
425 436
426 437 def lang_options_for_select(blank=true)
427 438 (blank ? [["(auto)", ""]] : []) +
428 439 GLoc.valid_languages.collect{|lang| [ ll(lang.to_s, :general_lang_name), lang.to_s]}.sort{|x,y| x.last <=> y.last }
429 440 end
430 441
431 442 def label_tag_for(name, option_tags = nil, options = {})
432 443 label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "")
433 444 content_tag("label", label_text)
434 445 end
435 446
436 447 def labelled_tabular_form_for(name, object, options, &proc)
437 448 options[:html] ||= {}
438 449 options[:html][:class] = 'tabular' unless options[:html].has_key?(:class)
439 450 form_for(name, object, options.merge({ :builder => TabularFormBuilder, :lang => current_language}), &proc)
440 451 end
441 452
442 453 def back_url_hidden_field_tag
443 454 hidden_field_tag 'back_url', (params[:back_url] || request.env['HTTP_REFERER'])
444 455 end
445 456
446 457 def check_all_links(form_name)
447 458 link_to_function(l(:button_check_all), "checkAll('#{form_name}', true)") +
448 459 " | " +
449 460 link_to_function(l(:button_uncheck_all), "checkAll('#{form_name}', false)")
450 461 end
451 462
452 463 def progress_bar(pcts, options={})
453 464 pcts = [pcts, pcts] unless pcts.is_a?(Array)
454 465 pcts[1] = pcts[1] - pcts[0]
455 466 pcts << (100 - pcts[1] - pcts[0])
456 467 width = options[:width] || '100px;'
457 468 legend = options[:legend] || ''
458 469 content_tag('table',
459 470 content_tag('tr',
460 471 (pcts[0] > 0 ? content_tag('td', '', :width => "#{pcts[0].floor}%;", :class => 'closed') : '') +
461 472 (pcts[1] > 0 ? content_tag('td', '', :width => "#{pcts[1].floor}%;", :class => 'done') : '') +
462 473 (pcts[2] > 0 ? content_tag('td', '', :width => "#{pcts[2].floor}%;", :class => 'todo') : '')
463 474 ), :class => 'progress', :style => "width: #{width};") +
464 475 content_tag('p', legend, :class => 'pourcent')
465 476 end
466 477
467 478 def context_menu_link(name, url, options={})
468 479 options[:class] ||= ''
469 480 if options.delete(:selected)
470 481 options[:class] << ' icon-checked disabled'
471 482 options[:disabled] = true
472 483 end
473 484 if options.delete(:disabled)
474 485 options.delete(:method)
475 486 options.delete(:confirm)
476 487 options.delete(:onclick)
477 488 options[:class] << ' disabled'
478 489 url = '#'
479 490 end
480 491 link_to name, url, options
481 492 end
482 493
483 494 def calendar_for(field_id)
484 495 include_calendar_headers_tags
485 496 image_tag("calendar.png", {:id => "#{field_id}_trigger",:class => "calendar-trigger"}) +
486 497 javascript_tag("Calendar.setup({inputField : '#{field_id}', ifFormat : '%Y-%m-%d', button : '#{field_id}_trigger' });")
487 498 end
488 499
489 500 def include_calendar_headers_tags
490 501 unless @calendar_headers_tags_included
491 502 @calendar_headers_tags_included = true
492 503 content_for :header_tags do
493 504 javascript_include_tag('calendar/calendar') +
494 505 javascript_include_tag("calendar/lang/calendar-#{current_language}.js") +
495 506 javascript_include_tag('calendar/calendar-setup') +
496 507 stylesheet_link_tag('calendar')
497 508 end
498 509 end
499 510 end
500 511
501 512 def wikitoolbar_for(field_id)
502 513 return '' unless Setting.text_formatting == 'textile'
503 514
504 515 help_link = l(:setting_text_formatting) + ': ' +
505 516 link_to(l(:label_help), compute_public_path('wiki_syntax', 'help', 'html'),
506 517 :onclick => "window.open(\"#{ compute_public_path('wiki_syntax', 'help', 'html') }\", \"\", \"resizable=yes, location=no, width=300, height=640, menubar=no, status=no, scrollbars=yes\"); return false;")
507 518
508 519 javascript_include_tag('jstoolbar/jstoolbar') +
509 520 javascript_include_tag("jstoolbar/lang/jstoolbar-#{current_language}") +
510 521 javascript_tag("var toolbar = new jsToolBar($('#{field_id}')); toolbar.setHelpLink('#{help_link}'); toolbar.draw();")
511 522 end
512 523
513 524 def content_for(name, content = nil, &block)
514 525 @has_content ||= {}
515 526 @has_content[name] = true
516 527 super(name, content, &block)
517 528 end
518 529
519 530 def has_content?(name)
520 531 (@has_content && @has_content[name]) || false
521 532 end
522 533 end
@@ -1,184 +1,184
1 1 # redMine - project management software
2 2 # Copyright (C) 2006 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 'csv'
19 19
20 20 module IssuesHelper
21 21 include ApplicationHelper
22 22
23 23 def render_issue_tooltip(issue)
24 24 @cached_label_start_date ||= l(:field_start_date)
25 25 @cached_label_due_date ||= l(:field_due_date)
26 26 @cached_label_assigned_to ||= l(:field_assigned_to)
27 27 @cached_label_priority ||= l(:field_priority)
28 28
29 29 link_to_issue(issue) + ": #{h(issue.subject)}<br /><br />" +
30 30 "<strong>#{@cached_label_start_date}</strong>: #{format_date(issue.start_date)}<br />" +
31 31 "<strong>#{@cached_label_due_date}</strong>: #{format_date(issue.due_date)}<br />" +
32 32 "<strong>#{@cached_label_assigned_to}</strong>: #{issue.assigned_to}<br />" +
33 33 "<strong>#{@cached_label_priority}</strong>: #{issue.priority.name}"
34 34 end
35 35
36 36 def sidebar_queries
37 37 unless @sidebar_queries
38 38 # User can see public queries and his own queries
39 39 visible = ARCondition.new(["is_public = ? OR user_id = ?", true, (User.current.logged? ? User.current.id : 0)])
40 40 # Project specific queries and global queries
41 41 visible << (@project.nil? ? ["project_id IS NULL"] : ["project_id IS NULL OR project_id = ?", @project.id])
42 42 @sidebar_queries = Query.find(:all,
43 43 :order => "name ASC",
44 44 :conditions => visible.conditions)
45 45 end
46 46 @sidebar_queries
47 47 end
48 48
49 49 def show_detail(detail, no_html=false)
50 50 case detail.property
51 51 when 'attr'
52 52 label = l(("field_" + detail.prop_key.to_s.gsub(/\_id$/, "")).to_sym)
53 53 case detail.prop_key
54 54 when 'due_date', 'start_date'
55 55 value = format_date(detail.value.to_date) if detail.value
56 56 old_value = format_date(detail.old_value.to_date) if detail.old_value
57 57 when 'project_id'
58 58 p = Project.find_by_id(detail.value) and value = p.name if detail.value
59 59 p = Project.find_by_id(detail.old_value) and old_value = p.name if detail.old_value
60 60 when 'status_id'
61 61 s = IssueStatus.find_by_id(detail.value) and value = s.name if detail.value
62 62 s = IssueStatus.find_by_id(detail.old_value) and old_value = s.name if detail.old_value
63 63 when 'tracker_id'
64 64 t = Tracker.find_by_id(detail.value) and value = t.name if detail.value
65 65 t = Tracker.find_by_id(detail.old_value) and old_value = t.name if detail.old_value
66 66 when 'assigned_to_id'
67 67 u = User.find_by_id(detail.value) and value = u.name if detail.value
68 68 u = User.find_by_id(detail.old_value) and old_value = u.name if detail.old_value
69 69 when 'priority_id'
70 70 e = Enumeration.find_by_id(detail.value) and value = e.name if detail.value
71 71 e = Enumeration.find_by_id(detail.old_value) and old_value = e.name if detail.old_value
72 72 when 'category_id'
73 73 c = IssueCategory.find_by_id(detail.value) and value = c.name if detail.value
74 74 c = IssueCategory.find_by_id(detail.old_value) and old_value = c.name if detail.old_value
75 75 when 'fixed_version_id'
76 76 v = Version.find_by_id(detail.value) and value = v.name if detail.value
77 77 v = Version.find_by_id(detail.old_value) and old_value = v.name if detail.old_value
78 78 end
79 79 when 'cf'
80 80 custom_field = CustomField.find_by_id(detail.prop_key)
81 81 if custom_field
82 82 label = custom_field.name
83 83 value = format_value(detail.value, custom_field.field_format) if detail.value
84 84 old_value = format_value(detail.old_value, custom_field.field_format) if detail.old_value
85 85 end
86 86 when 'attachment'
87 87 label = l(:label_attachment)
88 88 end
89 89
90 90 label ||= detail.prop_key
91 91 value ||= detail.value
92 92 old_value ||= detail.old_value
93 93
94 94 unless no_html
95 95 label = content_tag('strong', label)
96 96 old_value = content_tag("i", h(old_value)) if detail.old_value
97 97 old_value = content_tag("strike", old_value) if detail.old_value and (!detail.value or detail.value.empty?)
98 if detail.property == 'attachment' && !value.blank? && Attachment.find_by_id(detail.prop_key)
98 if detail.property == 'attachment' && !value.blank? && a = Attachment.find_by_id(detail.prop_key)
99 99 # Link to the attachment if it has not been removed
100 value = link_to(value, :controller => 'attachments', :action => 'show', :id => detail.prop_key)
100 value = link_to_attachment(a)
101 101 else
102 102 value = content_tag("i", h(value)) if value
103 103 end
104 104 end
105 105
106 106 if !detail.value.blank?
107 107 case detail.property
108 108 when 'attr', 'cf'
109 109 if !detail.old_value.blank?
110 110 label + " " + l(:text_journal_changed, old_value, value)
111 111 else
112 112 label + " " + l(:text_journal_set_to, value)
113 113 end
114 114 when 'attachment'
115 115 "#{label} #{value} #{l(:label_added)}"
116 116 end
117 117 else
118 118 case detail.property
119 119 when 'attr', 'cf'
120 120 label + " " + l(:text_journal_deleted) + " (#{old_value})"
121 121 when 'attachment'
122 122 "#{label} #{old_value} #{l(:label_deleted)}"
123 123 end
124 124 end
125 125 end
126 126
127 127 def issues_to_csv(issues, project = nil)
128 128 ic = Iconv.new(l(:general_csv_encoding), 'UTF-8')
129 129 decimal_separator = l(:general_csv_decimal_separator)
130 130 export = StringIO.new
131 131 CSV::Writer.generate(export, l(:general_csv_separator)) do |csv|
132 132 # csv header fields
133 133 headers = [ "#",
134 134 l(:field_status),
135 135 l(:field_project),
136 136 l(:field_tracker),
137 137 l(:field_priority),
138 138 l(:field_subject),
139 139 l(:field_assigned_to),
140 140 l(:field_category),
141 141 l(:field_fixed_version),
142 142 l(:field_author),
143 143 l(:field_start_date),
144 144 l(:field_due_date),
145 145 l(:field_done_ratio),
146 146 l(:field_estimated_hours),
147 147 l(:field_created_on),
148 148 l(:field_updated_on)
149 149 ]
150 150 # Export project custom fields if project is given
151 151 # otherwise export custom fields marked as "For all projects"
152 152 custom_fields = project.nil? ? IssueCustomField.for_all : project.all_issue_custom_fields
153 153 custom_fields.each {|f| headers << f.name}
154 154 # Description in the last column
155 155 headers << l(:field_description)
156 156 csv << headers.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
157 157 # csv lines
158 158 issues.each do |issue|
159 159 fields = [issue.id,
160 160 issue.status.name,
161 161 issue.project.name,
162 162 issue.tracker.name,
163 163 issue.priority.name,
164 164 issue.subject,
165 165 issue.assigned_to,
166 166 issue.category,
167 167 issue.fixed_version,
168 168 issue.author.name,
169 169 format_date(issue.start_date),
170 170 format_date(issue.due_date),
171 171 issue.done_ratio,
172 172 issue.estimated_hours.to_s.gsub('.', decimal_separator),
173 173 format_time(issue.created_on),
174 174 format_time(issue.updated_on)
175 175 ]
176 176 custom_fields.each {|f| fields << show_value(issue.custom_value_for(f)) }
177 177 fields << issue.description
178 178 csv << fields.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
179 179 end
180 180 end
181 181 export.rewind
182 182 export
183 183 end
184 184 end
@@ -1,122 +1,122
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 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 "digest/md5"
19 19
20 20 class Attachment < ActiveRecord::Base
21 21 belongs_to :container, :polymorphic => true
22 22 belongs_to :author, :class_name => "User", :foreign_key => "author_id"
23 23
24 24 validates_presence_of :container, :filename, :author
25 25 validates_length_of :filename, :maximum => 255
26 26 validates_length_of :disk_filename, :maximum => 255
27 27
28 28 acts_as_event :title => :filename,
29 :url => Proc.new {|o| {:controller => 'attachments', :action => 'download', :id => o.id}}
29 :url => Proc.new {|o| {:controller => 'attachments', :action => 'download', :id => o.id, :filename => o.filename}}
30 30
31 31 cattr_accessor :storage_path
32 32 @@storage_path = "#{RAILS_ROOT}/files"
33 33
34 34 def validate
35 35 errors.add_to_base :too_long if self.filesize > Setting.attachment_max_size.to_i.kilobytes
36 36 end
37 37
38 38 def file=(incoming_file)
39 39 unless incoming_file.nil?
40 40 @temp_file = incoming_file
41 41 if @temp_file.size > 0
42 42 self.filename = sanitize_filename(@temp_file.original_filename)
43 43 self.disk_filename = Attachment.disk_filename(filename)
44 44 self.content_type = @temp_file.content_type.to_s.chomp
45 45 self.filesize = @temp_file.size
46 46 end
47 47 end
48 48 end
49 49
50 50 def file
51 51 nil
52 52 end
53 53
54 54 # Copy temp file to its final location
55 55 def before_save
56 56 if @temp_file && (@temp_file.size > 0)
57 57 logger.debug("saving '#{self.diskfile}'")
58 58 File.open(diskfile, "wb") do |f|
59 59 f.write(@temp_file.read)
60 60 end
61 61 self.digest = Digest::MD5.hexdigest(File.read(diskfile))
62 62 end
63 63 # Don't save the content type if it's longer than the authorized length
64 64 if self.content_type && self.content_type.length > 255
65 65 self.content_type = nil
66 66 end
67 67 end
68 68
69 69 # Deletes file on the disk
70 70 def after_destroy
71 71 File.delete(diskfile) if !filename.blank? && File.exist?(diskfile)
72 72 end
73 73
74 74 # Returns file's location on disk
75 75 def diskfile
76 76 "#{@@storage_path}/#{self.disk_filename}"
77 77 end
78 78
79 79 def increment_download
80 80 increment!(:downloads)
81 81 end
82 82
83 83 def project
84 84 container.project
85 85 end
86 86
87 87 def image?
88 88 self.filename =~ /\.(jpe?g|gif|png)$/i
89 89 end
90 90
91 91 def is_text?
92 92 Redmine::MimeType.is_type?('text', filename)
93 93 end
94 94
95 95 def is_diff?
96 96 self.filename =~ /\.(patch|diff)$/i
97 97 end
98 98
99 99 private
100 100 def sanitize_filename(value)
101 101 # get only the filename, not the whole path
102 102 just_filename = value.gsub(/^.*(\\|\/)/, '')
103 103 # NOTE: File.basename doesn't work right with Windows paths on Unix
104 104 # INCORRECT: just_filename = File.basename(value.gsub('\\\\', '/'))
105 105
106 106 # Finally, replace all non alphanumeric, hyphens or periods with underscore
107 107 @filename = just_filename.gsub(/[^\w\.\-]/,'_')
108 108 end
109 109
110 110 # Returns an ASCII or hashed filename
111 111 def self.disk_filename(filename)
112 112 df = DateTime.now.strftime("%y%m%d%H%M%S") + "_"
113 113 if filename =~ %r{^[a-zA-Z0-9_\.\-]*$}
114 114 df << filename
115 115 else
116 116 df << Digest::MD5.hexdigest(filename)
117 117 # keep the extension if any
118 118 df << $1 if filename =~ %r{(\.[a-zA-Z0-9]+)$}
119 119 end
120 120 df
121 121 end
122 122 end
@@ -1,18 +1,18
1 1 <div class="attachments">
2 2 <% for attachment in attachments %>
3 <p><%= link_to attachment.filename, {:controller => 'attachments', :action => 'show', :id => attachment }, :class => 'icon icon-attachment' -%>
3 <p><%= link_to_attachment attachment, :class => 'icon icon-attachment' -%>
4 4 <%= h(" - #{attachment.description}") unless attachment.description.blank? %>
5 5 <span class="size">(<%= number_to_human_size attachment.filesize %>)</span>
6 6 <% if options[:delete_url] %>
7 7 <%= link_to image_tag('delete.png'), options[:delete_url].update({:attachment_id => attachment}),
8 8 :confirm => l(:text_are_you_sure),
9 9 :method => :post,
10 10 :class => 'delete',
11 11 :title => l(:button_delete) %>
12 12 <% end %>
13 13 <% unless options[:no_author] %>
14 14 <span class="author"><%= attachment.author %>, <%= format_time(attachment.created_on) %></span>
15 15 <% end %>
16 16 </p>
17 17 <% end %>
18 18 </div>
@@ -1,15 +1,15
1 1 <h2><%=h @attachment.filename %></h2>
2 2
3 3 <div class="attachments">
4 4 <p><%= h("#{@attachment.description} - ") unless @attachment.description.blank? %>
5 5 <span class="author"><%= @attachment.author %>, <%= format_time(@attachment.created_on) %></span></p>
6 <p><%= link_to l(:button_download), {:controller => 'attachments', :action => 'download', :id => @attachment } -%>
6 <p><%= link_to_attachment @attachment, :text => l(:button_download), :download => true -%>
7 7 <span class="size">(<%= number_to_human_size @attachment.filesize %>)</span></p>
8 8
9 9 </div>
10 10 &nbsp;
11 11 <%= render :partial => 'common/diff', :locals => {:diff => @diff, :diff_type => @diff_type} %>
12 12
13 13 <% content_for :header_tags do -%>
14 14 <%= stylesheet_link_tag "scm" -%>
15 15 <% end -%>
@@ -1,15 +1,15
1 1 <h2><%=h @attachment.filename %></h2>
2 2
3 3 <div class="attachments">
4 4 <p><%= h("#{@attachment.description} - ") unless @attachment.description.blank? %>
5 5 <span class="author"><%= @attachment.author %>, <%= format_time(@attachment.created_on) %></span></p>
6 <p><%= link_to l(:button_download), {:controller => 'attachments', :action => 'download', :id => @attachment } -%>
6 <p><%= link_to_attachment @attachment, :text => l(:button_download), :download => true -%>
7 7 <span class="size">(<%= number_to_human_size @attachment.filesize %>)</span></p>
8 8
9 9 </div>
10 10 &nbsp;
11 11 <%= render :partial => 'common/file', :locals => {:content => @content, :filename => @attachment.filename} %>
12 12
13 13 <% content_for :header_tags do -%>
14 14 <%= stylesheet_link_tag "scm" -%>
15 15 <% end -%>
@@ -1,45 +1,44
1 1 <div class="contextual">
2 2 <%= link_to_if_authorized l(:label_attachment_new), {:controller => 'projects', :action => 'add_file', :id => @project}, :class => 'icon icon-add' %>
3 3 </div>
4 4
5 5 <h2><%=l(:label_attachment_plural)%></h2>
6 6
7 7 <% delete_allowed = authorize_for('versions', 'destroy_file') %>
8 8
9 9 <table class="list">
10 10 <thead><tr>
11 11 <th><%=l(:field_version)%></th>
12 12 <%= sort_header_tag("#{Attachment.table_name}.filename", :caption => l(:field_filename)) %>
13 13 <%= sort_header_tag("#{Attachment.table_name}.created_on", :caption => l(:label_date), :default_order => 'desc') %>
14 14 <%= sort_header_tag("#{Attachment.table_name}.filesize", :caption => l(:field_filesize), :default_order => 'desc') %>
15 15 <%= sort_header_tag("#{Attachment.table_name}.downloads", :caption => l(:label_downloads_abbr), :default_order => 'desc') %>
16 16 <th>MD5</th>
17 17 <% if delete_allowed %><th></th><% end %>
18 18 </tr></thead>
19 19 <tbody>
20 20 <% for version in @versions %>
21 21 <% unless version.attachments.empty? %>
22 22 <tr><th colspan="7" align="left"><span class="icon icon-package"><b><%= version.name %></b></span></th></tr>
23 23 <% for file in version.attachments %>
24 24 <tr class="<%= cycle("odd", "even") %>">
25 25 <td></td>
26 <td><%= link_to(h(file.filename), {:controller => 'attachments', :action => 'download', :id => file},
27 :title => file.description) %></td>
26 <td><%= link_to_attachment file, :download => true, :title => file.description %></td>
28 27 <td align="center"><%= format_time(file.created_on) %></td>
29 28 <td align="center"><%= number_to_human_size(file.filesize) %></td>
30 29 <td align="center"><%= file.downloads %></td>
31 30 <td align="center"><small><%= file.digest %></small></td>
32 31 <% if delete_allowed %>
33 32 <td align="center">
34 33 <%= link_to_if_authorized image_tag('delete.png'), {:controller => 'versions', :action => 'destroy_file', :id => version, :attachment_id => file}, :confirm => l(:text_are_you_sure), :method => :post %>
35 34 </td>
36 35 <% end %>
37 36 </tr>
38 37 <% end
39 38 reset_cycle %>
40 39 <% end %>
41 40 <% end %>
42 41 </tbody>
43 42 </table>
44 43
45 44 <% html_title(l(:label_attachment_plural)) -%>
@@ -1,44 +1,46
1 1 ActionController::Routing::Routes.draw do |map|
2 2 # Add your own custom routes here.
3 3 # The priority is based upon order of creation: first created -> highest priority.
4 4
5 5 # Here's a sample route:
6 6 # map.connect 'products/:id', :controller => 'catalog', :action => 'view'
7 7 # Keep in mind you can assign values other than :controller and :action
8 8
9 9 map.home '', :controller => 'welcome'
10 10 map.signin 'login', :controller => 'account', :action => 'login'
11 11 map.signout 'logout', :controller => 'account', :action => 'logout'
12 12
13 13 map.connect 'wiki/:id/:page/:action', :controller => 'wiki', :page => nil
14 14 map.connect 'roles/workflow/:id/:role_id/:tracker_id', :controller => 'roles', :action => 'workflow'
15 15 map.connect 'help/:ctrl/:page', :controller => 'help'
16 16 #map.connect ':controller/:action/:id/:sort_key/:sort_order'
17 17
18 18 map.connect 'issues/:issue_id/relations/:action/:id', :controller => 'issue_relations'
19 19 map.connect 'projects/:project_id/issues/:action', :controller => 'issues'
20 20 map.connect 'projects/:project_id/news/:action', :controller => 'news'
21 21 map.connect 'projects/:project_id/documents/:action', :controller => 'documents'
22 22 map.connect 'projects/:project_id/boards/:action/:id', :controller => 'boards'
23 23 map.connect 'projects/:project_id/timelog/:action/:id', :controller => 'timelog'
24 24 map.connect 'boards/:board_id/topics/:action/:id', :controller => 'messages'
25 25
26 26 map.with_options :controller => 'repositories' do |omap|
27 27 omap.repositories_show 'repositories/browse/:id/*path', :action => 'browse'
28 28 omap.repositories_changes 'repositories/changes/:id/*path', :action => 'changes'
29 29 omap.repositories_diff 'repositories/diff/:id/*path', :action => 'diff'
30 30 omap.repositories_entry 'repositories/entry/:id/*path', :action => 'entry'
31 31 omap.repositories_entry 'repositories/annotate/:id/*path', :action => 'annotate'
32 32 omap.repositories_revision 'repositories/revision/:id/:rev', :action => 'revision'
33 33 end
34 34
35 map.connect 'attachments/:id', :controller => 'attachments', :action => 'show'
36
35 map.connect 'attachments/:id', :controller => 'attachments', :action => 'show', :id => /\d+/
36 map.connect 'attachments/:id/:filename', :controller => 'attachments', :action => 'show', :id => /\d+/, :filename => /.*/
37 map.connect 'attachments/download/:id/:filename', :controller => 'attachments', :action => 'download', :id => /\d+/, :filename => /.*/
38
37 39 # Allow downloading Web Service WSDL as a file with an extension
38 40 # instead of a file named 'wsdl'
39 41 map.connect ':controller/service.wsdl', :action => 'wsdl'
40 42
41 43
42 44 # Install the default route as the lowest priority.
43 45 map.connect ':controller/:action/:id'
44 46 end
@@ -1,64 +1,79
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2008 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 File.dirname(__FILE__) + '/../test_helper'
19 19 require 'attachments_controller'
20 20
21 21 # Re-raise errors caught by the controller.
22 22 class AttachmentsController; def rescue_action(e) raise e end; end
23 23
24 24
25 25 class AttachmentsControllerTest < Test::Unit::TestCase
26 26 fixtures :users, :projects, :issues, :attachments
27 27
28 28 def setup
29 29 @controller = AttachmentsController.new
30 30 @request = ActionController::TestRequest.new
31 31 @response = ActionController::TestResponse.new
32 32 Attachment.storage_path = "#{RAILS_ROOT}/test/fixtures/files"
33 33 User.current = nil
34 34 end
35 35
36 def test_routing
37 assert_routing('/attachments/1', :controller => 'attachments', :action => 'show', :id => '1')
38 assert_routing('/attachments/1/filename.ext', :controller => 'attachments', :action => 'show', :id => '1', :filename => 'filename.ext')
39 assert_routing('/attachments/download/1', :controller => 'attachments', :action => 'download', :id => '1')
40 assert_routing('/attachments/download/1/filename.ext', :controller => 'attachments', :action => 'download', :id => '1', :filename => 'filename.ext')
41 end
42
43 def test_recognizes
44 assert_recognizes({:controller => 'attachments', :action => 'show', :id => '1'}, '/attachments/1')
45 assert_recognizes({:controller => 'attachments', :action => 'show', :id => '1'}, '/attachments/show/1')
46 assert_recognizes({:controller => 'attachments', :action => 'show', :id => '1', :filename => 'filename.ext'}, '/attachments/1/filename.ext')
47 assert_recognizes({:controller => 'attachments', :action => 'download', :id => '1'}, '/attachments/download/1')
48 assert_recognizes({:controller => 'attachments', :action => 'download', :id => '1', :filename => 'filename.ext'},'/attachments/download/1/filename.ext')
49 end
50
36 51 def test_show_diff
37 52 get :show, :id => 5
38 53 assert_response :success
39 54 assert_template 'diff'
40 55 end
41 56
42 57 def test_show_text_file
43 58 get :show, :id => 4
44 59 assert_response :success
45 60 assert_template 'file'
46 61 end
47 62
48 63 def test_show_other
49 64 get :show, :id => 6
50 65 assert_response :success
51 66 assert_equal 'application/octet-stream', @response.content_type
52 67 end
53 68
54 69 def test_download_text_file
55 70 get :download, :id => 4
56 71 assert_response :success
57 72 assert_equal 'application/x-ruby', @response.content_type
58 73 end
59 74
60 75 def test_anonymous_on_private_private
61 76 get :download, :id => 7
62 77 assert_redirected_to 'account/login'
63 78 end
64 79 end
General Comments 0
You need to be logged in to leave comments. Login now