mafintosh / torrent-stream

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

Blocks associated with file #71

Open sneurlax opened 10 years ago

sneurlax commented 10 years ago

In a multi-file torrent (read: not typical movie torrents as used with peerflix, popcorn time, etc., i.e. music albums,) how would I find the blocks (piece-indexes) associated with each file?

Specifically: engine.on('download') emits a piece-index; how do I know where that lies within a file? That is, is there a way to tell that a file consists of blocks 52-79?

Thanks for reading. I'll document as I figure this out.

BastienClement commented 10 years ago

In torrent-stream, this is handled by the storage backend.

https://github.com/mafintosh/torrent-stream/blob/master/lib/storage.js

Unfortunately, the piecesMap object is not publicly available. The main logic however is quite simple.

var fileStart = file.offset;
var fileEnd = file.offset + file.length;

var firstPiece = Math.floor(fileStart / pieceLength);
var lastPiece = Math.floor((fileEnd - 1) / pieceLength);
amilajack commented 8 years ago

@galedric @jaruba How can I get the pieceLength in this example?

jaruba commented 8 years ago

@amilajack

var peerflix = require('peerflix'),
    engine = peerflix(magnetLink);

engine.server.on('listening', function() {
    console.log('Piece length: ' + engine.torrent.pieceLength);
});

Should work, I didn't test it though.

amilajack commented 8 years ago

@jaruba Thank! Sorry for late response. I must have missed the notification.

Also, how is @galedric is getting his file object?