e5922c27363729c3e4e08a4bd5c9da9fb4023be9
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Request / Upload.pm
1 package Catalyst::Request::Upload;
2
3 use strict;
4 use base 'Class::Accessor::Fast';
5
6 use Catalyst::Exception;
7 use File::Copy ();
8 use IO::File   ();
9 use File::Spec::Unix;
10
11 __PACKAGE__->mk_accessors(qw/filename headers size tempname type basename/);
12
13 sub new { shift->SUPER::new( ref( $_[0] ) ? $_[0] : {@_} ) }
14
15 =head1 NAME
16
17 Catalyst::Request::Upload - handles file upload requests
18
19 =head1 SYNOPSIS
20
21     $upload->basename;
22     $upload->copy_to;
23     $upload->fh;
24     $upload->filename;
25     $upload->headers;
26     $upload->link_to;
27     $upload->size;
28     $upload->slurp;
29     $upload->tempname;
30     $upload->type;
31
32 To specify where Catalyst should put the temporary files, set the 'uploadtmp'
33 option in the Catalyst config. If unset, Catalyst will use the system temp dir.
34
35     __PACKAGE__->config( uploadtmp => '/path/to/tmpdir' );
36
37 It is provided a way to have configurable temporary directory.
38 If there is no config uploadtmp, system temprary directory will used.
39
40     __PACKAGE__->config( uploadtmp => '/path/to/tmpdir' );
41
42 See also L<Catalyst>.
43
44 =head1 DESCRIPTION
45
46 This class provides accessors and methods to handle client upload requests.
47
48 =head1 METHODS
49
50 =head2 $upload->new
51
52 Simple constructor.
53
54 =head2 $upload->copy_to
55
56 Copies the temporary file using L<File::Copy>. Returns true for success,
57 false for failure.
58
59      $upload->copy_to('/path/to/target');
60
61 =cut
62
63 sub copy_to {
64     my $self = shift;
65     return File::Copy::copy( $self->tempname, @_ );
66 }
67
68 =head2 $upload->fh
69
70 Opens a temporary file (see tempname below) and returns an L<IO::File> handle.
71
72 =cut
73
74 sub fh {
75     my $self = shift;
76
77     my $fh = IO::File->new( $self->tempname, IO::File::O_RDONLY );
78
79     unless ( defined $fh ) {
80
81         my $filename = $self->tempname;
82
83         Catalyst::Exception->throw(
84             message => qq/Can't open '$filename': '$!'/ );
85     }
86
87     return $fh;
88 }
89
90 =head2 $upload->filename
91
92 Returns the client-supplied filename.
93
94 =head2 $upload->headers
95
96 Returns an L<HTTP::Headers> object for the request.
97
98 =head2 $upload->link_to
99
100 Creates a hard link to the temporary file. Returns true for success, 
101 false for failure.
102
103     $upload->link_to('/path/to/target');
104
105 =cut
106
107 sub link_to {
108     my ( $self, $target ) = @_;
109     return CORE::link( $self->tempname, $target );
110 }
111
112 =head2 $upload->size
113
114 Returns the size of the uploaded file in bytes.
115
116 =head2 $upload->slurp
117
118 Returns a scalar containing the contents of the temporary file.
119
120 =cut
121
122 sub slurp {
123     my ( $self, $layer ) = @_;
124
125     unless ($layer) {
126         $layer = ':raw';
127     }
128
129     my $content = undef;
130     my $handle  = $self->fh;
131
132     binmode( $handle, $layer );
133
134     while ( $handle->sysread( my $buffer, 8192 ) ) {
135         $content .= $buffer;
136     }
137
138     return $content;
139 }
140
141 sub basename {
142     my $self = shift;
143     unless ( $self->{basename} ) {
144         my $basename = $self->filename;
145         $basename =~ s|\\|/|g;
146         $basename = ( File::Spec::Unix->splitpath($basename) )[2];
147         $basename =~ s|[^\w\.-]+|_|g;
148         $self->{basename} = $basename;
149     }
150
151     return $self->{basename};
152 }
153
154 =head2 $upload->basename
155
156 Returns basename for C<filename>.
157
158 =head2 $upload->tempname
159
160 Returns the path to the temporary file.
161
162 =head2 $upload->type
163
164 Returns the client-supplied Content-Type.
165
166 =head1 AUTHORS
167
168 Sebastian Riedel, C<sri@cpan.org>
169
170 Christian Hansen, C<ch@ngmedia.com>
171
172 =head1 COPYRIGHT
173
174 This program is free software, you can redistribute it and/or modify
175 it under the same terms as Perl itself.
176
177 =cut
178
179 1;