File::Path auf legacy Interface umgestellt, damit es auf Maschinen vor 2010 läuft.
[kivitendo-erp.git] / SL / SessionFile.pm
1 package SL::SessionFile;
2
3 use strict;
4
5 use parent qw(Rose::Object);
6
7 use Carp;
8 use File::Path qw(mkpath rmtree);
9 use English qw(-no_match_vars);
10 use IO::File;
11 use POSIX qw(strftime);
12
13 use Rose::Object::MakeMethods::Generic
14 (
15  scalar => [ qw(fh file_name) ],
16 );
17
18 sub new {
19   my ($class, $file_name, $mode) = @_;
20
21   my $self   = $class->SUPER::new;
22
23   my $path   = $self->prepare_path;
24   $file_name =~ s:.*/::g;
25   $file_name =  "${path}/${file_name}";
26
27   $self->fh(IO::File->new($file_name, $mode)) if $mode;
28   $self->file_name($file_name);
29
30   return $self;
31 }
32
33 sub exists {
34   my ($self) = @_;
35   return -f $self->file_name;
36 }
37
38 sub size {
39   my ($self) = @_;
40   return -s $self->file_name;
41 }
42
43 sub displayable_mtime {
44   my ($self) = @_;
45   return '' unless $self->exists;
46
47   my @mtime = localtime((stat $self->file_name)[9]);
48   return $::locale->format_date(\%::myconfig, $mtime[5] + 1900, $mtime[4] + 1, $mtime[3]) . ' ' . strftime('%H:%M:%S', @mtime);
49 }
50
51 sub get_path {
52   die "No session ID" unless $::auth->get_session_id;
53   return "users/session_files/" . $::auth->get_session_id;
54 }
55
56 sub prepare_path {
57   my $path = get_path();
58   return $path if -d $path;
59   mkpath $path;
60   die "Creating ${path} failed" unless -d $path;
61   return $path;
62 }
63
64 sub destroy_session {
65   my ($class, $session_id) = @_;
66
67   $session_id =~ s/[^a-z0-9]//gi;
68   rmtree "users/session_files/$session_id" if $session_id;
69 }
70
71 1;
72 __END__
73
74 =pod
75
76 =encoding utf8
77
78 =head1 NAME
79
80 SL::SessionFile - Create files that are removed when the session is
81 destroyed or expires
82
83 =head1 SYNOPSIS
84
85   use SL::SessionFile;
86
87   # Create a session file named "customer.csv" (relative names only)
88   my $sfile = SL::SessionFile->new("customer.csv", "w");
89   $sfile->fh->print("col1;col2;col3\n" .
90                     "value1;value2;value3\n");
91   $sfile->fh->close;
92
93   # Does temporary file exist?
94   my $sfile = SL::SessionFile->new("customer.csv");
95   if ($sfile->exists) {
96     print "file exists; size " . $sfile->size . " bytes; mtime " . $sfile->displayable_mtime . "\n";
97   }
98
99 A small class that wraps around files that only exist as long as the
100 user's session exists. The session expiration mechanism will delete
101 all session files when the session itself is removed due to expiry or
102 the user logging out.
103
104 Files are stored in session-specific folders in
105 C<users/session_files/SESSIONID>.
106
107 =head1 MEMBER FUNCTIONS
108
109 =over 4
110
111 =item C<new $file_name, [$mode]>
112
113 Create a new instance. C<$file_name> is a relative file name (path
114 components are stripped) to the session-specific temporary directory.
115
116 If C<$mode> is given then try to open the file as an instance of
117 C<IO::File>. C<$mode> is passed through to C<IO::File::new>.
118
119 =item C<fh>
120
121 Returns the instance of C<IO::File> associated with the file.
122
123 =item C<file_name>
124
125 Returns the full relative file name associated with this instance. If
126 it has been created for "customer.csv" then the value returned might
127 be C<users/session_files/e8789b98721347/customer.csv>.
128
129 =item C<exists>
130
131 Returns trueish if the file exists.
132
133 =item C<size>
134
135 Returns the file's size in bytes.
136
137 =item C<displayable_mtime>
138
139 Returns the modification time suitable for display (e.g. date
140 formatted according to the user's date format), e.g.
141 C<22.01.2011 14:12:22>.
142
143 =back
144
145 =head1 OBJECT FUNCTIONS
146
147 =over 4
148
149 =item C<get_path>
150
151 Returns the name of the session-specific directory used for file
152 storage relative to the Lx-Office installation folder.
153
154 =item C<prepare_path>
155
156 Creates all directories in C<get_path> if they do not exist. Returns
157 the same as C<get_path>.
158
159 =item C<destroy_session $id>
160
161 Removes all files and the directory belonging to the session C<$id>.
162
163 =back
164
165 =head1 BUGS
166
167 Nothing here yet.
168
169 =head1 AUTHOR
170
171 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
172
173 =cut