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