App-MHFS

 view release on metacpan or  search on metacpan

lib/MHFS/Plugin/BitTorrent/Client/Interface.pm  view on Meta::CPAN

        $buf   .= "<tbody><tr><td>$torrent_name</td><td>$size_print</td><td>$percent_print</td><td>$done_print</td></tr></tbody>";
        $buf   .= '</table>';

        # Assume we are downloading, if the bytes don't match
        if($bytes_done < $size_bytes) {
            $buf   .= '<meta http-equiv="refresh" content="3">';
            $request->SendHTML($buf);
        }
        else {
            # print out the files with usage options
            MHFS::BitTorrent::Client::torrent_file_information($server, $qs->{'infohash'}, $torrent_raw, sub {
            if(! defined $_[0]){ $request->Send404; return; };
            my ($tfi) = @_;
            my @files = sort (keys %$tfi);
            $buf .= '<br>';
            $buf .= '<table border="1" >';
            $buf .= '<thead><tr><th>File</th><th>Size</th><th>DL</th><th>Play in browser</th></tr></thead>';
            $buf .= '<tbody';
            foreach my $file (@files) {
                my $htmlfile = ${escape_html($file)};
                my $urlfile = uri_escape($file);
                my $link = '<a href="get_video?name=' . $urlfile . '&fmt=noconv">DL</a>';
                my $playlink = play_in_browser_link($file, $urlfile);
                $buf .= "<tr><td>$htmlfile</td><td>" . get_SI_size($tfi->{$file}{'size'}) . "</td><td>$link</td>";
                $buf .= "<td>$playlink</td>" if(!defined($qs->{'playinbrowser'}) || ($qs->{'playinbrowser'} == 1));
                $buf .= "</tr>";
            }
            $buf .= '</tbody';
            $buf .= '</table>';

            $request->SendHTML($buf);
            });
        }

        });
    }
    else {
        MHFS::BitTorrent::Client::torrent_list_torrents($server, sub{
            if(! defined $_[0]){ $request->Send404; return; };
            my ($rtresponse) = @_;
            my @lines = split( /\n/, $rtresponse);
            my $buf = '<h1>Torrents</h1>';
            $buf  .=  '<h3><a href="video?action=browsemovies">Browse Movies</a> | <a href="video">Video</a> | <a href="music">Music</a></h3>';
            $buf   .= '<table border="1" >';
            $buf   .= '<thead><tr><th>Name</th><th>Hash</th><th>Size</th><th>Done</th><th>Private</th></tr></thead>';
            $buf   .= "<tbody>";
            my $curtor = '';
            while(1) {
                if($curtor =~ /^\[(u?)['"](.+)['"],\s'(.+)',\s([0-9]+),\s([0-9]+),\s([0-9]+)\]$/) {
                    my %torrent;
                    my $is_unicode = $1;
                    $torrent{'name'} = $2;
                    $torrent{'hash'} = $3;
                    $torrent{'size_bytes'} = $4;
                    $torrent{'bytes_done'} = $5;
                    $torrent{'private'} = $6;
                    if($is_unicode) {
                        my $escaped_unicode = $torrent{'name'};
                        $torrent{'name'} =~ s/\\u(.{4})/chr(hex($1))/eg;
                        $torrent{'name'} =~ s/\\x(.{2})/chr(hex($1))/eg;
                        my $decoded_as = $torrent{'name'};
                        $torrent{'name'} = ${escape_html($torrent{'name'})};
                        if($qs->{'logunicode'}) {
                            say 'unicode escaped: ' . $escaped_unicode;
                            say 'decoded as: ' . $decoded_as;
                            say 'html escaped ' . $torrent{'name'};
                        }
                    }
                    $buf .= '<tr><td>' . $torrent{'name'} . '</td><td>' . $torrent{'hash'} . '</td><td>' . $torrent{'size_bytes'} . '</td><td>' . $torrent{'bytes_done'} . '</td><td>' . $torrent{'private'} . '</td></tr>';
                    $curtor = '';
                }
                else {
                    my $line = shift @lines;
                    if(! $line) {
                        last;
                    }
                    $curtor .= $line;
                }
            }
            $buf   .= '</tbody></table>';
            $request->SendHTML($buf);
        });
    }
}

sub torrentload {
    my ($request) = @_;
    my $packagename = __PACKAGE__;
    my $self = $request->{'client'}{server}{'loaded_plugins'}{$packagename};

    if((exists $request->{'qs'}{'dlsubsystem'}) && (exists $request->{'qs'}{'privdata'}) ) {
        my $subsystem = $request->{'qs'}{'dlsubsystem'};
        if(exists $self->{'dlsubsystems'}{$subsystem}) {
            my $server = $request->{'client'}{'server'};
            $self->{'dlsubsystems'}{$subsystem}->dl($server, $request->{'qs'}{'privdata'}, sub {
                my ($result, $destdir) = @_;
                if(! $result) {
                    say "failed to dl torrent";
                    $request->Send404;
                    return;
                }
                MHFS::BitTorrent::Client::torrent_start($server, \$result, $destdir, {
                    'on_success' => sub {
                        my ($hexhash) = @_;
                        $request->SendRedirectRawURL(301, 'view?infohash=' . $hexhash);
                    },
                    'on_failure' => sub {
                        $request->Send404;
                    }
                });
            });
            return;
        }
    }
    $request->Send404;
}

sub new {
    my ($class, $settings) = @_;
    my $self =  { 'dlsubsystems' => {}};
    bless $self, $class;

    $self->{'routes'} = [
        [ '/torrent/view', \&torrentview ],
        [ '/torrent/load', \&torrentload ]



( run in 0.705 second using v1.01-cache-2.11-cpan-5b529ec07f3 )