minor typos in new Mason.pm
[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_to
20     $upload->fh
21     $upload->filename;
22     $upload->link_to;
23     $upload->size;
24     $upload->slurp;
25     $upload->tempname;
26     $upload->type;
27
28 See also L<Catalyst>.
29
30 =head1 DESCRIPTION
31
32 This is the Catalyst Request Upload class, which provides a set of accessors 
33 to the upload data.
34
35 =head1 METHODS
36
37 =over 4
38
39 =item $upload->copy_to
40
41 Copies tempname using C<File::Copy>. Returns true for success, false otherwise.
42
43      $upload->copy_to('/path/to/target');
44
45 =cut
46
47 sub copy_to {
48     my $self = shift;
49     return File::Copy::copy( $self->tempname, @_ );
50 }
51
52 =item $upload->fh
53
54 Opens tempname and returns a C<IO::File> handle.
55
56 =cut
57
58 sub fh {
59     my $self = shift;
60
61     my $fh = IO::File->new( $self->tempname, IO::File::O_RDONLY )
62       or die( "Can't open ", $self->tempname, ": ", $! );
63
64     return $fh;
65 }
66
67 =item $upload->filename
68
69 Contains client supplied filename.
70
71 =item $upload->link_to
72
73 Creates a hard link to the tempname.  Returns true for success, 
74 false otherwise.
75
76     $upload->link_to('/path/to/target');
77
78 =cut
79
80 sub link_to {
81     my ( $self, $target ) = @_;
82     return CORE::link( $self->tempname, $target );
83 }
84
85 =item $upload->size
86
87 Contains size of the file in bytes.
88
89 =item $upload->slurp
90
91 Returns a scalar containing contents of tempname.
92
93 =cut
94
95 sub 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
114 =item $upload->tempname
115
116 Contains path to the temporary spool file.
117
118 =item $upload->type
119
120 Contains client supplied Content-Type.
121
122 =back
123
124 =head1 AUTHOR
125
126 Sebastian Riedel, C<sri@cpan.org>
127 Christian Hansen, C<ch@ngmedia.com>
128
129 =head1 COPYRIGHT
130
131 This program is free software, you can redistribute it and/or modify
132 it under the same terms as Perl itself.
133
134 =cut
135
136 1;