From b02f2364287a6237084370c77fcc93eedd57b593 Mon Sep 17 00:00:00 2001 From: DC Date: Sun, 20 Mar 2016 21:55:54 -0700 Subject: [PATCH] Show files even when torrent isn't torrenting --- renderer/index.css | 2 +- renderer/index.js | 10 ++++++++++ renderer/views/torrent-list.js | 28 +++++++++++++++++++--------- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/renderer/index.css b/renderer/index.css index 38552636..f0fe4654 100644 --- a/renderer/index.css +++ b/renderer/index.css @@ -396,7 +396,7 @@ input { position: absolute; top: 20px; left: 20px; - width: calc(100% - 100px); + width: calc(100% - 150px); text-shadow: rgba(0, 0, 0, 0.5) 0 0 4px; } diff --git a/renderer/index.js b/renderer/index.js index a0e12396..51081f85 100644 --- a/renderer/index.js +++ b/renderer/index.js @@ -453,6 +453,7 @@ function addTorrentEvents (torrent) { torrentSummary.ready = true torrentSummary.name = torrentSummary.displayName || torrent.name torrentSummary.infoHash = torrent.infoHash + torrentSummary.files = torrent.files.map(summarizeFileInTorrent) 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 // have to download it again. Never ask the DHT the same question twice. function saveTorrentFile (torrentSummary, torrent) { diff --git a/renderer/views/torrent-list.js b/renderer/views/torrent-list.js index 1bdbfdd6..9e8ae857 100644 --- a/renderer/views/torrent-list.js +++ b/renderer/views/torrent-list.js @@ -131,8 +131,11 @@ function TorrentList (state, dispatch) { // Show files, per-file download status and play buttons, and so on 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 - if (!torrent || !torrent.files) { + if (!files) { // We don't know what files this torrent contains var message = torrent ? 'Downloading torrent data using magnet link...' @@ -140,7 +143,7 @@ function TorrentList (state, dispatch) { filesElement = hx`
${message}
` } else { // 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`
Files @@ -165,20 +168,27 @@ function TorrentList (state, dispatch) { } // 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) { // First, find out how much of the file we've downloaded - var numPieces = file._endPiece - file._startPiece + 1 - var numPiecesPresent = 0 - for (var piece = file._startPiece; piece <= file._endPiece; piece++) { - if (torrent.bitfield.get(piece)) numPiecesPresent++ + // (If we're not currently torrenting, just say whether we have the file or not) + if (file._startPiece) { + var numPieces = file._endPiece - file._startPiece + 1 + 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`${isDone ? 'check' : 'close'}` } - var progress = Math.round(100 * numPiecesPresent / numPieces) + '%' - var isDone = numPieces === numPiecesPresent // Second, render the file as a table row var icon 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 */ } else if (TorrentPlayer.isPlayable(file)) { icon = 'play_arrow' /* playable? add option to play */