lala
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Request / Upload.pm
CommitLineData
146554c5 1package Catalyst::Request::Upload;
2
3use strict;
4use base 'Class::Accessor::Fast';
5
a2f2cde9 6use Catalyst::Exception;
47ae6960 7use File::Copy ();
8use IO::File ();
3e22baa5 9use File::Spec::Unix;
146554c5 10
3e22baa5 11__PACKAGE__->mk_accessors(qw/filename headers size tempname type basename/);
146554c5 12
b549b049 13sub new { shift->SUPER::new( ref( $_[0] ) ? $_[0] : {@_} ) }
146554c5 14
15=head1 NAME
16
bab3a22c 17Catalyst::Request::Upload - handles file upload requests
146554c5 18
19=head1 SYNOPSIS
20
3e22baa5 21 $upload->basename;
bab3a22c 22 $upload->copy_to;
23 $upload->fh;
146554c5 24 $upload->filename;
4be535b1 25 $upload->headers;
3ffaf022 26 $upload->link_to;
146554c5 27 $upload->size;
32d4bba8 28 $upload->slurp;
146554c5 29 $upload->tempname;
30 $upload->type;
31
7257e9db 32To specify where Catalyst should put the temporary files, set the 'uploadtmp'
33option in the Catalyst config. If unset, Catalyst will use the system temp dir.
34
35 __PACKAGE__->config( uploadtmp => '/path/to/tmpdir' );
36
7c48f3f6 37It is provided a way to have configurable temporary directory.
38If there is no config uploadtmp, system temprary directory will used.
39
40 __PACKAGE__->config( uploadtmp => '/path/to/tmpdir' );
41
146554c5 42See also L<Catalyst>.
43
44=head1 DESCRIPTION
45
bab3a22c 46This class provides accessors and methods to handle client upload requests.
146554c5 47
48=head1 METHODS
49
b5ecfcf0 50=head2 $upload->new
cd3bb248 51
bab3a22c 52Simple constructor.
cd3bb248 53
b5ecfcf0 54=head2 $upload->copy_to
47ae6960 55
bab3a22c 56Copies the temporary file using L<File::Copy>. Returns true for success,
57false for failure.
47ae6960 58
3ffaf022 59 $upload->copy_to('/path/to/target');
60
47ae6960 61=cut
62
3ffaf022 63sub copy_to {
c462faf0 64 my $self = shift;
65 return File::Copy::copy( $self->tempname, @_ );
47ae6960 66}
67
b5ecfcf0 68=head2 $upload->fh
146554c5 69
bab3a22c 70Opens a temporary file (see tempname below) and returns an L<IO::File> handle.
146554c5 71
72=cut
73
74sub fh {
75 my $self = shift;
76
a2f2cde9 77 my $fh = IO::File->new( $self->tempname, IO::File::O_RDONLY );
4be535b1 78
a2f2cde9 79 unless ( defined $fh ) {
4be535b1 80
a2f2cde9 81 my $filename = $self->tempname;
4be535b1 82
a2f2cde9 83 Catalyst::Exception->throw(
4be535b1 84 message => qq/Can't open '$filename': '$!'/ );
a2f2cde9 85 }
146554c5 86
87 return $fh;
88}
89
b5ecfcf0 90=head2 $upload->filename
146554c5 91
bab3a22c 92Returns the client-supplied filename.
146554c5 93
b5ecfcf0 94=head2 $upload->headers
4be535b1 95
bab3a22c 96Returns an L<HTTP::Headers> object for the request.
4be535b1 97
b5ecfcf0 98=head2 $upload->link_to
146554c5 99
bab3a22c 100Creates a hard link to the temporary file. Returns true for success,
101false for failure.
146554c5 102
3ffaf022 103 $upload->link_to('/path/to/target');
146554c5 104
105=cut
106
3ffaf022 107sub link_to {
47ae6960 108 my ( $self, $target ) = @_;
5c0ff128 109 return CORE::link( $self->tempname, $target );
146554c5 110}
111
b5ecfcf0 112=head2 $upload->size
146554c5 113
bab3a22c 114Returns the size of the uploaded file in bytes.
146554c5 115
b5ecfcf0 116=head2 $upload->slurp
32d4bba8 117
bab3a22c 118Returns a scalar containing the contents of the temporary file.
32d4bba8 119
120=cut
121
122sub slurp {
123 my ( $self, $layer ) = @_;
124
4be535b1 125 unless ($layer) {
32d4bba8 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
3e22baa5 141sub 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
156Returns basename for C<filename>.
157
b5ecfcf0 158=head2 $upload->tempname
146554c5 159
bab3a22c 160Returns the path to the temporary file.
146554c5 161
b5ecfcf0 162=head2 $upload->type
146554c5 163
bab3a22c 164Returns the client-supplied Content-Type.
146554c5 165
bab3a22c 166=head1 AUTHORS
146554c5 167
168Sebastian Riedel, C<sri@cpan.org>
bab3a22c 169
146554c5 170Christian Hansen, C<ch@ngmedia.com>
171
172=head1 COPYRIGHT
173
174This program is free software, you can redistribute it and/or modify
175it under the same terms as Perl itself.
176
177=cut
178
1791;