mafintosh / torrent-stream

The low level streaming torrent engine that peerflix uses
MIT License
1.94k stars 228 forks source link

File download progress #72

Open sneurlax opened 10 years ago

sneurlax commented 10 years ago

Is there a way to find a file's download progress? I've posted a question that could be related--I know that if I know all the blocks associated with a file that I could listen for the piece-indexes emitted by engine.on('download'), but I was wondering if torrent-stream has a built-in method for finding a file's download progress.

BastienClement commented 10 years ago

engine.swarm.downloaded from the internal peer-wire-swarm instance.

asapach commented 10 years ago

@galedric, that shows the traffic (total bytes downloaded). What @sneurlax wants is to translate the bitfield into file progress (percentage?) using the algorithm in #71

kuba-orlik commented 6 years ago

I use

var fileStart = file.offset;
var fileEnd = file.offset + file.length;
const pieceLength = engine.torrent.pieceLength;
var firstPiece = Math.floor(fileStart / pieceLength);
var lastPiece = Math.floor((fileEnd - 1) / pieceLength);

const file_pieces_progess = Array.from(engine.bitfield.buffer)
        .map(n => leftpad(n.toString(2), 8, "0"))
    .join("")
    .split("")
    .slice(firstPiece, lastPiece - firstPiece);

const downloaded_file_pieces_amount = file_pieces_progess.filter(n => n == 1)
    .length;
const total_file_piceces = lastPiece - firstPiece;
console.log(
    `Downloaded ${(downloaded_file_pieces_amount /
        total_file_piceces *
        100
    ).toFixed(2)}% @${prettyBytes(engine.swarm.downloadSpeed())}/s`
);

and add it to engine.on("verify", ...)

I'm 100% sure there's a more efficient way to do this, but at least that's where the necessary data is