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