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