added a copy method to C::R::Upload and updated Cookbook
[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
11sub new { shift->SUPER::new( ref( $_[0] ) ? $_[0] : {@_} ) }
12
13=head1 NAME
14
15Catalyst::Request::Upload - Catalyst Request Upload Class
16
17=head1 SYNOPSIS
18
47ae6960 19 $upload->copy
146554c5 20 $upload->fh
21 $upload->filename;
22 $upload->link;
23 $upload->size;
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
47ae6960 38=item $upload->copy( $target [, $bufferlen ] )
39
40Copies tempname using C<File::Copy>. Returns true for success, false otherwise.
41
42=cut
43
44sub copy {
45 my ( $self, $target, $buffer ) = @_;
46 return File::Copy::copy( $self->tempname, $target, $buffer );
47}
48
146554c5 49=item $upload->fh
50
51Opens tempname and returns a C<IO::File> handle.
52
53=cut
54
55sub fh {
56 my $self = shift;
57
47ae6960 58 my $fh = IO::File->new( $self->tempname, IO::File::O_RDONLY )
146554c5 59 or die( "Can't open ", $self->tempname, ": ", $! );
60
61 return $fh;
62}
63
64=item $upload->filename
65
66Contains client supplied filename.
67
68=item $upload->link
69
70Creates a new filename linked to the old filename. Returns true for
71success, false otherwise.
72
73 $upload->link('/my/path');
74
75=cut
76
77sub link {
47ae6960 78 my ( $self, $target ) = @_;
5c0ff128 79 return CORE::link( $self->tempname, $target );
146554c5 80}
81
82=item $upload->size
83
84Contains size of the file in bytes.
85
86=item $upload->tempname
87
88Contains path to the temporary spool file.
89
90=item $upload->type
91
92Contains client supplied Content-Type.
93
94=back
95
96=head1 AUTHOR
97
98Sebastian Riedel, C<sri@cpan.org>
99Christian Hansen, C<ch@ngmedia.com>
100
101=head1 COPYRIGHT
102
103This program is free software, you can redistribute it and/or modify
104it under the same terms as Perl itself.
105
106=cut
107
1081;