X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FSessionFile.pm;h=839264074c67429e3dfb63842888216f940bc5e4;hb=3782a90c336bc6c506f572e607c8526cb5e79ea3;hp=12f498465b2d80223de6c33ec924ec212119e4ba;hpb=4344f4e30035852c6f28d78623511ad770010e08;p=kivitendo-erp.git diff --git a/SL/SessionFile.pm b/SL/SessionFile.pm index 12f498465..839264074 100644 --- a/SL/SessionFile.pm +++ b/SL/SessionFile.pm @@ -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); @@ -13,23 +13,43 @@ use POSIX qw(strftime); use Rose::Object::MakeMethods::Generic ( scalar => [ qw(fh file_name) ], + 'scalar --get_set_init' => [ qw(session_id) ], ); sub new { - my ($class, $file_name, $mode) = @_; + my ($class, $file_name, %params) = @_; my $self = $class->SUPER::new; + if ($params{session_id}) { + $self->session_id($params{session_id}) + } + my $path = $self->prepare_path; - $file_name =~ s:.*/::g; + $file_name =~ s{.*/}{}g; $file_name = "${path}/${file_name}"; - $self->fh(IO::File->new($file_name, $mode)) if $mode; $self->file_name($file_name); + 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)); + } + return $self; } +sub open { + my ($self, $mode) = @_; + return $self->fh(IO::File->new($self->file_name, $mode)); +} + sub exists { my ($self) = @_; return -f $self->file_name; @@ -49,23 +69,27 @@ sub displayable_mtime { } sub get_path { - die "No session ID" unless $::auth->get_session_id; - return "users/session_files/" . $::auth->get_session_id; + die "No session ID" unless $_[0]->session_id; + return "users/session_files/" . $_[0]->session_id; } sub prepare_path { - my $path = get_path(); + my $path = $_[0]->get_path; return $path if -d $path; - make_path $path; + mkpath $path; die "Creating ${path} failed" unless -d $path; return $path; } +sub init_session_id { + $::auth->get_session_id; +} + 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; @@ -85,7 +109,7 @@ destroyed or expires use SL::SessionFile; # Create a session file named "customer.csv" (relative names only) - my $sfile = SL::SessionFile->new("customer.csv", "w"); + my $sfile = SL::SessionFile->new('customer.csv', mode => 'w'); $sfile->fh->print("col1;col2;col3\n" . "value1;value2;value3\n"); $sfile->fh->close; @@ -108,13 +132,16 @@ C. =over 4 -=item C +=item C 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. C<$mode> is passed through to C. +If C<$params{mode}> is given then try to open the file as an instance +of C. C<${mode}> is passed through to C. + +If C<$params{encoding}> is given then the file is opened with the +appropriate encoding layer. =item C @@ -126,6 +153,10 @@ Returns the full relative file name associated with this instance. If it has been created for "customer.csv" then the value returned might be C. +=item C + +Opens the file_name given at creation with the given parameters. + =item C Returns trueish if the file exists. @@ -149,7 +180,7 @@ C<22.01.2011 14:12:22>. =item C Returns the name of the session-specific directory used for file -storage relative to the Lx-Office installation folder. +storage relative to the kivitendo installation folder. =item C