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