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