1 package SL::SessionFile;
 
   5 use parent qw(Rose::Object);
 
   8 use File::Path qw(mkpath rmtree);
 
   9 use English qw(-no_match_vars);
 
  11 use POSIX qw(strftime);
 
  13 use Rose::Object::MakeMethods::Generic
 
  15  scalar => [ qw(fh file_name) ],
 
  19   my ($class, $file_name, %params) = @_;
 
  21   my $self   = $class->SUPER::new;
 
  23   my $path   = $self->prepare_path;
 
  24   $file_name =~ s:.*/::g;
 
  25   $file_name =  "${path}/${file_name}";
 
  28     my $mode = $params{mode};
 
  30     if ($params{encoding}) {
 
  31       $params{encoding} =~ s/[^a-z0-9\-]//gi;
 
  32       $mode .= ':encoding(' . $params{encoding} . ')';
 
  35     $self->fh(IO::File->new($file_name, $mode));
 
  38   $self->file_name($file_name);
 
  45   return -f $self->file_name;
 
  50   return -s $self->file_name;
 
  53 sub displayable_mtime {
 
  55   return '' unless $self->exists;
 
  57   my @mtime = localtime((stat $self->file_name)[9]);
 
  58   return $::locale->format_date(\%::myconfig, $mtime[5] + 1900, $mtime[4] + 1, $mtime[3]) . ' ' . strftime('%H:%M:%S', @mtime);
 
  62   die "No session ID" unless $::auth->get_session_id;
 
  63   return "users/session_files/" . $::auth->get_session_id;
 
  67   my $path = get_path();
 
  68   return $path if -d $path;
 
  70   die "Creating ${path} failed" unless -d $path;
 
  75   my ($class, $session_id) = @_;
 
  77   $session_id =~ s/[^a-z0-9]//gi;
 
  78   rmtree "users/session_files/$session_id" if $session_id;
 
  90 SL::SessionFile - Create files that are removed when the session is
 
  97   # Create a session file named "customer.csv" (relative names only)
 
  98   my $sfile = SL::SessionFile->new("customer.csv", "w");
 
  99   $sfile->fh->print("col1;col2;col3\n" .
 
 100                     "value1;value2;value3\n");
 
 103   # Does temporary file exist?
 
 104   my $sfile = SL::SessionFile->new("customer.csv");
 
 105   if ($sfile->exists) {
 
 106     print "file exists; size " . $sfile->size . " bytes; mtime " . $sfile->displayable_mtime . "\n";
 
 109 A small class that wraps around files that only exist as long as the
 
 110 user's session exists. The session expiration mechanism will delete
 
 111 all session files when the session itself is removed due to expiry or
 
 112 the user logging out.
 
 114 Files are stored in session-specific folders in
 
 115 C<users/session_files/SESSIONID>.
 
 117 =head1 MEMBER FUNCTIONS
 
 121 =item C<new $file_name, [%params]>
 
 123 Create a new instance. C<$file_name> is a relative file name (path
 
 124 components are stripped) to the session-specific temporary directory.
 
 126 If C<$params{mode}> is given then try to open the file as an instance
 
 127 of C<IO::File>. C<${mode}> is passed through to C<IO::File::new>.
 
 129 If C<$params{encoding}> is given then the file is opened with the
 
 130 appropriate encoding layer.
 
 134 Returns the instance of C<IO::File> associated with the file.
 
 138 Returns the full relative file name associated with this instance. If
 
 139 it has been created for "customer.csv" then the value returned might
 
 140 be C<users/session_files/e8789b98721347/customer.csv>.
 
 144 Returns trueish if the file exists.
 
 148 Returns the file's size in bytes.
 
 150 =item C<displayable_mtime>
 
 152 Returns the modification time suitable for display (e.g. date
 
 153 formatted according to the user's date format), e.g.
 
 154 C<22.01.2011 14:12:22>.
 
 158 =head1 OBJECT FUNCTIONS
 
 164 Returns the name of the session-specific directory used for file
 
 165 storage relative to the Lx-Office installation folder.
 
 167 =item C<prepare_path>
 
 169 Creates all directories in C<get_path> if they do not exist. Returns
 
 170 the same as C<get_path>.
 
 172 =item C<destroy_session $id>
 
 174 Removes all files and the directory belonging to the session C<$id>.
 
 184 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>