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