Apache-MP3
view release on metacpan or search on metacpan
sub find_mp3s {
my $self = shift;
my $recurse = shift;
#changing this so that it is possible to find mp3s from search page
# my $uri = dirname($self->r->uri);
my $uri = dirname(shift || $self->r->uri);
my $dir = dirname($self->r->filename);
my @uris = $self->sort_mp3s($self->_find_mp3s($dir,$recurse));
foreach (@uris) {
# strip directory part
substr($_,0,length($dir)+1) = '' if index($_,$dir) == 0;
# turn into a URL
$_ = "$uri/$_";
}
return \@uris;
}
# recursive find
sub _find_mp3s {
my $self = shift;
my ($d,$recurse) = @_;
my ($directories,$files) = $self->read_directory($d);
# Add the directory back onto each file
unless ($d eq '.') {
foreach my $k (keys %$files) {
$files->{"$d/$k"} = $files->{$k};
delete $files->{$k};
}
}
if ($recurse) {
foreach (@$directories) {
my $f = $self->_find_mp3s("$d/$_",$recurse);
# Add the new files to our main hash
$files->{$_} = $f->{$_} foreach keys %$f;
}
}
return $files;
}
# sort MP3s
sub sort_mp3s {
my $self = shift;
my $files = shift;
return sort keys %$files;
}
#################################################
# interesting configuration directives start here
#################################################
#utility subroutine for configuration
sub get_dir {
my $self = shift;
my ($config,$default) = @_;
my $dir = $self->r->dir_config($config) || $default;
return $dir if $dir =~ m!^/!; # looks like a path
return $dir if $dir =~ m!^\w+://!; # looks like a URL
return $self->default_dir . '/' . $dir;
}
# return true if downloads are allowed from this directory
sub download_ok {
my $d = shift->r->dir_config('AllowDownload') || '';
return $d !~ /$NO/oi;
}
# return true if streaming is allowed from this directory
sub stream_ok {
my $d = shift->r->dir_config('AllowStream') || '';
return $d !~ /$NO/oi;
}
# return true if playing locally is allowed
sub playlocal_ok {
my $d = shift->r->dir_config('AllowPlayLocally') || '';
return $d =~ /$YES/oi;
}
# return true if we should check that the client can accomodate streaming
sub check_stream_client {
my $d = shift->r->dir_config('CheckStreamClient') || '';
return $d =~ /$YES/oi;
}
# return true if client can stream
sub is_stream_client {
my $r = shift->r;
my $h = $r->headers_in;
$h->{'Icy-MetaData'} # winamp/xmms
|| $h->{'Bandwidth'} # realplayer
|| $h->{'Accept'} =~ m!\baudio/mpeg\b! # mpg123 and others
|| $h->{'User-Agent'} =~ m!^NSPlayer/! # Microsoft media player
|| $h->{'User-Agent'} =~ m!^xmms/!;
}
# whether to read info for each MP3 file (might take a long time)
sub read_mp3_info {
my $d = shift->r->dir_config('ReadMP3Info') || '';
return $d !~ /$NO/oi;
}
# whether to time out streams
sub stream_timeout {
shift->r->dir_config('StreamTimeout') || 0;
}
# how long an album list is considered so long we should put buttons
# at the top as well as the bottom
sub file_list_is_long { shift->r->dir_config('LongList') || 10 }
sub home_label {
my $self = shift;
my $home = $self->r->dir_config('HomeLabel') ||
$self->x('Home');
return lc($home) eq 'hostname' ? $self->r->hostname : $home;
}
( run in 1.855 second using v1.01-cache-2.11-cpan-39bf76dae61 )