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