pdf: lib: add the method to return attachment from filename and encoding (#3261)

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@7912 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Toshi MARUYAMA 2011-11-24 09:43:01 +00:00
parent be29227c12
commit f936b7b5a4
3 changed files with 76 additions and 0 deletions

View File

@ -517,6 +517,19 @@ module Redmine
end end
txt txt
end end
def self.attach(attachments, filename, encoding)
filename_utf8 = Redmine::CodesetUtil.to_utf8(filename, encoding)
atta = nil
if filename_utf8 =~ /^[^\/"]+\.(gif|jpg|jpe|jpeg|png)$/i
atta = Attachment.latest_attach(attachments, filename_utf8)
end
if atta && atta.readable? && atta.visible?
return atta
else
return nil
end
end
end end
end end
end end

View File

@ -208,3 +208,29 @@ attachments_017:
filename: testfile.PNG filename: testfile.PNG
filesize: 3582 filesize: 3582
author_id: 2 author_id: 2
attachments_018:
content_type: image/png
downloads: 0
created_on: 2011-01-23 16:14:50 +09:00
disk_filename: 101123161450_testfile_1.png
container_id: 14
digest: 8e0294de2441577c529f170b6fb8f638
id: 18
container_type: Issue
description: ""
filename: testテスト.png
filesize: 2654
author_id: 2
attachments_019:
content_type: image/png
downloads: 0
created_on: 2011-02-23 16:14:50 +09:00
disk_filename: 101223161450_testfile_2.png
container_id: 14
digest: 6bc2963e8d7ea0d3e68d12d1fba3d6ca
id: 19
container_type: Issue
description: ""
filename: Testテスト.PNG
filesize: 3582
author_id: 2

View File

@ -19,6 +19,8 @@ require File.expand_path('../../../../../test_helper', __FILE__)
require 'iconv' require 'iconv'
class PdfTest < ActiveSupport::TestCase class PdfTest < ActiveSupport::TestCase
fixtures :users, :projects, :roles, :members, :member_roles,
:enabled_modules, :issues, :trackers, :attachments
def test_fix_text_encoding_nil def test_fix_text_encoding_nil
assert_equal '', Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(nil, "UTF-8") assert_equal '', Redmine::Export::PDF::RDMPdfEncoding::rdm_from_utf8(nil, "UTF-8")
@ -87,4 +89,39 @@ class PdfTest < ActiveSupport::TestCase
assert_equal "Texte encod? en ISO-8859-1", txt_1 assert_equal "Texte encod? en ISO-8859-1", txt_1
assert_equal "?a?b?c?d?e test", txt_2 assert_equal "?a?b?c?d?e test", txt_2
end end
def test_attach
Attachment.storage_path = "#{Rails.root}/test/fixtures/files"
str2 = "\x83e\x83X\x83g"
str2.force_encoding("ASCII-8BIT") if str2.respond_to?(:force_encoding)
encoding = ( RUBY_PLATFORM == 'java' ? "SJIS" : "CP932" )
a1 = Attachment.find(17)
a2 = Attachment.find(19)
User.current = User.find(1)
assert a1.readable?
assert a1.visible?
assert a2.readable?
assert a2.visible?
aa1 = Redmine::Export::PDF::RDMPdfEncoding::attach(Attachment.all, "Testfile.PNG", "UTF-8")
assert_equal 17, aa1.id
aa2 = Redmine::Export::PDF::RDMPdfEncoding::attach(Attachment.all, "test#{str2}.png", encoding)
assert_equal 19, aa2.id
User.current = nil
assert a1.readable?
assert (! a1.visible?)
assert a2.readable?
assert (! a2.visible?)
aa1 = Redmine::Export::PDF::RDMPdfEncoding::attach(Attachment.all, "Testfile.PNG", "UTF-8")
assert_equal nil, aa1
aa2 = Redmine::Export::PDF::RDMPdfEncoding::attach(Attachment.all, "test#{str2}.png", encoding)
assert_equal nil, aa2
set_tmp_attachments_directory
end
end end