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