diff --git a/lib/redmine/export/pdf.rb b/lib/redmine/export/pdf.rb index b5b4a4386..593398961 100644 --- a/lib/redmine/export/pdf.rb +++ b/lib/redmine/export/pdf.rb @@ -517,6 +517,19 @@ module Redmine end txt 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 diff --git a/test/fixtures/attachments.yml b/test/fixtures/attachments.yml index 57bd23822..32d7cabc1 100644 --- a/test/fixtures/attachments.yml +++ b/test/fixtures/attachments.yml @@ -208,3 +208,29 @@ attachments_017: filename: testfile.PNG filesize: 3582 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 diff --git a/test/unit/lib/redmine/export/pdf_test.rb b/test/unit/lib/redmine/export/pdf_test.rb index d80741cdd..9b89958b8 100644 --- a/test/unit/lib/redmine/export/pdf_test.rb +++ b/test/unit/lib/redmine/export/pdf_test.rb @@ -19,6 +19,8 @@ require File.expand_path('../../../../../test_helper', __FILE__) require 'iconv' class PdfTest < ActiveSupport::TestCase + fixtures :users, :projects, :roles, :members, :member_roles, + :enabled_modules, :issues, :trackers, :attachments def test_fix_text_encoding_nil 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 "?a?b?c?d?e test", txt_2 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