App-MHFS

 view release on metacpan or  search on metacpan

lib/MHFS/Plugin/MusicLibrary.pm  view on Meta::CPAN

        my $source = $self->{'settings'}{'SOURCES'}{$msource->[0]};
        if($sendFiles{$source->{'type'}}->($request, $node->{'path'}, $node->{'node'}, $source, $nameloc)) {
            return 1;
        }
    }
    say "SendFromLibrary: did not find in library, 404ing";
    say "name: " . $request->{'qs'}{'name'};
    $request->Send404;
}

sub SendResources {
    my ($self, $request) = @_;

    if(! HAS_MHFS_XS) {
        say __PACKAGE__.": route not available without XS";
        $request->Send503();
        return;
    }

    my $utf8name = decode('UTF-8', $request->{'qs'}{'name'});
    foreach my $msource (@{$self->{'sources'}}) {
        my $node = $self->FindInLibrary($msource, $utf8name);
        next if ! $node;
        my $comments = MHFS::XS::get_vorbis_comments($node->{'path'});
        my $commenthash = {};
        foreach my $comment (@{$comments}) {
            $comment = decode('UTF-8', $comment);
            my ($key, $value) = split('=', $comment);
            $commenthash->{$key} = $value;
        }
        $request->SendAsJSON($commenthash);
        return 1;
    }
    say "SendFromLibrary: did not find in library, 404ing";
    say "name: " . $request->{'qs'}{'name'};
    $request->Send404;
}

sub SendArt {
    my ($self, $request) = @_;

    my $utf8name = decode('UTF-8', $request->{'qs'}{'name'});
    foreach my $msource (@{$self->{'sources'}}) {
        my $node = $self->FindInLibrary($msource, $utf8name);
        next if ! $node;

        my $dname = $node->{'path'};
        my $dh;
        if(! opendir($dh, $dname)) {
            $dname = dirname($node->{'path'});
            if(! opendir($dh, $dname)) {
                $request->Send404;
                return 1;
            }
        }

        # scan dir for art
        my @files;
        while(my $fname = readdir($dh)) {
            my $last = lc(substr($fname, -4));
            push @files, $fname if(($last eq '.png') || ($last eq '.jpg') || ($last eq 'jpeg'));
        }
        closedir($dh);
        if( ! @files) {
            $request->Send404;
            return 1;
        }
        my $tosend = "$dname/" . $files[0];
        foreach my $file (@files) {
            foreach my $expname ('cover', 'front', 'album') {
                if(substr($file, 0, length($expname)) eq $expname) {
                    $tosend = "$dname/$file";
                    last;
                }
            }
        }
        say "tosend $tosend";
        $request->SendLocalFile($tosend);
        return 1;
    }
}

sub UpdateLibrariesAsync {
    my ($self, $evp, $onUpdateEnd) = @_;
    MHFS::Process->new_output_child($evp, sub {
        # done in child
        my ($datachannel) = @_;

        # save references to before
        my @potentialupdates = ('html', 'musicdbhtml', 'musicdbjson');
        my %before;
        foreach my $pupdate (@potentialupdates) {
            $before{$pupdate} = $self->{$pupdate};
        }

        # build the new libraries
        $self->BuildLibraries();

        # determine what needs to be updated
        my @updates = (['sources', $self->{'sources'}]);
        foreach my $pupdate(@potentialupdates) {
            if($before{$pupdate} ne $self->{$pupdate}) {
                push @updates, [$pupdate, $self->{$pupdate}];
            }
        }

        # serialize and output
        my $pipedata = freeze(\@updates);
        print $datachannel $pipedata;
        exit 0;
    }, sub {
        my ($out, $err) = @_;
        say "BEGIN_FROM_CHILD---------";
        print $err;
        say "END_FROM_CHILD-----------";
        my $unthawed;
        {
            local $@;
            unless (eval {
                $unthawed = thaw($out);
                return 1;



( run in 0.490 second using v1.01-cache-2.11-cpan-df04353d9ac )