Apache2-EmbedMP3

 view release on metacpan or  search on metacpan

lib/Apache2/EmbedMP3.pm  view on Meta::CPAN


=back

=head1 COPYRIGHT

Copyright (C) 2009 by David Moreno.

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

package Apache2::EmbedMP3;

our $VERSION = '0.1';

use Apache2::Request;
use Apache2::RequestRec ();
use Apache2::RequestIO ();
use Apache2::RequestUtil ();
use Apache2::Util ();
use Apache2::Const -compile => qw/OK :common DIR_MAGIC_TYPE/;
use Apache2::EmbedMP3::Template;
use Data::Dumper;
use Cwd;
use Music::Tag;
use Digest::MD5 qw/md5_hex/;

sub handler {
	my($r) = shift;

	if(! -e $r->filename) {
		return Apache2::Const::NOT_FOUND;
	} elsif(! -r $r->filename) {
		return Apache2::Const::FORBIDDEN;
	} else {
		my $req = Apache2::Request->new($r);

		if(
				# let's face it, this is why Perl sucks
				$req->param and
				scalar keys %{$req->param} == 1 and
				not $req->param( ${ [ keys %{$param} ] }[0] ) and
				length ${ [ keys %{$req->param} ] }[0] == 32
		) {
			my($md5) = keys %{$req->param}; # not too intuitive
			if($md5 eq md5_hex($r->filename)) {
				$r->content_type("audio/mpeg");
				$r->headers_out->set("Content-Length" => -s $r->filename);
				open my $fh, "<", $r->filename or die "Apache2::EmbedMP3 wrong $!!";
				while(<$fh>) {
					$r->print($_);
				}
				close $fh;
				return Apache2::Const::OK;
			} else {
				return Apache2::Const::FORBIDDEN;
			}
		} else {
			my $md5 = md5_hex($r->filename);
			$r->content_type("text/html; charset=utf-8");

			my $template = $r->dir_config('template');
			my $wpaudioplayer = $r->dir_config("wpaudioplayer");

			my $t = Apache2::EmbedMP3::Template->new($template);
			my $info = Music::Tag->new($r->filename);
			$info->add_plugin("Lyrics");
			$info->get_tag();

			$r->print(
				$t->process(
					uri => $r->uri,
					md5 => $md5,
					wpaudioplayer => $r->dir_config("wpaudioplayer"),
					js => $r->dir_config("wpaudioplayer_js"),
					template => $template,
					lyrics => $info->lyrics,
					title => $info->title,
					artist => $info->artist,
					year => $info->year,
					album => $info->album,
				)
			);

			return Apache2::Const::OK;
		}
	}
}

1;

__END__
Hello! Your md5 cookie is [% md5 %]



( run in 2.094 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )