updated MANIFIEST
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Request / Upload.pm
CommitLineData
146554c5 1package Catalyst::Request::Upload;
2
3use strict;
4use base 'Class::Accessor::Fast';
5
47ae6960 6use File::Copy ();
7use IO::File ();
146554c5 8
9__PACKAGE__->mk_accessors(qw/filename size tempname type/);
10
b549b049 11sub new { shift->SUPER::new( ref( $_[0] ) ? $_[0] : {@_} ) }
146554c5 12
13=head1 NAME
14
15Catalyst::Request::Upload - Catalyst Request Upload Class
16
17=head1 SYNOPSIS
18
3ffaf022 19 $upload->copy_to
146554c5 20 $upload->fh
21 $upload->filename;
3ffaf022 22 $upload->link_to;
146554c5 23 $upload->size;
32d4bba8 24 $upload->slurp;
146554c5 25 $upload->tempname;
26 $upload->type;
27
28See also L<Catalyst>.
29
30=head1 DESCRIPTION
31
47ae6960 32This is the Catalyst Request Upload class, which provides a set of accessors
33to the upload data.
146554c5 34
35=head1 METHODS
36
37=over 4
38
3ffaf022 39=item $upload->copy_to
47ae6960 40
41Copies tempname using C<File::Copy>. Returns true for success, false otherwise.
42
3ffaf022 43 $upload->copy_to('/path/to/target');
44
47ae6960 45=cut
46
3ffaf022 47sub copy_to {
c462faf0 48 my $self = shift;
49 return File::Copy::copy( $self->tempname, @_ );
47ae6960 50}
51
146554c5 52=item $upload->fh
53
54Opens tempname and returns a C<IO::File> handle.
55
56=cut
57
58sub fh {
59 my $self = shift;
60
47ae6960 61 my $fh = IO::File->new( $self->tempname, IO::File::O_RDONLY )
146554c5 62 or die( "Can't open ", $self->tempname, ": ", $! );
63
64 return $fh;
65}
66
67=item $upload->filename
68
69Contains client supplied filename.
70
3ffaf022 71=item $upload->link_to
146554c5 72
3ffaf022 73Creates a hard link to the tempname. Returns true for success,
74false otherwise.
146554c5 75
3ffaf022 76 $upload->link_to('/path/to/target');
146554c5 77
78=cut
79
3ffaf022 80sub link_to {
47ae6960 81 my ( $self, $target ) = @_;
5c0ff128 82 return CORE::link( $self->tempname, $target );
146554c5 83}
84
85=item $upload->size
86
87Contains size of the file in bytes.
88
32d4bba8 89=item $upload->slurp
90
91Returns a scalar containing contents of tempname.
92
93=cut
94
95sub slurp {
96 my ( $self, $layer ) = @_;
97
98 unless ( $layer ) {
99 $layer = ':raw';
100 }
101
102 my $content = undef;
103 my $handle = $self->fh;
104
105 binmode( $handle, $layer );
106
107 while ( $handle->sysread( my $buffer, 8192 ) ) {
108 $content .= $buffer;
109 }
110
111 return $content;
112}
113
146554c5 114=item $upload->tempname
115
116Contains path to the temporary spool file.
117
118=item $upload->type
119
120Contains client supplied Content-Type.
121
122=back
123
124=head1 AUTHOR
125
126Sebastian Riedel, C<sri@cpan.org>
127Christian Hansen, C<ch@ngmedia.com>
128
129=head1 COPYRIGHT
130
131This program is free software, you can redistribute it and/or modify
132it under the same terms as Perl itself.
133
134=cut
135
1361;