EmbedMP3.pm untested

filter: Statementstmt 0.0% 0 / 69 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 106 CC 5 SCAR 23.7
CC
5
Cov
0%
handler
30.0
BEGIN
2.0
BEGIN
2.0
CRAP 10.8
1 =head1 NAME
2  
3 Apache2::EmbedMP3 - Embed MP3 audio files into a templated web interface
4 using WP Audio Player.
5  
6 =head1 SYNOPSIS
7  
8 On your Apache configuration file:
9  
10 <Files ~ "\.mp3$">
11 SetHandler modperl
12 PerlSetVar wpaudioplayer /audio-player/player.swf
13 PerlSetVar wpaudioplayer_js /audio-player/wpaudioplayer.js
14 PerlResponseHandler Apache2::EmbedMP3
15 </Files>
16  
17 If you want to restrict only a certain directory to serve MP3s using
18 C<Apache2::EmbedMP3>, you can wrap the C<Files> declaration on a
19 C<Directory> block. For more information, take a look at excellent
20 Apache2's documentation.
21  
22 By default, you have to have C</wpaudioplayer.swf> and C</wpaudioplayer.js>
23 accessible on your webserver. You can download WP Audio Player from
24 L<http://wpaudioplayer.com/>, you only need the main JS file and the SWF.
25 WP Audio Player is a nice GPL product.
26  
27 However, as specified on the SYNOPSIS, you can override these default behavior
28 and C<PerlSetVar> C<wpaudioplayer> and C<wpaudioplayer_js> to point
29 wherever you'd have them.
30  
31 That's it. Just go to any MP3 file within your web server. With that
32 setup, C<Apache2::EmbedMP3> will use a default template.
33  
34 =head1 TEMPLATING
35  
36 Take a look at the default template located at example/template.tt.
37 That is not the real file used by this module but it's a verbatim copy.
38 The file is placed there just as an example so you can make
39 your own template without too much internal poking.
40  
41 Once you have your own template, just C<PerlSetVar> it to the handler:
42  
43 <Files ~ "\.mp3$">
44 SetHandler modperl
45 PerlSetVar template /path/to/my/template.tt
46 PerlResponseHandler Apache2::EmbedMP3
47 </Files>
48  
49 A more complete example:
50  
51 <Files ~ "\.mp3$">
52 SetHandler modperl
53 PerlSetVar template /path/to/my/template.tt
54 PerlSetVar wpaudioplayer /somewhere/wpaudioplayer.swf
55 # or...
56 PerlSetVar wpaudioplayer_js http://my.other.server/rocks/wpaudioplayer.js
57 PerlResponseHandler Apache2::EmbedMP3
58 </Files>
59  
60 I believe it's pretty obvious that the templating system used and
61 required is L<Template::Toolkit>. Wherever you want to embed the player
62 within, just call: C<[% player %]>.
63  
64 =head1 DESCRIPTION
65  
66 C<Apache2::EmbledMP3> has been already described on the previous section
67 :-)
68  
69 However...
70  
71 C<Apache2::EmbedMP3> enables Apache to show MP3 files using WP Audio Player.
72 This will ease any deployment of MP3 galleries you'd need to do
73 since you could just put the MP3s on an Apache accessible location, and
74 they will be presented on a proper way to your final user.
75  
76 Additionally, the following information for the files is available:
77  
78 =over
79  
80 =item * artist
81  
82 =item * title
83  
84 =item * album name
85  
86 =item * album year
87  
88 =item * lyrics!
89  
90 =back
91  
92 These are presented by the default template and you can use them too
93 on your own templates. This information is possible by using L<Music::Tag::MP3>.
94  
95 =head1 DEPENDENCIES
96  
97 =over
98  
99 =item * L<Music::Tag::MP3>
100  
101 =item * L<Music::Tag::Lyrics>
102  
103 =back
104  
105 =head1 SEE IT IN ACTION
106  
107 You can see it in action here: L<http://dev.axiombox.com/~david/mp3/>.
108  
109 =head1 WORDPRESS AUDIO PLAYER
110  
111 Find the WP Audio Player distribution on L<http://wpaudioplayer.com/>. It is a nice
112 little GPL audio player.
113  
114 =head1 DOWNLOAD
115  
116 Download it at CPAN: L<http://search.cpan.org/~damog>.
117  
118 =head1 PROJECT
119  
120 Code is hosted at L<http://github.com/damog/apache2-embedmp3>.
121  
122 =head1 AUTHOR
123  
124 David Moreno <david@axiombox.com>, L<http://damog.net/>.
125 Some other similar projects are announced on the Infinite Pig
126 Theorem blog: L<http://log.damog.net>.
127  
128 =head1 SEE ALSO
129  
130 L<Apache2::EmbedFLV>.
131  
132 =head1 BUGS
133  
134 Apparently, there's a bug if the filename of your MP3 contains spaces.
135 This will be tracked later.
136  
137 =head1 THANKS
138  
139 =over
140  
141 =item * Raquel Hernandez, L<http://maggit.net>, who made the default template.
142  
143 =back
144  
145 =head1 COPYRIGHT
146  
147 Copyright (C) 2009 by David Moreno.
148  
149 This library is free software; you can redistribute it and/or modify
150 it under the same terms as Perl itself.
151  
152 =cut
153  
154 package Apache2::EmbedMP3;
155  
156 our $VERSION = '0.1';
157  
158 use Apache2::Request;
159 use Apache2::RequestRec ();
160 use Apache2::RequestIO ();
161 use Apache2::RequestUtil ();
162 use Apache2::Util ();
163 use Apache2::Const -compile => qw/OK :common DIR_MAGIC_TYPE/;
164 use Apache2::EmbedMP3::Template;
165 use Data::Dumper;
166 use Cwd;
167 use Music::Tag;
168 use Digest::MD5 qw/md5_hex/;
169  
170 sub handler {
171 my($r) = shift;
172  
173 if(! -e $r->filename) {
174 return Apache2::Const::NOT_FOUND;
175 } elsif(! -r $r->filename) {
176 return Apache2::Const::FORBIDDEN;
177 } else {
178 my $req = Apache2::Request->new($r);
179  
180 if(
181 # let's face it, this is why Perl sucks
182 $req->param and
183 scalar keys %{$req->param} == 1 and
184 not $req->param( ${ [ keys %{$param} ] }[0] ) and
185 length ${ [ keys %{$req->param} ] }[0] == 32
186 ) {
187 my($md5) = keys %{$req->param}; # not too intuitive
188 if($md5 eq md5_hex($r->filename)) {
189 $r->content_type("audio/mpeg");
190 $r->headers_out->set("Content-Length" => -s $r->filename);
191 open my $fh, "<", $r->filename or die "Apache2::EmbedMP3 wrong $!!";
192 while(<$fh>) {
193 $r->print($_);
194 }
195 close $fh;
196 return Apache2::Const::OK;
197 } else {
198 return Apache2::Const::FORBIDDEN;
199 }
200 } else {
201 my $md5 = md5_hex($r->filename);
202 $r->content_type("text/html; charset=utf-8");
203  
204 my $template = $r->dir_config('template');
205 my $wpaudioplayer = $r->dir_config("wpaudioplayer");
206  
207 my $t = Apache2::EmbedMP3::Template->new($template);
208 my $info = Music::Tag->new($r->filename);
209 $info->add_plugin("Lyrics");
210 $info->get_tag();
211  
212 $r->print(
213 $t->process(
214 uri => $r->uri,
215 md5 => $md5,
216 wpaudioplayer => $r->dir_config("wpaudioplayer"),
217 js => $r->dir_config("wpaudioplayer_js"),
218 template => $template,
219 lyrics => $info->lyrics,
220 title => $info->title,
221 artist => $info->artist,
222 year => $info->year,
223 album => $info->album,
224 )
225 );
226  
227 return Apache2::Const::OK;
228 }
229 }
230 }
231  
232 1;
233  
234 __END__