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 | |
3ffaf022 |
39 | =item $upload->copy_to |
47ae6960 |
40 | |
41 | Copies tempname using C<File::Copy>. Returns true for success, false otherwise. |
42 | |
3ffaf022 |
43 | $upload->copy_to('/path/to/target'); |
44 | |
47ae6960 |
45 | =cut |
46 | |
3ffaf022 |
47 | sub copy_to { |
c462faf0 |
48 | my $self = shift; |
49 | return File::Copy::copy( $self->tempname, @_ ); |
47ae6960 |
50 | } |
51 | |
146554c5 |
52 | =item $upload->fh |
53 | |
54 | Opens tempname and returns a C<IO::File> handle. |
55 | |
56 | =cut |
57 | |
58 | sub fh { |
59 | my $self = shift; |
60 | |
47ae6960 |
61 | my $fh = IO::File->new( $self->tempname, IO::File::O_RDONLY ) |
146554c5 |
62 | or die( "Can't open ", $self->tempname, ": ", $! ); |
63 | |
64 | return $fh; |
65 | } |
66 | |
67 | =item $upload->filename |
68 | |
69 | Contains client supplied filename. |
70 | |
3ffaf022 |
71 | =item $upload->link_to |
146554c5 |
72 | |
3ffaf022 |
73 | Creates a hard link to the tempname. Returns true for success, |
74 | false otherwise. |
146554c5 |
75 | |
3ffaf022 |
76 | $upload->link_to('/path/to/target'); |
146554c5 |
77 | |
78 | =cut |
79 | |
3ffaf022 |
80 | sub link_to { |
47ae6960 |
81 | my ( $self, $target ) = @_; |
5c0ff128 |
82 | return CORE::link( $self->tempname, $target ); |
146554c5 |
83 | } |
84 | |
85 | =item $upload->size |
86 | |
87 | Contains size of the file in bytes. |
88 | |
32d4bba8 |
89 | =item $upload->slurp |
90 | |
91 | Returns a scalar containing contents of tempname. |
92 | |
93 | =cut |
94 | |
95 | sub slurp { |
96 | my ( $self, $layer ) = @_; |
97 | |
98 | unless ( $layer ) { |
99 | $layer = ':raw'; |
100 | } |
101 | |
102 | my $content = undef; |
103 | my $handle = $self->fh; |
104 | |
105 | binmode( $handle, $layer ); |
106 | |
107 | while ( $handle->sysread( my $buffer, 8192 ) ) { |
108 | $content .= $buffer; |
109 | } |
110 | |
111 | return $content; |
112 | } |
113 | |
146554c5 |
114 | =item $upload->tempname |
115 | |
116 | Contains path to the temporary spool file. |
117 | |
118 | =item $upload->type |
119 | |
120 | Contains client supplied Content-Type. |
121 | |
122 | =back |
123 | |
124 | =head1 AUTHOR |
125 | |
126 | Sebastian Riedel, C<sri@cpan.org> |
127 | Christian Hansen, C<ch@ngmedia.com> |
128 | |
129 | =head1 COPYRIGHT |
130 | |
131 | This program is free software, you can redistribute it and/or modify |
132 | it under the same terms as Perl itself. |
133 | |
134 | =cut |
135 | |
136 | 1; |