@@ -1,93 +1,95 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2011 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 |
|
20 | 20 | before_filter :file_readable, :read_authorize, :except => :destroy |
|
21 | 21 | before_filter :delete_authorize, :only => :destroy |
|
22 | 22 | |
|
23 | 23 | accept_api_auth :show, :download |
|
24 | 24 | |
|
25 | 25 | def show |
|
26 | 26 | respond_to do |format| |
|
27 | 27 | format.html { |
|
28 | 28 | if @attachment.is_diff? |
|
29 | 29 | @diff = File.new(@attachment.diskfile, "rb").read |
|
30 | @diff_type = params[:type] || User.current.pref[:diff_type] || 'inline' | |
|
31 | @diff_type = 'inline' unless %w(inline sbs).include?(@diff_type) | |
|
30 | 32 | render :action => 'diff' |
|
31 | 33 | elsif @attachment.is_text? && @attachment.filesize <= Setting.file_max_size_displayed.to_i.kilobyte |
|
32 | 34 | @content = File.new(@attachment.diskfile, "rb").read |
|
33 | 35 | render :action => 'file' |
|
34 | 36 | else |
|
35 | 37 | download |
|
36 | 38 | end |
|
37 | 39 | } |
|
38 | 40 | format.api |
|
39 | 41 | end |
|
40 | 42 | end |
|
41 | 43 | |
|
42 | 44 | def download |
|
43 | 45 | if @attachment.container.is_a?(Version) || @attachment.container.is_a?(Project) |
|
44 | 46 | @attachment.increment_download |
|
45 | 47 | end |
|
46 | 48 | |
|
47 | 49 | # images are sent inline |
|
48 | 50 | send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename), |
|
49 | 51 | :type => detect_content_type(@attachment), |
|
50 | 52 | :disposition => (@attachment.image? ? 'inline' : 'attachment') |
|
51 | 53 | |
|
52 | 54 | end |
|
53 | 55 | |
|
54 | 56 | verify :method => :post, :only => :destroy |
|
55 | 57 | def destroy |
|
56 | 58 | # Make sure association callbacks are called |
|
57 | 59 | @attachment.container.attachments.delete(@attachment) |
|
58 | 60 | redirect_to :back |
|
59 | 61 | rescue ::ActionController::RedirectBackError |
|
60 | 62 | redirect_to :controller => 'projects', :action => 'show', :id => @project |
|
61 | 63 | end |
|
62 | 64 | |
|
63 | 65 | private |
|
64 | 66 | def find_project |
|
65 | 67 | @attachment = Attachment.find(params[:id]) |
|
66 | 68 | # Show 404 if the filename in the url is wrong |
|
67 | 69 | raise ActiveRecord::RecordNotFound if params[:filename] && params[:filename] != @attachment.filename |
|
68 | 70 | @project = @attachment.project |
|
69 | 71 | rescue ActiveRecord::RecordNotFound |
|
70 | 72 | render_404 |
|
71 | 73 | end |
|
72 | 74 | |
|
73 | 75 | # Checks that the file exists and is readable |
|
74 | 76 | def file_readable |
|
75 | 77 | @attachment.readable? ? true : render_404 |
|
76 | 78 | end |
|
77 | 79 | |
|
78 | 80 | def read_authorize |
|
79 | 81 | @attachment.visible? ? true : deny_access |
|
80 | 82 | end |
|
81 | 83 | |
|
82 | 84 | def delete_authorize |
|
83 | 85 | @attachment.deletable? ? true : deny_access |
|
84 | 86 | end |
|
85 | 87 | |
|
86 | 88 | def detect_content_type(attachment) |
|
87 | 89 | content_type = attachment.content_type |
|
88 | 90 | if content_type.blank? |
|
89 | 91 | content_type = Redmine::MimeType.of(attachment.filename) |
|
90 | 92 | end |
|
91 | 93 | content_type.to_s |
|
92 | 94 | end |
|
93 | 95 | end |
@@ -1,17 +1,24 | |||
|
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"><%= link_to_user(@attachment.author) %>, <%= format_time(@attachment.created_on) %></span></p> |
|
6 | 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 | ||
|
9 | 8 | </div> |
|
10 | | |
|
9 | <p> | |
|
10 | <% form_tag({}, :method => 'get') do %> | |
|
11 | <label><%= l(:label_view_diff) %></label> | |
|
12 | <%= select_tag 'type', | |
|
13 | options_for_select( | |
|
14 | [[l(:label_diff_inline), "inline"], [l(:label_diff_side_by_side), "sbs"]], @diff_type), | |
|
15 | :onchange => "if (this.value != '') {this.form.submit()}" %> | |
|
16 | <% end %> | |
|
17 | </p> | |
|
11 | 18 | <%= render :partial => 'common/diff', :locals => {:diff => @diff, :diff_type => @diff_type} %> |
|
12 | 19 | |
|
13 | 20 | <% html_title @attachment.filename %> |
|
14 | 21 | |
|
15 | 22 | <% content_for :header_tags do -%> |
|
16 | 23 | <%= stylesheet_link_tag "scm" -%> |
|
17 | 24 | <% end -%> |
General Comments 0
You need to be logged in to leave comments.
Login now