File Coverage

blib/lib/Catalyst/Plugin/Upload/Audio/File.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1             package Catalyst::Plugin::Upload::Audio::File;
2              
3 1     1   904 use strict;
  1         2  
  1         36  
4 1     1   1529 use Catalyst::Request::Upload;
  0            
  0            
5              
6             our $VERSION = '0.01';
7              
8             {
9             package Catalyst::Request::Upload;
10             use Audio::File;
11              
12             # Get the actual Audio::File object
13             sub audio_file { return shift->_load_audio_file; }
14              
15             # Get values in the AudioProperties of the file
16             sub length { return shift->_get_property('length'); }
17             sub bitrate { return shift->_get_property('bitrate'); }
18             sub sample_rate { return shift->_get_property('sample_rate'); }
19             sub channels { return shift->_get_property('channels'); }
20              
21             # Get values in the Tags of the file
22             sub title { return shift->_get_tag('title'); }
23             sub artist { return shift->_get_tag('artist'); }
24             sub album { return shift->_get_tag('album'); }
25             sub comment { return shift->_get_tag('comment'); }
26             sub genre { return shift->_get_tag('genre'); }
27             sub year { return shift->_get_tag('year'); }
28             sub track { return shift->_get_tag('track'); }
29             sub total { return shift->_get_tag('total'); }
30              
31             sub _get_property {
32             my ($self, $property) = (shift, @_);
33              
34             $self->_load_audio_file
35             ? return $self->{__audio_file}->audio_properties->all->{$property}
36             : undef;
37             }
38              
39             sub _get_tag {
40             my ($self, $tag) = (shift, @_);
41              
42             $self->_load_audio_file
43             ? return $self->{__audio_file}->tag->all->{$tag}
44             : undef;
45             }
46              
47             sub _load_audio_file {
48             my $self = shift;
49             eval {
50             $self->{__audio_file} ||= Audio::File->new($self->filename);
51             };
52             return $self->{__audio_file};
53             }
54             }
55              
56             1;
57              
58             __END__
59              
60             =head1 NAME
61              
62             Catalyst::Plugin::Upload::Audio::File - Get an Audio::File from an upload
63              
64             =head1 SYNOPSIS
65              
66             use Catalyst qw/Upload::Audio::File/;
67              
68             if ( my $upload = $c->request->upload('file_to_upload') ) {
69             # The destination file must have the file extension intact
70             my $temp_file = "/tmp".$upload->filename;
71              
72             # The new file location must be reflected into the $upload object
73             $upload->copy_to($temp_file);
74             $upload->filename($temp_file);
75              
76             print "Got a file of length ".$upload->length."\n";
77             print "The bitrate is ".$upload->bitrate."\n";
78             }
79              
80              
81             =head1 DESCRIPTION
82              
83             Extends C<Catalyst::Request::Upload> with C<Audio::File>.
84              
85             =head1 WARNING
86              
87             Because `Catalyst::Request::Upload->filename` contains the name of
88             the file as the user uploaded it and -not- the name of a real file on disk,
89             and because `$upload->tempname` is a random string with no file extension, you
90             must copy the file with the file extension intact and reflect the new file
91             location back into the C<$upload> object as demonstrated in the Synopsis.
92              
93             =head1 METHODS
94              
95             See L<Audio::File> for more detailed descriptions of available methods.
96              
97             All methods except C<audio_file> return the scalar value of the file property
98             or C<undef>.
99              
100             =over 4
101              
102             =item audio_file
103              
104             The Audio::File object itself.
105              
106             =item length
107              
108             The length of the file.
109            
110             =item bitrate
111              
112             The bitrate of the file.
113              
114             =item sample_rate
115              
116             The sample rate of the file.
117              
118             =item channels
119              
120             The number of audio channels in the file.
121              
122             =item title
123              
124             The title from the file metadata ("tags").
125              
126             =item artist
127              
128             The artist name from the file metadata.
129              
130             =item album
131              
132             The album name from the file metadata.
133              
134             =item comment
135              
136             The comment from the file metadata.
137              
138             =item genre
139              
140             The genre from the file metadata.
141              
142             =item year
143              
144             The year from the file metadata.
145              
146             =item track
147              
148             The track number from the file metadata.
149              
150             =item total
151              
152             The total tracks from the file metadata.
153              
154             =back
155              
156             =head1 SEE ALSO
157              
158             L<Audio::File>
159              
160             =head1 AUTHOR
161              
162             Nathaniel Heinrichs, C<nheinric@cpan.org>
163              
164             =head1 LICENSE
165              
166             Copyright (c) 2009 Nathaniel Heinrichs
167              
168             Written while employed at Orinoco K.K., L<http://www.orinoco.jp>
169            
170             This library is free software.
171             You can redistribute it and/or modify it under the same terms as perl itself.
172              
173             =cut