Missed some changes, merged again from r1019
[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
16Catalyst::Request::Upload - Catalyst Request Upload Class
17
18=head1 SYNOPSIS
19
3ffaf022 20 $upload->copy_to
146554c5 21 $upload->fh
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
30See also L<Catalyst>.
31
32=head1 DESCRIPTION
33
47ae6960 34This is the Catalyst Request Upload class, which provides a set of accessors
35to the upload data.
146554c5 36
37=head1 METHODS
38
39=over 4
40
cd3bb248 41=item $upload->new
42
43simple constructor.
44
3ffaf022 45=item $upload->copy_to
47ae6960 46
47Copies tempname using C<File::Copy>. Returns true for success, false otherwise.
48
3ffaf022 49 $upload->copy_to('/path/to/target');
50
47ae6960 51=cut
52
3ffaf022 53sub copy_to {
c462faf0 54 my $self = shift;
55 return File::Copy::copy( $self->tempname, @_ );
47ae6960 56}
57
146554c5 58=item $upload->fh
59
60Opens tempname and returns a C<IO::File> handle.
61
62=cut
63
64sub fh {
65 my $self = shift;
66
a2f2cde9 67 my $fh = IO::File->new( $self->tempname, IO::File::O_RDONLY );
4be535b1 68
a2f2cde9 69 unless ( defined $fh ) {
4be535b1 70
a2f2cde9 71 my $filename = $self->tempname;
4be535b1 72
a2f2cde9 73 Catalyst::Exception->throw(
4be535b1 74 message => qq/Can't open '$filename': '$!'/ );
a2f2cde9 75 }
146554c5 76
77 return $fh;
78}
79
80=item $upload->filename
81
82Contains client supplied filename.
83
4be535b1 84=item $upload->headers
85
86Returns a C<HTTP::Headers> object.
87
3ffaf022 88=item $upload->link_to
146554c5 89
3ffaf022 90Creates a hard link to the tempname. Returns true for success,
91false otherwise.
146554c5 92
3ffaf022 93 $upload->link_to('/path/to/target');
146554c5 94
95=cut
96
3ffaf022 97sub link_to {
47ae6960 98 my ( $self, $target ) = @_;
5c0ff128 99 return CORE::link( $self->tempname, $target );
146554c5 100}
101
102=item $upload->size
103
104Contains size of the file in bytes.
105
32d4bba8 106=item $upload->slurp
107
108Returns a scalar containing contents of tempname.
109
110=cut
111
112sub slurp {
113 my ( $self, $layer ) = @_;
114
4be535b1 115 unless ($layer) {
32d4bba8 116 $layer = ':raw';
117 }
118
119 my $content = undef;
120 my $handle = $self->fh;
121
122 binmode( $handle, $layer );
123
124 while ( $handle->sysread( my $buffer, 8192 ) ) {
125 $content .= $buffer;
126 }
127
128 return $content;
129}
130
146554c5 131=item $upload->tempname
132
133Contains path to the temporary spool file.
134
135=item $upload->type
136
137Contains client supplied Content-Type.
138
139=back
140
141=head1 AUTHOR
142
143Sebastian Riedel, C<sri@cpan.org>
144Christian Hansen, C<ch@ngmedia.com>
145
146=head1 COPYRIGHT
147
148This program is free software, you can redistribute it and/or modify
149it under the same terms as Perl itself.
150
151=cut
152
1531;