Apache::MP3 list_directory()

This is the code that generates the fancy directory listings.

sub list_directory {
  my $r = shift;
  my $dir = $r->filename;
  my $uri = $r->uri;
  my $default_dir = $r->dir_config('IconDir')    || ICON_DIR;
  my $stylesheet  = $r->dir_config('Stylesheet') || STYLESHEET;
  my $parent_icon = $r->dir_config('ParentIcon') || PARENTICON;
  my $cd_icon     = $r->dir_config('TitleIcon')     || CDICON;
  my $cd_list_icon= $r->dir_config('DirectoryIcon') || CDLISTICON;
  my $song_icon   = $r->dir_config('SongIcon')   || SONGICON;
  foreach (\$stylesheet,\$parent_icon,\$cd_icon,\$cd_list_icon,\$song_icon) {
    $$_ = "$default_dir/$$_" unless $$_ =~ m!^/!;
  }

  my $download_ok = $r->dir_config('AllowDownload') !~ /$NO/oi;
  my $stream_ok   = $r->dir_config('AllowStream')   !~ /$NO/oi;

  return DECLINED unless my ($directories,$mp3s) = read_directory($r,$dir);

  my $title = $uri;

  print header(),
        start_html(-title  =>$uri,
		   $stylesheet ? (-style => {-src=>$stylesheet}) :()
		  ),
        h1(img({-src=>$cd_icon},$title));

  print img({-src=>$parent_icon}),a({-href=>'..'},'Parent Directory'),br,"\n";

  if (%$directories) {
    print h2({-align=>'LEFT'},'CD Directories');

    print start_table({-border=>0,-width=>'100%'}),"\n";
    # two-column list
    my @d = sort keys %$directories;
    my $rows = @d/2;
    for (my $row=0; $row < $rows; $row++) {
      my $d1 = $d[$row];
      my $d2 = $d[$rows+$row];
      print start_TR({-valign=>'BOTTOM'});
      for (0,1) {
	my $d = $d[$_ * $rows + $row];
	print td(img({-src=>$cd_list_icon}),
		 a({-href=>escape($d).'/'},$d)),
	      td(a({-href=>escape($d).'/playlist.m3u?Play+All=1'},'[play all]'));
      }
      print end_TR,"\n";
    }
    print end_table;
  }

  if (%$mp3s) {
    print hr if %$directories;

    $uri =~ s!([^a-zA-Z0-9/])!uc sprintf("%%%02x",ord($1))!eg;
    print start_form(-action=>"${uri}playlist.m3u");

    print
      a({-name=>'cds'}),
      start_table({-border=>0,-cellspacing=>0,-width=>'100%'}),"\n";
    print  TR(
	      td(),
	      td({-align=>'LEFT',-colspan=>4},
		 submit('Play Selected'),submit('Play All'))) if $stream_ok;
    print TR({-class=>'title'},
	     th(),th({-align=>'LEFT'},[
				   $stream_ok ? 'Select/Stream' : '',
				   $download_ok ? 'Title (download)' : 'Title',
				   'Artist','Duration','Bitrate'])),"\n";

    my $count = 0;
    for my $song (sort keys %$mp3s) {
      my $url = escape($song);
      (my $play = $url) =~ s/(\.[^.]+)?$/.m3u/;
      my $highlight = $count++ % 2 ? 'highlight' : 'normal';
      print TR(
	       td({-class=>$highlight},
		  [
		   img({-src=>$song_icon}),
		   $stream_ok ? checkbox(-name=>'file',-value=>$song,-label=>' ') .
		                a({-href=>"$play?play=1"},b('[play]'))
                              : '' ,
		   $download_ok ? a({-href=>$url},$mp3s->{$song}{title} || $song)
		                : $mp3s->{$song}{title} || $song ,
		   map { $_ || '&nbsp;' } @{$mp3s->{$song}}{qw(artist duration bps)}
		  ])),"\n";
      
    }
    print  TR(td(),
	      td({-align=>'LEFT',-colspan=>4},
		 submit('Play Selected'),submit('Play All'))) if $stream_ok;
    print end_table,"\n";
    print end_form;
  }
  print end_html;
  return OK;
}


<< Previous
Contents >> Next >>

Lincoln D. Stein, lstein@cshl.org
Cold Spring Harbor Laboratory
Last modified: Sun Jun 4 13:09:13 PDT 2000