Make everything which used to inherit CAF use the MX::Emulate::CAF role + test
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Request / Upload.pm
1 package Catalyst::Request::Upload;
2
3 use Moose;
4 with 'MooseX::Emulate::Class::Accessor::Fast';
5
6 use Catalyst::Exception;
7 use File::Copy ();
8 use IO::File   ();
9 use File::Spec::Unix;
10
11 has filename => (is => 'rw');
12 has headers => (is => 'rw');
13 has size => (is => 'rw');
14 has tempname => (is => 'rw');
15 has type => (is => 'rw');
16 has basename => (is => 'rw');
17
18 has fh => (
19   is => 'rw',
20   required => 1,
21   lazy => 1,
22   default => sub {
23     my $self = shift;
24
25     my $fh = IO::File->new($self->tempname, IO::File::O_RDONLY);
26     unless ( defined $fh ) {
27       my $filename = $self->tempname;
28       Catalyst::Exception->throw(
29           message => qq/Can't open '$filename': '$!'/ );
30     }
31
32     return $fh;
33   },
34 );
35
36 no Moose;
37
38 =head1 NAME
39
40 Catalyst::Request::Upload - handles file upload requests
41
42 =head1 SYNOPSIS
43
44     $upload->basename;
45     $upload->copy_to;
46     $upload->fh;
47     $upload->filename;
48     $upload->headers;
49     $upload->link_to;
50     $upload->size;
51     $upload->slurp;
52     $upload->tempname;
53     $upload->type;
54
55 To specify where Catalyst should put the temporary files, set the 'uploadtmp'
56 option in the Catalyst config. If unset, Catalyst will use the system temp dir.
57
58     __PACKAGE__->config( uploadtmp => '/path/to/tmpdir' );
59
60 See also L<Catalyst>.
61
62 =head1 DESCRIPTION
63
64 This class provides accessors and methods to handle client upload requests.
65
66 =head1 METHODS
67
68 =head2 $upload->new
69
70 Simple constructor.
71
72 =head2 $upload->copy_to
73
74 Copies the temporary file using L<File::Copy>. Returns true for success,
75 false for failure.
76
77      $upload->copy_to('/path/to/target');
78
79 =cut
80
81 sub copy_to {
82     my $self = shift;
83     return File::Copy::copy( $self->tempname, @_ );
84 }
85
86 =head2 $upload->fh
87
88 Opens a temporary file (see tempname below) and returns an L<IO::File> handle.
89
90 =head2 $upload->filename
91
92 Returns the client-supplied filename.
93
94 =head2 $upload->headers
95
96 Returns an L<HTTP::Headers> object for the request.
97
98 =head2 $upload->link_to
99
100 Creates a hard link to the temporary file. Returns true for success, 
101 false for failure.
102
103     $upload->link_to('/path/to/target');
104
105 =cut
106
107 sub link_to {
108     my ( $self, $target ) = @_;
109     return CORE::link( $self->tempname, $target );
110 }
111
112 =head2 $upload->size
113
114 Returns the size of the uploaded file in bytes.
115
116 =head2 $upload->slurp
117
118 Returns a scalar containing the contents of the temporary file.
119
120 =cut
121
122 sub slurp {
123     my ( $self, $layer ) = @_;
124
125     unless ($layer) {
126         $layer = ':raw';
127     }
128
129     my $content = undef;
130     my $handle  = $self->fh;
131
132     binmode( $handle, $layer );
133
134     while ( $handle->sysread( my $buffer, 8192 ) ) {
135         $content .= $buffer;
136     }
137
138     return $content;
139 }
140
141 sub basename {
142     my $self = shift;
143     unless ( $self->{basename} ) {
144         my $basename = $self->filename;
145         $basename =~ s|\\|/|g;
146         $basename = ( File::Spec::Unix->splitpath($basename) )[2];
147         $basename =~ s|[^\w\.-]+|_|g;
148         $self->{basename} = $basename;
149     }
150
151     return $self->{basename};
152 }
153
154 =head2 $upload->basename
155
156 Returns basename for C<filename>.
157
158 =head2 $upload->tempname
159
160 Returns the path to the temporary file.
161
162 =head2 $upload->type
163
164 Returns the client-supplied Content-Type.
165
166 =head2 meta
167
168 Provided by Moose
169
170 =head1 AUTHORS
171
172 Catalyst Contributors, see Catalyst.pm
173
174 =head1 COPYRIGHT
175
176 This program is free software, you can redistribute it and/or modify
177 it under the same terms as Perl itself.
178
179 =cut
180
181 __PACKAGE__->meta->make_immutable;
182
183 1;