Fix startTorrenting / add file functionality

This commit is contained in:
DC
2016-03-20 01:38:51 -07:00
parent 26dc1e82fd
commit b6bd9dbb70

View File

@@ -55,7 +55,11 @@ function init () {
// WebTorrent.app is a hybrid client, as explained here: https://webtorrent.io/faq // WebTorrent.app is a hybrid client, as explained here: https://webtorrent.io/faq
state.client = new WebTorrent() state.client = new WebTorrent()
state.client.on('warning', onWarning) state.client.on('warning', onWarning)
state.client.on('error', onError) state.client.on('error', function (err) {
// TODO: WebTorrent should have semantic errors
if (err.message.startsWith('There is already a swarm')) onError('Couldn\'t add duplicate torrent')
else onError(err)
})
resumeTorrents() /* restart everything we were torrenting last time the app ran */ resumeTorrents() /* restart everything we were torrenting last time the app ran */
// The UI is built with virtual-dom, a minimalist library extracted from React // The UI is built with virtual-dom, a minimalist library extracted from React
@@ -300,7 +304,7 @@ function loadState (callback) {
function resumeTorrents () { function resumeTorrents () {
state.saved.torrents state.saved.torrents
.filter((x) => x.status !== 'paused') .filter((x) => x.status !== 'paused')
.forEach((x) => startTorrenting(x.infoHash)) .forEach((x) => startTorrentingSummary(x))
} }
// Write state.saved to the JSON state file // Write state.saved to the JSON state file
@@ -373,12 +377,10 @@ function getTorrent (infoHash) {
// Adds a torrent to the list, starts downloading/seeding. TorrentID can be a // Adds a torrent to the list, starts downloading/seeding. TorrentID can be a
// magnet URI, infohash, or torrent file: https://github.com/feross/webtorrent#clientaddtorrentid-opts-function-ontorrent-torrent- // magnet URI, infohash, or torrent file: https://github.com/feross/webtorrent#clientaddtorrentid-opts-function-ontorrent-torrent-
function addTorrent (torrentId) { function addTorrent (torrentId) {
// Charlie Chaplin: 'magnet:?xt=urn:btih:cddf0459a718523480f7499da5ed1a504cffecb8&dn=charlie%5Fchaplin%5Ffilm%5Ffestival' var torrent = startTorrentingID(torrentId)
if (!torrentId) torrentId = 'magnet:?xt=urn:btih:6a02592d2bbc069628cd5ed8a54f88ee06ac0ba5&dn=CosmosLaundromatFirstCycle' torrent.on('infoHash', function () {
addTorrentToList(torrent)
var torrent = startTorrenting(torrentId) })
addTorrentToList(torrent)
} }
function addTorrentToList (torrent) { function addTorrentToList (torrent) {
@@ -403,19 +405,25 @@ function addTorrentToList (torrent) {
} }
} }
// Starts downloading and/or seeding a given torrent, torrentSummary or magnet URI // Starts downloading and/or seeding a given torrentSummary. Returns WebTorrent object
function startTorrenting (infoHash) { function startTorrentingSummary (torrentSummary) {
checkIfTorrentFileExists(infoHash, function (torrentPath, exists) { var s = torrentSummary
var torrentID = exists ? torrentPath : infoHash if (s.torrentPath) return startTorrentingID(s.torrentPath)
var torrent = state.client.add(torrentID, { else if (s.magnetURI) return startTorrentingID(s.magnetURI)
path: state.saved.downloadPath // Use downloads folder else return startTorrentingID(s.infoHash)
})
addTorrentEvents(torrent)
return torrent
})
} }
// Stops downloading and/or seeding. See startTorrenting // Starts a given TorrentID, which can be an infohash, magnet URI, etc. Returns WebTorrent object
// See https://github.com/feross/webtorrent/blob/master/docs/api.md#clientaddtorrentid-opts-function-ontorrent-torrent-
function startTorrentingID (torrentID) {
var torrent = state.client.add(torrentID, {
path: state.saved.downloadPath // Use downloads folder
})
addTorrentEvents(torrent)
return torrent
}
// Stops downloading and/or seeding
function stopTorrenting (infoHash) { function stopTorrenting (infoHash) {
var torrent = getTorrent(infoHash) var torrent = getTorrent(infoHash)
if (torrent) torrent.destroy() if (torrent) torrent.destroy()
@@ -511,11 +519,11 @@ function checkIfTorrentFileExists (infoHash, cb) {
}) })
} }
function startServer (infoHash, index, cb) { function startServer (torrentSummary, index, cb) {
if (state.server) return cb() if (state.server) return cb()
var torrent = getTorrent(infoHash) var torrent = getTorrent(torrentSummary.infoHash)
if (!torrent) torrent = startTorrenting(infoHash) if (!torrent) torrent = startTorrentingSummary(torrentSummary)
if (torrent.ready) startServerFromReadyTorrent(torrent, index, cb) if (torrent.ready) startServerFromReadyTorrent(torrent, index, cb)
else torrent.on('ready', () => startServerFromReadyTorrent(torrent, index, cb)) else torrent.on('ready', () => startServerFromReadyTorrent(torrent, index, cb))
} }
@@ -571,7 +579,7 @@ function openPlayer (torrentSummary, index) {
update() update()
}, 10000) /* give it a few seconds */ }, 10000) /* give it a few seconds */
startServer(torrentSummary.infoHash, index, function (err) { startServer(torrentSummary, index, function (err) {
if (err) return onError(err) if (err) return onError(err)
// if we timed out (user clicked play a long time ago), don't autoplay // if we timed out (user clicked play a long time ago), don't autoplay
@@ -627,7 +635,7 @@ function closePlayer () {
function toggleTorrent (torrentSummary) { function toggleTorrent (torrentSummary) {
if (torrentSummary.status === 'paused') { if (torrentSummary.status === 'paused') {
torrentSummary.status = 'new' torrentSummary.status = 'new'
startTorrenting(torrentSummary.infoHash) startTorrentingSummary(torrentSummary)
playInterfaceSound(config.SOUND_ENABLE) playInterfaceSound(config.SOUND_ENABLE)
} else { } else {
torrentSummary.status = 'paused' torrentSummary.status = 'paused'
@@ -689,7 +697,7 @@ function restoreBounds () {
} }
function onError (err) { function onError (err) {
console.error(err.stack) if (err.stack) console.error(err.stack)
playInterfaceSound(config.SOUND_ERROR) playInterfaceSound(config.SOUND_ERROR)
state.errors.push({ state.errors.push({
time: new Date().getTime(), time: new Date().getTime(),