App-MHFS

 view release on metacpan or  search on metacpan

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

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;
            }) {
                warn("thaw threw exception");
            }
        }
        if($unthawed){
            foreach my $update (@$unthawed) {
                say "Updating " . $update->[0];
                $self->{$update->[0]} = $update->[1];
            }
        }
        else {
            say "failed to thaw, library not updated.";
        }
        $onUpdateEnd->();
    });
}

sub new {
    my ($class, $settings) = @_;
    my $self =  {'settings' => $settings};
    bless $self, $class;
    my $pstart = __PACKAGE__.":";

    # no sources until loaded
    $self->{'sources'} = [];
    $self->{'html'} = __PACKAGE__.' not loaded';
    $self->{'musicdbhtml'} = __PACKAGE__.' not loaded';
    $self->{'musicdbjson'} = '{}';

    my $musicpageroute = sub {
        my ($request) = @_;
        return $self->SendLibrary($request);
    };

    my $musicdlroute = sub {
        my ($request) = @_;
        return $self->SendFromLibrary($request);
    };

    my $musicresourcesroute = sub {
        my ($request) = @_;
        return $self->SendResources($request);
    };

    $self->{'routes'} = [
        ['/music', $musicpageroute],
        ['/music_dl', $musicdlroute],
        ['/music_resources', $musicresourcesroute],
        ['/music_art', sub {
            my ($request) = @_;
            return $self->SendArt($request);
        }]
    ];

    $self->{'timers'} = [
        # update the library at start and periodically
        [0, 300, sub {
            my ($timer, $current_time, $evp) = @_;
            say "$pstart library timer";
            UpdateLibrariesAsync($self, $evp, sub {
                say "$pstart library timer done";
            });
            return 1;
        }],
    ];

    return $self;
}

1;



( run in 1.239 second using v1.01-cache-2.11-cpan-39bf76dae61 )