Refactor main.js: torrent list controller

This commit is contained in:
DC
2016-06-30 19:19:10 -07:00
parent 4319ef2853
commit f85e0a61b1
5 changed files with 422 additions and 347 deletions

View File

@@ -1,6 +1,8 @@
module.exports = {
getPosterPath,
getTorrentPath
getTorrentPath,
getByKey,
getTorrentID
}
var path = require('path')
@@ -22,3 +24,22 @@ function getPosterPath (torrentSummary) {
// Backslashes in URLS in CSS cause bizarre string encoding issues
return posterPath.replace(/\\/g, '/')
}
// Expects a torrentSummary
// Returns a torrentID: filename, magnet URI, or infohash
function getTorrentID (torrentSummary) {
var s = torrentSummary
if (s.torrentFileName) { // Load torrent file from disk
return getTorrentPath(s)
} else { // Load torrent from DHT
return s.magnetURI || s.infoHash
}
}
// Expects a torrentKey or infoHash
// Returns the corresponding torrentSummary, or undefined
function getByKey (state, torrentKey) {
if (!torrentKey) return undefined
return state.saved.torrents.find((x) =>
x.torrentKey === torrentKey || x.infoHash === torrentKey)
}