App-MHFS
view release on metacpan or search on metacpan
lib/MHFS/Plugin/MusicLibrary.pm view on Meta::CPAN
package MHFS::Plugin::MusicLibrary v0.7.0;
use 5.014;
use strict; use warnings;
use feature 'say';
use Cwd qw(abs_path getcwd);
use File::Find;
use Data::Dumper;
use Devel::Peek;
use Fcntl ':mode';
use File::stat;
use File::Basename;
use File::Path qw(make_path);
use Scalar::Util qw(looks_like_number);
use MHFS::Util qw(get_printable_utf8 escape_html_noquote LOCK_GET_LOCKDATA LOCK_WRITE UNLOCK_WRITE);
BEGIN {
if( ! (eval "use JSON; 1")) {
eval "use JSON::PP; 1" or die "No implementation of JSON available";
warn __PACKAGE__.": Using PurePerl version of JSON (JSON::PP)";
}
}
use Encode qw(decode encode);
use URI::Escape;
use Storable qw(dclone);
use Fcntl ':mode';
use Time::HiRes qw( usleep clock_gettime CLOCK_REALTIME CLOCK_MONOTONIC);
use Scalar::Util qw(looks_like_number weaken);
use POSIX qw/ceil/;
use Storable qw( freeze thaw);
#use ExtUtils::testlib;
use FindBin;
use File::Spec;
use List::Util qw[min max];
use HTML::Template;
use MHFS::Process;
# Optional dependency, MHFS::XS
BEGIN {
use constant HAS_MHFS_XS => (eval "use MHFS::XS; 1");
if(! HAS_MHFS_XS) {
warn __PACKAGE__.": XS not available";
}
}
# read the directory tree from desk and store
# this assumes filenames are UTF-8ish, the octlets will be the actual filename, but the printable filename is created by decoding it as UTF-8
sub BuildLibrary {
my ($path) = @_;
my $statinfo = stat($path);
return undef if(! $statinfo);
my $basepath = basename($path);
my $utf8name = get_printable_utf8($basepath);
if(!S_ISDIR($statinfo->mode)){
return undef if($path !~ /\.(flac|mp3|m4a|wav|ogg|webm)$/);
return [$basepath, $statinfo->size, undef, $utf8name];
}
else {
my $dir;
if(! opendir($dir, $path)) {
warn "outputdir: Cannot open directory: $path $!";
return undef;
}
my @files = sort { uc($a) cmp uc($b)} (readdir $dir);
closedir($dir);
my @tree;
my $size = 0;
foreach my $file (@files) {
next if(($file eq '.') || ($file eq '..'));
if(my $file = BuildLibrary("$path/$file")) {
push @tree, $file;
$size += $file->[1];
}
}
return undef if( $size eq 0);
return [$basepath, $size, \@tree, $utf8name];
}
}
sub ToHTML {
my ($files, $where) = @_;
$where //= '';
my $buf = '';
my $name_unencoded = $files->[3];
my $name = ${escape_html_noquote($name_unencoded)};
if($files->[2]) {
my $dir = $files->[0];
( run in 0.805 second using v1.01-cache-2.11-cpan-39bf76dae61 )