From 49063cf5266b884908a2a917985bec414f08221f Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Tue, 18 Jan 2011 21:07:03 -0800 Subject: [PATCH] Fix UTF-8 handling in WebGUI::Storage class. Add tests for all methods that were checked. Fixes bug #12023. --- docs/changelog/7.x.x.txt | 1 + lib/WebGUI/Storage.pm | 6 +++- t/Storage/utf8_filenames.t | 58 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 t/Storage/utf8_filenames.t diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index 2857d886d..35aaef992 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -1,6 +1,7 @@ 7.10.8 - rfe #12016 for the top story as well - fixed #11965: Friend Manager only shows 15 people + - fixed #12023: International URLs of aattachments & files in folder 7.10.7 - rfe #10521: Use monospaced font in template edit textarea diff --git a/lib/WebGUI/Storage.pm b/lib/WebGUI/Storage.pm index 9901b2953..ce57e3e86 100644 --- a/lib/WebGUI/Storage.pm +++ b/lib/WebGUI/Storage.pm @@ -304,7 +304,10 @@ sub addFileFromFilesystem { if (! defined $pathToFile) { return undef; } + ##Handle UTF-8 filenames. + $pathToFile = Encode::encode_utf8($pathToFile); $pathToFile = Cwd::realpath($pathToFile); # trace any symbolic links + $pathToFile = Encode::decode_utf8($pathToFile); if (-d $pathToFile) { $self->session->log->error($pathToFile." is a directory, not a file."); return undef; @@ -372,6 +375,7 @@ sub addFileFromFormPost { return $filename; } my $clientFilename = $upload->filename; + $clientFilename = Encode::decode_utf8($clientFilename); next unless $clientFilename; next @@ -1080,7 +1084,7 @@ sub getFiles { callback => sub { my $obj = shift; my $rel = $obj->relative($dir); - my $str = $rel->stringify; + my $str = Encode::decode_utf8($rel->stringify); if (! $showAll ) { return if $str =~ /^thumb-/; return if $str =~ /^\./; diff --git a/t/Storage/utf8_filenames.t b/t/Storage/utf8_filenames.t new file mode 100644 index 000000000..d843258a2 --- /dev/null +++ b/t/Storage/utf8_filenames.t @@ -0,0 +1,58 @@ +#------------------------------------------------------------------- +# WebGUI is Copyright 2001-2009 Plain Black Corporation. +#------------------------------------------------------------------- +# Please read the legal notices (docs/legal.txt) and the license +# (docs/license.txt) that came with this distribution before using +# this software. +#------------------------------------------------------------------- +# http://www.plainblack.com info@plainblack.com +#------------------------------------------------------------------- + +#The goal of this test is to checkout uft8 handling in filenames. + +use FindBin; +use strict; +use lib "$FindBin::Bin/..//lib"; + +use WebGUI::Test; +use WebGUI::Session; +use WebGUI::Storage; + +use Test::More; +use Test::Deep; +use Encode; +use Cwd (); + +my $session = WebGUI::Test->session; + +plan tests => 4; + +my $storage = WebGUI::Storage->create($session); +WebGUI::Test->addToCleanup($storage); + +my $filename = "Viel_Spa\x{00DF}.txt"; +utf8::upgrade($filename); +$storage->addFileFromScalar($filename, 'some content'); +ok -e $storage->getPath($filename), 'addFileFromScalar: wrote filename with UTF-8 name'; + +my $filesystem_storage = WebGUI::Storage->create($session); +WebGUI::Test->addToCleanup($filesystem_storage); + +$filesystem_storage->addFileFromFilesystem($storage->getPath($filename)); +ok -e $filesystem_storage->getPath($filename), 'addFileFromFilesystem: brought file over with UTF-8 name'; + +cmp_deeply( + $filesystem_storage->getFiles(), + [ $filename ], + 'getFiles: returns names in UTF-8' +); + +my $copy_name = "Ca\x{00F1}on.txt"; +utf8::upgrade($copy_name); +$filesystem_storage->copyFile($filename, $copy_name); + +cmp_bag( + $filesystem_storage->getFiles(), + [ $filename, $copy_name ], + 'copyFile: copies files handling UTF-8 correctly' +);