Fix duplicate torrent handling
WebTorrent 0.91 changed how duplicate torrents are handled, which broke handling in WebTorrent Desktop. After this PR: - No more try-catch on client.add -- this has never thrown errors. - No check for duplicate torrent.key value since client.add no longer returns the same torrent object when adding a duplicate torrent. It emits 'error' instead, and that case is already handled :)
This commit is contained in:
@@ -724,16 +724,16 @@ function torrentWarning (torrentKey, message) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function torrentError (torrentKey, message) {
|
function torrentError (torrentKey, message) {
|
||||||
var torrentSummary = getTorrentSummary(torrentKey)
|
// TODO: WebTorrent needs semantic errors
|
||||||
|
if (message.startsWith('Cannot add duplicate torrent')) {
|
||||||
|
// Remove infohash from the message
|
||||||
|
message = 'Cannot add duplicate torrent'
|
||||||
|
}
|
||||||
|
onError(message)
|
||||||
|
|
||||||
// TODO: WebTorrent should have semantic errors
|
var torrentSummary = getTorrentSummary(torrentKey)
|
||||||
if (message.startsWith('There is already a swarm')) {
|
if (torrentSummary) {
|
||||||
onError(new Error('Can\'t add duplicate torrent'))
|
console.log('Pausing torrent %s due to error: %s', torrentSummary.infoHash, message)
|
||||||
} else if (!torrentSummary) {
|
|
||||||
onError(message)
|
|
||||||
} else {
|
|
||||||
console.log('error, stopping torrent %s (%s):\n\t%o',
|
|
||||||
torrentSummary.name, torrentSummary.infoHash, message)
|
|
||||||
torrentSummary.status = 'paused'
|
torrentSummary.status = 'paused'
|
||||||
update()
|
update()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -68,23 +68,14 @@ function init () {
|
|||||||
// See https://github.com/feross/webtorrent/blob/master/docs/api.md#clientaddtorrentid-opts-function-ontorrent-torrent-
|
// See https://github.com/feross/webtorrent/blob/master/docs/api.md#clientaddtorrentid-opts-function-ontorrent-torrent-
|
||||||
function startTorrenting (torrentKey, torrentID, path, fileModtimes) {
|
function startTorrenting (torrentKey, torrentID, path, fileModtimes) {
|
||||||
console.log('starting torrent %s: %s', torrentKey, torrentID)
|
console.log('starting torrent %s: %s', torrentKey, torrentID)
|
||||||
var torrent
|
|
||||||
try {
|
var torrent = client.add(torrentID, {
|
||||||
torrent = client.add(torrentID, {
|
path: path,
|
||||||
path: path,
|
fileModtimes: fileModtimes
|
||||||
fileModtimes: fileModtimes
|
})
|
||||||
})
|
|
||||||
} catch (err) {
|
|
||||||
return ipc.send('wt-error', torrentKey, err.message)
|
|
||||||
}
|
|
||||||
// If we add a duplicate magnet URI or infohash, WebTorrent returns the
|
|
||||||
// existing torrent object! (If we add a duplicate torrent file, it creates a
|
|
||||||
// new torrent object and raises an error later.) Workaround:
|
|
||||||
if (torrent.key) {
|
|
||||||
return ipc.send('wt-error', torrentKey, 'Can\'t add duplicate torrent')
|
|
||||||
}
|
|
||||||
torrent.key = torrentKey
|
torrent.key = torrentKey
|
||||||
addTorrentEvents(torrent)
|
addTorrentEvents(torrent)
|
||||||
|
|
||||||
return torrent
|
return torrent
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user