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