##// END OF EJS Templates
pdf: lib: add the method to return attachment from filename and encoding (#3261)...
Toshi MARUYAMA -
r7792:f936b7b5a4ec
parent child
Show More
@@ -517,6 +517,19 module Redmine
517 517 end
518 518 txt
519 519 end
520
521 def self.attach(attachments, filename, encoding)
522 filename_utf8 = Redmine::CodesetUtil.to_utf8(filename, encoding)
523 atta = nil
524 if filename_utf8 =~ /^[^\/"]+\.(gif|jpg|jpe|jpeg|png)$/i
525 atta = Attachment.latest_attach(attachments, filename_utf8)
526 end
527 if atta && atta.readable? && atta.visible?
528 return atta
529 else
530 return nil
531 end
532 end
520 533 end
521 534 end
522 535 end
@@ -208,3 +208,29 attachments_017:
208 208 filename: testfile.PNG
209 209 filesize: 3582
210 210 author_id: 2
211 attachments_018:
212 content_type: image/png
213 downloads: 0
214 created_on: 2011-01-23 16:14:50 +09:00
215 disk_filename: 101123161450_testfile_1.png
216 container_id: 14
217 digest: 8e0294de2441577c529f170b6fb8f638
218 id: 18
219 container_type: Issue
220 description: ""
221 filename: testγƒ†γ‚Ήγƒˆ.png
222 filesize: 2654
223 author_id: 2
224 attachments_019:
225 content_type: image/png
226 downloads: 0
227 created_on: 2011-02-23 16:14:50 +09:00
228 disk_filename: 101223161450_testfile_2.png
229 container_id: 14
230 digest: 6bc2963e8d7ea0d3e68d12d1fba3d6ca
231 id: 19
232 container_type: Issue
233 description: ""
234 filename: Testγƒ†γ‚Ήγƒˆ.PNG
235 filesize: 3582
236 author_id: 2
@@ -19,6 +19,8 require File.expand_path('../../../../../test_helper', __FILE__)
19 19 require 'iconv'
20 20
21 21 class PdfTest < ActiveSupport::TestCase
22 fixtures :users, :projects, :roles, :members, :member_roles,
23 :enabled_modules, :issues, :trackers, :attachments
22 24
23 25 def test_fix_text_encoding_nil
24 26 assert_equal '', Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(nil, "UTF-8")
@@ -87,4 +89,39 class PdfTest < ActiveSupport::TestCase
87 89 assert_equal "Texte encod? en ISO-8859-1", txt_1
88 90 assert_equal "?a?b?c?d?e test", txt_2
89 91 end
92
93 def test_attach
94 Attachment.storage_path = "#{Rails.root}/test/fixtures/files"
95
96 str2 = "\x83e\x83X\x83g"
97 str2.force_encoding("ASCII-8BIT") if str2.respond_to?(:force_encoding)
98 encoding = ( RUBY_PLATFORM == 'java' ? "SJIS" : "CP932" )
99
100 a1 = Attachment.find(17)
101 a2 = Attachment.find(19)
102
103 User.current = User.find(1)
104 assert a1.readable?
105 assert a1.visible?
106 assert a2.readable?
107 assert a2.visible?
108
109 aa1 = Redmine::Export::PDF::RDMPdfEncoding::attach(Attachment.all, "Testfile.PNG", "UTF-8")
110 assert_equal 17, aa1.id
111 aa2 = Redmine::Export::PDF::RDMPdfEncoding::attach(Attachment.all, "test#{str2}.png", encoding)
112 assert_equal 19, aa2.id
113
114 User.current = nil
115 assert a1.readable?
116 assert (! a1.visible?)
117 assert a2.readable?
118 assert (! a2.visible?)
119
120 aa1 = Redmine::Export::PDF::RDMPdfEncoding::attach(Attachment.all, "Testfile.PNG", "UTF-8")
121 assert_equal nil, aa1
122 aa2 = Redmine::Export::PDF::RDMPdfEncoding::attach(Attachment.all, "test#{str2}.png", encoding)
123 assert_equal nil, aa2
124
125 set_tmp_attachments_directory
126 end
90 127 end
General Comments 0
You need to be logged in to leave comments. Login now