##// END OF EJS Templates
attachment: use repositories setting to convert contents character encoding (#2371)...
Toshi MARUYAMA -
r7746:77710ece26d8
parent child
Show More
@@ -1,59 +1,47
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 module AttachmentsHelper
18 module AttachmentsHelper
19 # Displays view/delete links to the attachments of the given object
19 # Displays view/delete links to the attachments of the given object
20 # Options:
20 # Options:
21 # :author -- author names are not displayed if set to false
21 # :author -- author names are not displayed if set to false
22 def link_to_attachments(container, options = {})
22 def link_to_attachments(container, options = {})
23 options.assert_valid_keys(:author)
23 options.assert_valid_keys(:author)
24
24
25 if container.attachments.any?
25 if container.attachments.any?
26 options = {:deletable => container.attachments_deletable?, :author => true}.merge(options)
26 options = {:deletable => container.attachments_deletable?, :author => true}.merge(options)
27 render :partial => 'attachments/links', :locals => {:attachments => container.attachments, :options => options}
27 render :partial => 'attachments/links', :locals => {:attachments => container.attachments, :options => options}
28 end
28 end
29 end
29 end
30
30
31 def to_utf8(str)
31 def to_utf8(str)
32 if str.respond_to?(:force_encoding)
32 Redmine::CodesetUtil.to_utf8_by_setting(str)
33 str.force_encoding('UTF-8')
34 return str if str.valid_encoding?
35 else
36 return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
37 end
38
39 begin
40 Iconv.conv('UTF-8//IGNORE', 'UTF-8', str + ' ')[0..-3]
41 rescue Iconv::InvalidEncoding
42 # "UTF-8//IGNORE" is not supported on some OS
43 str
44 end
45 end
33 end
46
34
47 def render_api_attachment(attachment, api)
35 def render_api_attachment(attachment, api)
48 api.attachment do
36 api.attachment do
49 api.id attachment.id
37 api.id attachment.id
50 api.filename attachment.filename
38 api.filename attachment.filename
51 api.filesize attachment.filesize
39 api.filesize attachment.filesize
52 api.content_type attachment.content_type
40 api.content_type attachment.content_type
53 api.description attachment.description
41 api.description attachment.description
54 api.content_url url_for(:controller => 'attachments', :action => 'download', :id => attachment, :filename => attachment.filename, :only_path => false)
42 api.content_url url_for(:controller => 'attachments', :action => 'download', :id => attachment, :filename => attachment.filename, :only_path => false)
55 api.author(:id => attachment.author.id, :name => attachment.author.name) if attachment.author
43 api.author(:id => attachment.author.id, :name => attachment.author.name) if attachment.author
56 api.created_on attachment.created_on
44 api.created_on attachment.created_on
57 end
45 end
58 end
46 end
59 end
47 end
@@ -1,213 +1,217
1 # encoding: utf-8
1 # encoding: utf-8
2 #
2 #
3 # Redmine - project management software
3 # Redmine - project management software
4 # Copyright (C) 2006-2011 Jean-Philippe Lang
4 # Copyright (C) 2006-2011 Jean-Philippe Lang
5 #
5 #
6 # This program is free software; you can redistribute it and/or
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
9 # of the License, or (at your option) any later version.
10 #
10 #
11 # This program is distributed in the hope that it will be useful,
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
14 # GNU General Public License for more details.
15 #
15 #
16 # You should have received a copy of the GNU General Public License
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
19
20 require File.expand_path('../../test_helper', __FILE__)
20 require File.expand_path('../../test_helper', __FILE__)
21 require 'attachments_controller'
21 require 'attachments_controller'
22
22
23 # Re-raise errors caught by the controller.
23 # Re-raise errors caught by the controller.
24 class AttachmentsController; def rescue_action(e) raise e end; end
24 class AttachmentsController; def rescue_action(e) raise e end; end
25
25
26 class AttachmentsControllerTest < ActionController::TestCase
26 class AttachmentsControllerTest < ActionController::TestCase
27 fixtures :users, :projects, :roles, :members, :member_roles,
27 fixtures :users, :projects, :roles, :members, :member_roles,
28 :enabled_modules, :issues, :trackers, :attachments,
28 :enabled_modules, :issues, :trackers, :attachments,
29 :versions, :wiki_pages, :wikis, :documents
29 :versions, :wiki_pages, :wikis, :documents
30
30
31 def setup
31 def setup
32 @controller = AttachmentsController.new
32 @controller = AttachmentsController.new
33 @request = ActionController::TestRequest.new
33 @request = ActionController::TestRequest.new
34 @response = ActionController::TestResponse.new
34 @response = ActionController::TestResponse.new
35 Attachment.storage_path = "#{Rails.root}/test/fixtures/files"
35 Attachment.storage_path = "#{Rails.root}/test/fixtures/files"
36 User.current = nil
36 User.current = nil
37 end
37 end
38
38
39 def test_show_diff
39 def test_show_diff
40 ['inline', 'sbs'].each do |dt|
40 ['inline', 'sbs'].each do |dt|
41 # 060719210727_changeset_utf8.diff
41 # 060719210727_changeset_utf8.diff
42 get :show, :id => 14, :type => dt
42 get :show, :id => 14, :type => dt
43 assert_response :success
43 assert_response :success
44 assert_template 'diff'
44 assert_template 'diff'
45 assert_equal 'text/html', @response.content_type
45 assert_equal 'text/html', @response.content_type
46 assert_tag 'th',
46 assert_tag 'th',
47 :attributes => {:class => /filename/},
47 :attributes => {:class => /filename/},
48 :content => /issues_controller.rb\t\(rΓ©vision 1484\)/
48 :content => /issues_controller.rb\t\(rΓ©vision 1484\)/
49 assert_tag 'td',
49 assert_tag 'td',
50 :attributes => {:class => /line-code/},
50 :attributes => {:class => /line-code/},
51 :content => /Demande créée avec succès/
51 :content => /Demande créée avec succès/
52 end
52 end
53 end
53 end
54
54
55 def test_show_diff_should_strip_non_utf8_content
55 def test_show_diff_replcace_cannot_convert_content
56 ['inline', 'sbs'].each do |dt|
56 with_settings :repositories_encodings => 'UTF-8' do
57 # 060719210727_changeset_iso8859-1.diff
57 ['inline', 'sbs'].each do |dt|
58 get :show, :id => 5, :type => dt
58 # 060719210727_changeset_iso8859-1.diff
59 assert_response :success
59 get :show, :id => 5
60 assert_template 'diff'
60 assert_response :success
61 assert_equal 'text/html', @response.content_type
61 assert_template 'diff'
62 assert_tag 'th',
62 assert_equal 'text/html', @response.content_type
63 :attributes => {:class => /filename/},
63 assert_tag 'th',
64 :content => /issues_controller.rb\t\(rvision 1484\)/
64 :attributes => {:class => "filename"},
65 assert_tag 'td',
65 :content => /issues_controller.rb\t\(r\?vision 1484\)/
66 :attributes => {:class => /line-code/},
66 assert_tag 'td',
67 :content => /Demande cre avec succs/
67 :attributes => {:class => /line-code/},
68 :content => /Demande cr\?\?e avec succ\?s/
69 end
68 end
70 end
69 end
71 end
70
72
71 def test_show_text_file
73 def test_show_text_file
72 get :show, :id => 4
74 get :show, :id => 4
73 assert_response :success
75 assert_response :success
74 assert_template 'file'
76 assert_template 'file'
75 assert_equal 'text/html', @response.content_type
77 assert_equal 'text/html', @response.content_type
76 end
78 end
77
79
78 def test_show_text_file_utf_8
80 def test_show_text_file_utf_8
79 set_tmp_attachments_directory
81 set_tmp_attachments_directory
80 a = Attachment.new(:container => Issue.find(1),
82 a = Attachment.new(:container => Issue.find(1),
81 :file => uploaded_test_file("japanese-utf-8.txt", "text/plain"),
83 :file => uploaded_test_file("japanese-utf-8.txt", "text/plain"),
82 :author => User.find(1))
84 :author => User.find(1))
83 assert a.save
85 assert a.save
84 assert_equal 'japanese-utf-8.txt', a.filename
86 assert_equal 'japanese-utf-8.txt', a.filename
85
87
86 str_japanese = "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e"
88 str_japanese = "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e"
87 str_japanese.force_encoding('UTF-8') if str_japanese.respond_to?(:force_encoding)
89 str_japanese.force_encoding('UTF-8') if str_japanese.respond_to?(:force_encoding)
88
90
89 get :show, :id => a.id
91 get :show, :id => a.id
90 assert_response :success
92 assert_response :success
91 assert_template 'file'
93 assert_template 'file'
92 assert_equal 'text/html', @response.content_type
94 assert_equal 'text/html', @response.content_type
93 assert_tag :tag => 'th',
95 assert_tag :tag => 'th',
94 :content => '1',
96 :content => '1',
95 :attributes => { :class => 'line-num' },
97 :attributes => { :class => 'line-num' },
96 :sibling => { :tag => 'td', :content => /#{str_japanese}/ }
98 :sibling => { :tag => 'td', :content => /#{str_japanese}/ }
97 end
99 end
98
100
99 def test_show_text_file_should_strip_non_utf8_content
101 def test_show_text_file_replcace_cannot_convert_content
100 set_tmp_attachments_directory
102 set_tmp_attachments_directory
101 a = Attachment.new(:container => Issue.find(1),
103 with_settings :repositories_encodings => 'UTF-8' do
102 :file => uploaded_test_file("iso8859-1.txt", "text/plain"),
104 a = Attachment.new(:container => Issue.find(1),
103 :author => User.find(1))
105 :file => uploaded_test_file("iso8859-1.txt", "text/plain"),
104 assert a.save
106 :author => User.find(1))
105 assert_equal 'iso8859-1.txt', a.filename
107 assert a.save
106
108 assert_equal 'iso8859-1.txt', a.filename
107 get :show, :id => a.id
109
108 assert_response :success
110 get :show, :id => a.id
109 assert_template 'file'
111 assert_response :success
110 assert_equal 'text/html', @response.content_type
112 assert_template 'file'
111 assert_tag :tag => 'th',
113 assert_equal 'text/html', @response.content_type
112 :content => '7',
114 assert_tag :tag => 'th',
113 :attributes => { :class => 'line-num' },
115 :content => '7',
114 :sibling => { :tag => 'td', :content => /Demande cre avec succs/ }
116 :attributes => { :class => 'line-num' },
117 :sibling => { :tag => 'td', :content => /Demande cr\?\?e avec succ\?s/ }
118 end
115 end
119 end
116
120
117 def test_show_text_file_should_send_if_too_big
121 def test_show_text_file_should_send_if_too_big
118 Setting.file_max_size_displayed = 512
122 Setting.file_max_size_displayed = 512
119 Attachment.find(4).update_attribute :filesize, 754.kilobyte
123 Attachment.find(4).update_attribute :filesize, 754.kilobyte
120
124
121 get :show, :id => 4
125 get :show, :id => 4
122 assert_response :success
126 assert_response :success
123 assert_equal 'application/x-ruby', @response.content_type
127 assert_equal 'application/x-ruby', @response.content_type
124 end
128 end
125
129
126 def test_show_other
130 def test_show_other
127 get :show, :id => 6
131 get :show, :id => 6
128 assert_response :success
132 assert_response :success
129 assert_equal 'application/octet-stream', @response.content_type
133 assert_equal 'application/octet-stream', @response.content_type
130 end
134 end
131
135
132 def test_show_file_from_private_issue_without_permission
136 def test_show_file_from_private_issue_without_permission
133 get :show, :id => 15
137 get :show, :id => 15
134 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2F15'
138 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2F15'
135 end
139 end
136
140
137 def test_show_file_from_private_issue_with_permission
141 def test_show_file_from_private_issue_with_permission
138 @request.session[:user_id] = 2
142 @request.session[:user_id] = 2
139 get :show, :id => 15
143 get :show, :id => 15
140 assert_response :success
144 assert_response :success
141 assert_tag 'h2', :content => /private.diff/
145 assert_tag 'h2', :content => /private.diff/
142 end
146 end
143
147
144 def test_download_text_file
148 def test_download_text_file
145 get :download, :id => 4
149 get :download, :id => 4
146 assert_response :success
150 assert_response :success
147 assert_equal 'application/x-ruby', @response.content_type
151 assert_equal 'application/x-ruby', @response.content_type
148 end
152 end
149
153
150 def test_download_should_assign_content_type_if_blank
154 def test_download_should_assign_content_type_if_blank
151 Attachment.find(4).update_attribute(:content_type, '')
155 Attachment.find(4).update_attribute(:content_type, '')
152
156
153 get :download, :id => 4
157 get :download, :id => 4
154 assert_response :success
158 assert_response :success
155 assert_equal 'text/x-ruby', @response.content_type
159 assert_equal 'text/x-ruby', @response.content_type
156 end
160 end
157
161
158 def test_download_missing_file
162 def test_download_missing_file
159 get :download, :id => 2
163 get :download, :id => 2
160 assert_response 404
164 assert_response 404
161 end
165 end
162
166
163 def test_anonymous_on_private_private
167 def test_anonymous_on_private_private
164 get :download, :id => 7
168 get :download, :id => 7
165 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fdownload%2F7'
169 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fdownload%2F7'
166 end
170 end
167
171
168 def test_destroy_issue_attachment
172 def test_destroy_issue_attachment
169 issue = Issue.find(3)
173 issue = Issue.find(3)
170 @request.session[:user_id] = 2
174 @request.session[:user_id] = 2
171
175
172 assert_difference 'issue.attachments.count', -1 do
176 assert_difference 'issue.attachments.count', -1 do
173 post :destroy, :id => 1
177 post :destroy, :id => 1
174 end
178 end
175 # no referrer
179 # no referrer
176 assert_redirected_to '/projects/ecookbook'
180 assert_redirected_to '/projects/ecookbook'
177 assert_nil Attachment.find_by_id(1)
181 assert_nil Attachment.find_by_id(1)
178 j = issue.journals.find(:first, :order => 'created_on DESC')
182 j = issue.journals.find(:first, :order => 'created_on DESC')
179 assert_equal 'attachment', j.details.first.property
183 assert_equal 'attachment', j.details.first.property
180 assert_equal '1', j.details.first.prop_key
184 assert_equal '1', j.details.first.prop_key
181 assert_equal 'error281.txt', j.details.first.old_value
185 assert_equal 'error281.txt', j.details.first.old_value
182 end
186 end
183
187
184 def test_destroy_wiki_page_attachment
188 def test_destroy_wiki_page_attachment
185 @request.session[:user_id] = 2
189 @request.session[:user_id] = 2
186 assert_difference 'Attachment.count', -1 do
190 assert_difference 'Attachment.count', -1 do
187 post :destroy, :id => 3
191 post :destroy, :id => 3
188 assert_response 302
192 assert_response 302
189 end
193 end
190 end
194 end
191
195
192 def test_destroy_project_attachment
196 def test_destroy_project_attachment
193 @request.session[:user_id] = 2
197 @request.session[:user_id] = 2
194 assert_difference 'Attachment.count', -1 do
198 assert_difference 'Attachment.count', -1 do
195 post :destroy, :id => 8
199 post :destroy, :id => 8
196 assert_response 302
200 assert_response 302
197 end
201 end
198 end
202 end
199
203
200 def test_destroy_version_attachment
204 def test_destroy_version_attachment
201 @request.session[:user_id] = 2
205 @request.session[:user_id] = 2
202 assert_difference 'Attachment.count', -1 do
206 assert_difference 'Attachment.count', -1 do
203 post :destroy, :id => 9
207 post :destroy, :id => 9
204 assert_response 302
208 assert_response 302
205 end
209 end
206 end
210 end
207
211
208 def test_destroy_without_permission
212 def test_destroy_without_permission
209 post :destroy, :id => 3
213 post :destroy, :id => 3
210 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fdestroy%2F3'
214 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fattachments%2Fdestroy%2F3'
211 assert Attachment.find_by_id(3)
215 assert Attachment.find_by_id(3)
212 end
216 end
213 end
217 end
General Comments 0
You need to be logged in to leave comments. Login now