Fehler im Namensraum err -> error
[kivitendo-erp.git] / SL / SessionFile.pm
index 12f4984..dbed415 100644 (file)
@@ -5,7 +5,7 @@ use strict;
 use parent qw(Rose::Object);
 
 use Carp;
-use File::Path qw(make_path remove_tree);
+use File::Path qw(mkpath rmtree);
 use English qw(-no_match_vars);
 use IO::File;
 use POSIX qw(strftime);
@@ -16,7 +16,7 @@ use Rose::Object::MakeMethods::Generic
 );
 
 sub new {
-  my ($class, $file_name, $mode) = @_;
+  my ($class, $file_name, %params) = @_;
 
   my $self   = $class->SUPER::new;
 
@@ -24,7 +24,17 @@ sub new {
   $file_name =~ s:.*/::g;
   $file_name =  "${path}/${file_name}";
 
-  $self->fh(IO::File->new($file_name, $mode)) if $mode;
+  if ($params{mode}) {
+    my $mode = $params{mode};
+
+    if ($params{encoding}) {
+      $params{encoding} =~ s/[^a-z0-9\-]//gi;
+      $mode .= ':encoding(' . $params{encoding} . ')';
+    }
+
+    $self->fh(IO::File->new($file_name, $mode));
+  }
+
   $self->file_name($file_name);
 
   return $self;
@@ -56,7 +66,7 @@ sub get_path {
 sub prepare_path {
   my $path = get_path();
   return $path if -d $path;
-  make_path $path;
+  mkpath $path;
   die "Creating ${path} failed" unless -d $path;
   return $path;
 }
@@ -65,7 +75,7 @@ sub destroy_session {
   my ($class, $session_id) = @_;
 
   $session_id =~ s/[^a-z0-9]//gi;
-  remove_tree "users/session_files/$session_id" if $session_id;
+  rmtree "users/session_files/$session_id" if $session_id;
 }
 
 1;
@@ -108,13 +118,16 @@ C<users/session_files/SESSIONID>.
 
 =over 4
 
-=item C<new $file_name, [$mode]>
+=item C<new $file_name, [%params]>
 
 Create a new instance. C<$file_name> is a relative file name (path
 components are stripped) to the session-specific temporary directory.
 
-If C<$mode> is given then try to open the file as an instance of
-C<IO::File>. C<$mode> is passed through to C<IO::File::new>.
+If C<$params{mode}> is given then try to open the file as an instance
+of C<IO::File>. C<${mode}> is passed through to C<IO::File::new>.
+
+If C<$params{encoding}> is given then the file is opened with the
+appropriate encoding layer.
 
 =item C<fh>