Show files even when torrent isn't torrenting

This commit is contained in:
DC
2016-03-20 21:55:54 -07:00
parent 041dd708ec
commit 720c9f4625
3 changed files with 30 additions and 10 deletions

View File

@@ -396,7 +396,7 @@ input {
position: absolute; position: absolute;
top: 20px; top: 20px;
left: 20px; left: 20px;
width: calc(100% - 100px); width: calc(100% - 150px);
text-shadow: rgba(0, 0, 0, 0.5) 0 0 4px; text-shadow: rgba(0, 0, 0, 0.5) 0 0 4px;
} }

View File

@@ -453,6 +453,7 @@ function addTorrentEvents (torrent) {
torrentSummary.ready = true torrentSummary.ready = true
torrentSummary.name = torrentSummary.displayName || torrent.name torrentSummary.name = torrentSummary.displayName || torrent.name
torrentSummary.infoHash = torrent.infoHash torrentSummary.infoHash = torrent.infoHash
torrentSummary.files = torrent.files.map(summarizeFileInTorrent)
saveTorrentFile(torrentSummary, torrent) saveTorrentFile(torrentSummary, torrent)
@@ -493,6 +494,15 @@ function generateTorrentPoster (torrent, torrentSummary) {
}) })
} }
// Produces a JSON saveable summary of a file in a torrent
function summarizeFileInTorrent (file) {
return {
name: file.name,
length: file.length,
isDownloaded: false
}
}
// Every time we resolve a magnet URI, save the torrent file so that we never // Every time we resolve a magnet URI, save the torrent file so that we never
// have to download it again. Never ask the DHT the same question twice. // have to download it again. Never ask the DHT the same question twice.
function saveTorrentFile (torrentSummary, torrent) { function saveTorrentFile (torrentSummary, torrent) {

View File

@@ -131,8 +131,11 @@ function TorrentList (state, dispatch) {
// Show files, per-file download status and play buttons, and so on // Show files, per-file download status and play buttons, and so on
function renderTorrentDetails (torrent, torrentSummary) { function renderTorrentDetails (torrent, torrentSummary) {
// If we're currently torrenting, show details directly from WebTorrent, including % downloaded
// Otherwise, show a summary of files in the torrent, if available
var files = (torrent && torrent.files) || torrentSummary.files
var filesElement var filesElement
if (!torrent || !torrent.files) { if (!files) {
// We don't know what files this torrent contains // We don't know what files this torrent contains
var message = torrent var message = torrent
? 'Downloading torrent data using magnet link...' ? 'Downloading torrent data using magnet link...'
@@ -140,7 +143,7 @@ function TorrentList (state, dispatch) {
filesElement = hx`<div class='files warning'>${message}</div>` filesElement = hx`<div class='files warning'>${message}</div>`
} else { } else {
// We do know the files. List them and show download stats for each one // We do know the files. List them and show download stats for each one
var fileRows = torrent.files.map((file, index) => renderFileRow(torrent, torrentSummary, file, index)) var fileRows = files.map((file, index) => renderFileRow(torrent, torrentSummary, file, index))
filesElement = hx` filesElement = hx`
<div class='files'> <div class='files'>
<strong>Files</strong> <strong>Files</strong>
@@ -165,20 +168,27 @@ function TorrentList (state, dispatch) {
} }
// Show a single file in the details view for a single torrent // Show a single file in the details view for a single torrent
// File can either be a WebTorrent file (if we're currently torrenting it) or an element of torrentSummary.files
function renderFileRow (torrent, torrentSummary, file, index) { function renderFileRow (torrent, torrentSummary, file, index) {
// First, find out how much of the file we've downloaded // First, find out how much of the file we've downloaded
var numPieces = file._endPiece - file._startPiece + 1 // (If we're not currently torrenting, just say whether we have the file or not)
var numPiecesPresent = 0 if (file._startPiece) {
for (var piece = file._startPiece; piece <= file._endPiece; piece++) { var numPieces = file._endPiece - file._startPiece + 1
if (torrent.bitfield.get(piece)) numPiecesPresent++ var numPiecesPresent = 0
for (var piece = file._startPiece; piece <= file._endPiece; piece++) {
if (torrent.bitfield.get(piece)) numPiecesPresent++
}
var progress = Math.round(100 * numPiecesPresent / numPieces) + '%'
var isDone = numPieces === numPiecesPresent
} else {
var isDone = file.isDownloaded
var progress = hx`<i.icon>${isDone ? 'check' : 'close'}</i>`
} }
var progress = Math.round(100 * numPiecesPresent / numPieces) + '%'
var isDone = numPieces === numPiecesPresent
// Second, render the file as a table row // Second, render the file as a table row
var icon var icon
var iconClass = '' var iconClass = ''
if (state.playing.infoHash === torrent.infoHash && state.playing.fileIndex === index) { if (state.playing.infoHash === torrentSummary.infoHash && state.playing.fileIndex === index) {
icon = 'pause_arrow' /* playing? add option to pause */ icon = 'pause_arrow' /* playing? add option to pause */
} else if (TorrentPlayer.isPlayable(file)) { } else if (TorrentPlayer.isPlayable(file)) {
icon = 'play_arrow' /* playable? add option to play */ icon = 'play_arrow' /* playable? add option to play */