##// END OF EJS Templates
Strip non utf8 content when displaying diff (ruby1.9 compatibility)....
Jean-Philippe Lang -
r4484:488285e12321
parent child
Show More
@@ -0,0 +1,13
1 Index: trunk/app/controllers/issues_controller.rb
2 ===================================================================
3 --- trunk/app/controllers/issues_controller.rb (révision 1483)
4 +++ trunk/app/controllers/issues_controller.rb (révision 1484)
5 @@ -149,7 +149,7 @@
6 attach_files(@issue, params[:attachments])
7 flash[:notice] = 'Demande créée avec succès'
8 Mailer.deliver_issue_add(@issue) if Setting.notified_events.include?('issue_added')
9 - redirect_to :controller => 'issues', :action => 'show', :id => @issue, :project_id => @project
10 + redirect_to :controller => 'issues', :action => 'show', :id => @issue
11 return
12 end
13 end
@@ -1,34 +1,40
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 module AttachmentsHelper
19 19 # Displays view/delete links to the attachments of the given object
20 20 # Options:
21 21 # :author -- author names are not displayed if set to false
22 22 def link_to_attachments(container, options = {})
23 23 options.assert_valid_keys(:author)
24 24
25 25 if container.attachments.any?
26 26 options = {:deletable => container.attachments_deletable?, :author => true}.merge(options)
27 27 render :partial => 'attachments/links', :locals => {:attachments => container.attachments, :options => options}
28 28 end
29 29 end
30 30
31 31 def to_utf8(str)
32 str
32 return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
33 begin
34 Iconv.conv('UTF-8//IGNORE', 'UTF-8', str + ' ')[0..-3]
35 rescue Iconv::InvalidEncoding
36 # "UTF-8//IGNORE" is not supported on some OS
37 str
38 end
33 39 end
34 40 end
@@ -1,67 +1,67
1 1 <% diff = Redmine::UnifiedDiff.new(diff, :type => diff_type, :max_lines => Setting.diff_max_lines_displayed.to_i) -%>
2 2 <% diff.each do |table_file| -%>
3 3 <div class="autoscroll">
4 4 <% if diff_type == 'sbs' -%>
5 5 <table class="filecontent">
6 6 <thead>
7 <tr><th colspan="4" class="filename"><%= table_file.file_name %></th></tr>
7 <tr><th colspan="4" class="filename"><%=to_utf8 table_file.file_name %></th></tr>
8 8 </thead>
9 9 <tbody>
10 10 <% prev_line_left, prev_line_right = nil, nil -%>
11 11 <% table_file.keys.sort.each do |key| -%>
12 12 <% if prev_line_left && prev_line_right && (table_file[key].nb_line_left != prev_line_left+1) && (table_file[key].nb_line_right != prev_line_right+1) -%>
13 13 <tr class="spacing">
14 14 <th class="line-num">...</th><td></td><th class="line-num">...</th><td></td>
15 15 <% end -%>
16 16 <tr>
17 17 <th class="line-num"><%= table_file[key].nb_line_left %></th>
18 18 <td class="line-code <%= table_file[key].type_diff_left %>">
19 19 <pre><%=to_utf8 table_file[key].line_left %></pre>
20 20 </td>
21 21 <th class="line-num"><%= table_file[key].nb_line_right %></th>
22 22 <td class="line-code <%= table_file[key].type_diff_right %>">
23 23 <pre><%=to_utf8 table_file[key].line_right %></pre>
24 24 </td>
25 25 </tr>
26 26 <% prev_line_left, prev_line_right = table_file[key].nb_line_left.to_i, table_file[key].nb_line_right.to_i -%>
27 27 <% end -%>
28 28 </tbody>
29 29 </table>
30 30
31 31 <% else -%>
32 32 <table class="filecontent syntaxhl">
33 33 <thead>
34 <tr><th colspan="3" class="filename"><%= table_file.file_name %></th></tr>
34 <tr><th colspan="3" class="filename"><%=to_utf8 table_file.file_name %></th></tr>
35 35 </thead>
36 36 <tbody>
37 37 <% prev_line_left, prev_line_right = nil, nil -%>
38 38 <% table_file.keys.sort.each do |key, line| %>
39 39 <% if prev_line_left && prev_line_right && (table_file[key].nb_line_left != prev_line_left+1) && (table_file[key].nb_line_right != prev_line_right+1) -%>
40 40 <tr class="spacing">
41 41 <th class="line-num">...</th><th class="line-num">...</th><td></td>
42 42 </tr>
43 43 <% end -%>
44 44 <tr>
45 45 <th class="line-num"><%= table_file[key].nb_line_left %></th>
46 46 <th class="line-num"><%= table_file[key].nb_line_right %></th>
47 47 <% if table_file[key].line_left.empty? -%>
48 48 <td class="line-code <%= table_file[key].type_diff_right %>">
49 49 <pre><%=to_utf8 table_file[key].line_right %></pre>
50 50 </td>
51 51 <% else -%>
52 52 <td class="line-code <%= table_file[key].type_diff_left %>">
53 53 <pre><%=to_utf8 table_file[key].line_left %></pre>
54 54 </td>
55 55 <% end -%>
56 56 </tr>
57 57 <% prev_line_left = table_file[key].nb_line_left.to_i if table_file[key].nb_line_left.to_i > 0 -%>
58 58 <% prev_line_right = table_file[key].nb_line_right.to_i if table_file[key].nb_line_right.to_i > 0 -%>
59 59 <% end -%>
60 60 </tbody>
61 61 </table>
62 62 <% end -%>
63 63
64 64 </div>
65 65 <% end -%>
66 66
67 67 <%= l(:text_diff_truncated) if diff.truncated? %>
@@ -1,159 +1,171
1 1 ---
2 2 attachments_001:
3 3 created_on: 2006-07-19 21:07:27 +02:00
4 4 downloads: 0
5 5 content_type: text/plain
6 6 disk_filename: 060719210727_error281.txt
7 7 container_id: 3
8 8 digest: b91e08d0cf966d5c6ff411bd8c4cc3a2
9 9 id: 1
10 10 container_type: Issue
11 11 filesize: 28
12 12 filename: error281.txt
13 13 author_id: 2
14 14 attachments_002:
15 15 created_on: 2007-01-27 15:08:27 +01:00
16 16 downloads: 0
17 17 content_type: text/plain
18 18 disk_filename: 060719210727_document.txt
19 19 container_id: 1
20 20 digest: b91e08d0cf966d5c6ff411bd8c4cc3a2
21 21 id: 2
22 22 container_type: Document
23 23 filesize: 28
24 24 filename: document.txt
25 25 author_id: 2
26 26 attachments_003:
27 27 created_on: 2006-07-19 21:07:27 +02:00
28 28 downloads: 0
29 29 content_type: image/gif
30 30 disk_filename: 060719210727_logo.gif
31 31 container_id: 4
32 32 digest: b91e08d0cf966d5c6ff411bd8c4cc3a2
33 33 id: 3
34 34 container_type: WikiPage
35 35 filesize: 280
36 36 filename: logo.gif
37 37 description: This is a logo
38 38 author_id: 2
39 39 attachments_004:
40 40 created_on: 2006-07-19 21:07:27 +02:00
41 41 container_type: Issue
42 42 container_id: 3
43 43 downloads: 0
44 44 disk_filename: 060719210727_source.rb
45 45 digest: b91e08d0cf966d5c6ff411bd8c4cc3a2
46 46 id: 4
47 47 filesize: 153
48 48 filename: source.rb
49 49 author_id: 2
50 50 description: This is a Ruby source file
51 51 content_type: application/x-ruby
52 52 attachments_005:
53 53 created_on: 2006-07-19 21:07:27 +02:00
54 54 container_type: Issue
55 55 container_id: 3
56 56 downloads: 0
57 disk_filename: 060719210727_changeset.diff
57 disk_filename: 060719210727_changeset_iso8859-1.diff
58 58 digest: b91e08d0cf966d5c6ff411bd8c4cc3a2
59 59 id: 5
60 60 filesize: 687
61 filename: changeset.diff
61 filename: changeset_iso8859-1.diff
62 62 author_id: 2
63 63 content_type: text/x-diff
64 64 attachments_006:
65 65 created_on: 2006-07-19 21:07:27 +02:00
66 66 container_type: Issue
67 67 container_id: 3
68 68 downloads: 0
69 69 disk_filename: 060719210727_archive.zip
70 70 digest: b91e08d0cf966d5c6ff411bd8c4cc3a2
71 71 id: 6
72 72 filesize: 157
73 73 filename: archive.zip
74 74 author_id: 2
75 75 content_type: application/octet-stream
76 76 attachments_007:
77 77 created_on: 2006-07-19 21:07:27 +02:00
78 78 container_type: Issue
79 79 container_id: 4
80 80 downloads: 0
81 81 disk_filename: 060719210727_archive.zip
82 82 digest: b91e08d0cf966d5c6ff411bd8c4cc3a2
83 83 id: 7
84 84 filesize: 157
85 85 filename: archive.zip
86 86 author_id: 1
87 87 content_type: application/octet-stream
88 88 attachments_008:
89 89 created_on: 2006-07-19 21:07:27 +02:00
90 90 container_type: Project
91 91 container_id: 1
92 92 downloads: 0
93 93 disk_filename: 060719210727_project_file.zip
94 94 digest: b91e08d0cf966d5c6ff411bd8c4cc3a2
95 95 id: 8
96 96 filesize: 320
97 97 filename: project_file.zip
98 98 author_id: 2
99 99 content_type: application/octet-stream
100 100 attachments_009:
101 101 created_on: 2006-07-19 21:07:27 +02:00
102 102 container_type: Version
103 103 container_id: 1
104 104 downloads: 0
105 105 disk_filename: 060719210727_version_file.zip
106 106 digest: b91e08d0cf966d5c6ff411bd8c4cc3a2
107 107 id: 9
108 108 filesize: 452
109 109 filename: version_file.zip
110 110 author_id: 2
111 111 content_type: application/octet-stream
112 112 attachments_010:
113 113 created_on: 2006-07-19 21:07:27 +02:00
114 114 container_type: Issue
115 115 container_id: 2
116 116 downloads: 0
117 117 disk_filename: 060719210727_picture.jpg
118 118 digest: b91e08d0cf966d5c6ff411bd8c4cc3a2
119 119 id: 10
120 120 filesize: 452
121 121 filename: picture.jpg
122 122 author_id: 2
123 123 content_type: image/jpeg
124 124 attachments_011:
125 125 created_on: 2007-02-12 15:08:27 +01:00
126 126 container_type: Document
127 127 container_id: 1
128 128 downloads: 0
129 129 disk_filename: 060719210727_picture.jpg
130 130 digest: b91e08d0cf966d5c6ff411bd8c4cc3a2
131 131 id: 11
132 132 filesize: 452
133 133 filename: picture.jpg
134 134 author_id: 2
135 135 content_type: image/jpeg
136 136 attachments_012:
137 137 created_on: 2006-07-19 21:07:27 +02:00
138 138 container_type: Version
139 139 container_id: 1
140 140 downloads: 0
141 141 disk_filename: 060719210727_version_file.zip
142 142 digest: b91e08d0cf966d5c6ff411bd8c4cc3a2
143 143 id: 12
144 144 filesize: 452
145 145 filename: version_file.zip
146 146 author_id: 2
147 147 content_type: application/octet-stream
148 148 attachments_013:
149 149 created_on: 2006-07-19 21:07:27 +02:00
150 150 container_type: Message
151 151 container_id: 1
152 152 downloads: 0
153 153 disk_filename: 060719210727_foo.zip
154 154 digest: b91e08d0cf966d5c6ff411bd8c4cc3a2
155 155 id: 13
156 156 filesize: 452
157 157 filename: foo.zip
158 158 author_id: 2
159 159 content_type: application/octet-stream
160 attachments_014:
161 created_on: 2006-07-19 21:07:27 +02:00
162 container_type: Issue
163 container_id: 3
164 downloads: 0
165 disk_filename: 060719210727_changeset_utf8.diff
166 digest: b91e08d0cf966d5c6ff411bd8c4cc3a2
167 id: 14
168 filesize: 687
169 filename: changeset_utf8.diff
170 author_id: 2
171 content_type: text/x-diff
@@ -1,13 +1,13
1 1 Index: trunk/app/controllers/issues_controller.rb
2 2 ===================================================================
3 --- trunk/app/controllers/issues_controller.rb (r�vision 1483)
4 +++ trunk/app/controllers/issues_controller.rb (r�vision 1484)
3 --- trunk/app/controllers/issues_controller.rb (r�vision 1483)
4 +++ trunk/app/controllers/issues_controller.rb (r�vision 1484)
5 5 @@ -149,7 +149,7 @@
6 6 attach_files(@issue, params[:attachments])
7 flash[:notice] = l(:notice_successful_create)
7 flash[:notice] = 'Demande cr��e avec succ�s'
8 8 Mailer.deliver_issue_add(@issue) if Setting.notified_events.include?('issue_added')
9 9 - redirect_to :controller => 'issues', :action => 'show', :id => @issue, :project_id => @project
10 10 + redirect_to :controller => 'issues', :action => 'show', :id => @issue
11 11 return
12 12 end
13 13 end
@@ -1,135 +1,158
1 # redMine - project management software
2 # Copyright (C) 2006-2008 Jean-Philippe Lang
1 # encoding: utf-8
2 #
3 # Redmine - project management software
4 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 5 #
4 6 # This program is free software; you can redistribute it and/or
5 7 # modify it under the terms of the GNU General Public License
6 8 # as published by the Free Software Foundation; either version 2
7 9 # of the License, or (at your option) any later version.
8 10 #
9 11 # This program is distributed in the hope that it will be useful,
10 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 14 # GNU General Public License for more details.
13 15 #
14 16 # You should have received a copy of the GNU General Public License
15 17 # along with this program; if not, write to the Free Software
16 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 19
18 20 require File.expand_path('../../test_helper', __FILE__)
19 21 require 'attachments_controller'
20 22
21 23 # Re-raise errors caught by the controller.
22 24 class AttachmentsController; def rescue_action(e) raise e end; end
23 25
24 26
25 27 class AttachmentsControllerTest < ActionController::TestCase
26 28 fixtures :users, :projects, :roles, :members, :member_roles, :enabled_modules, :issues, :trackers, :attachments,
27 29 :versions, :wiki_pages, :wikis, :documents
28 30
29 31 def setup
30 32 @controller = AttachmentsController.new
31 33 @request = ActionController::TestRequest.new
32 34 @response = ActionController::TestResponse.new
33 35 Attachment.storage_path = "#{RAILS_ROOT}/test/fixtures/files"
34 36 User.current = nil
35 37 end
36 38
37 39 def test_show_diff
38 get :show, :id => 5
40 get :show, :id => 14 # 060719210727_changeset_utf8.diff
41 assert_response :success
42 assert_template 'diff'
43 assert_equal 'text/html', @response.content_type
44
45 assert_tag 'th',
46 :attributes => {:class => /filename/},
47 :content => /issues_controller.rb\t\(révision 1484\)/
48 assert_tag 'td',
49 :attributes => {:class => /line-code/},
50 :content => /Demande créée avec succès/
51 end
52
53 def test_show_diff_should_strip_non_utf8_content
54 get :show, :id => 5 # 060719210727_changeset_iso8859-1.diff
39 55 assert_response :success
40 56 assert_template 'diff'
41 57 assert_equal 'text/html', @response.content_type
58
59 assert_tag 'th',
60 :attributes => {:class => /filename/},
61 :content => /issues_controller.rb\t\(rvision 1484\)/
62 assert_tag 'td',
63 :attributes => {:class => /line-code/},
64 :content => /Demande cre avec succs/
42 65 end
43 66
44 67 def test_show_text_file
45 68 get :show, :id => 4
46 69 assert_response :success
47 70 assert_template 'file'
48 71 assert_equal 'text/html', @response.content_type
49 72 end
50 73
51 74 def test_show_text_file_should_send_if_too_big
52 75 Setting.file_max_size_displayed = 512
53 76 Attachment.find(4).update_attribute :filesize, 754.kilobyte
54 77
55 78 get :show, :id => 4
56 79 assert_response :success
57 80 assert_equal 'application/x-ruby', @response.content_type
58 81 end
59 82
60 83 def test_show_other
61 84 get :show, :id => 6
62 85 assert_response :success
63 86 assert_equal 'application/octet-stream', @response.content_type
64 87 end
65 88
66 89 def test_download_text_file
67 90 get :download, :id => 4
68 91 assert_response :success
69 92 assert_equal 'application/x-ruby', @response.content_type
70 93 end
71 94
72 95 def test_download_should_assign_content_type_if_blank
73 96 Attachment.find(4).update_attribute(:content_type, '')
74 97
75 98 get :download, :id => 4
76 99 assert_response :success
77 100 assert_equal 'text/x-ruby', @response.content_type
78 101 end
79 102
80 103 def test_download_missing_file
81 104 get :download, :id => 2
82 105 assert_response 404
83 106 end
84 107
85 108 def test_anonymous_on_private_private
86 109 get :download, :id => 7
87 110 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fdownload%2F7'
88 111 end
89 112
90 113 def test_destroy_issue_attachment
91 114 issue = Issue.find(3)
92 115 @request.session[:user_id] = 2
93 116
94 117 assert_difference 'issue.attachments.count', -1 do
95 118 post :destroy, :id => 1
96 119 end
97 120 # no referrer
98 121 assert_redirected_to '/projects/ecookbook'
99 122 assert_nil Attachment.find_by_id(1)
100 123 j = issue.journals.find(:first, :order => 'created_on DESC')
101 124 assert_equal 'attachment', j.details.first.property
102 125 assert_equal '1', j.details.first.prop_key
103 126 assert_equal 'error281.txt', j.details.first.old_value
104 127 end
105 128
106 129 def test_destroy_wiki_page_attachment
107 130 @request.session[:user_id] = 2
108 131 assert_difference 'Attachment.count', -1 do
109 132 post :destroy, :id => 3
110 133 assert_response 302
111 134 end
112 135 end
113 136
114 137 def test_destroy_project_attachment
115 138 @request.session[:user_id] = 2
116 139 assert_difference 'Attachment.count', -1 do
117 140 post :destroy, :id => 8
118 141 assert_response 302
119 142 end
120 143 end
121 144
122 145 def test_destroy_version_attachment
123 146 @request.session[:user_id] = 2
124 147 assert_difference 'Attachment.count', -1 do
125 148 post :destroy, :id => 9
126 149 assert_response 302
127 150 end
128 151 end
129 152
130 153 def test_destroy_without_permission
131 154 post :destroy, :id => 3
132 155 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fdestroy%2F3'
133 156 assert Attachment.find_by_id(3)
134 157 end
135 158 end
General Comments 0
You need to be logged in to leave comments. Login now