From 6d664f2086842d9e8839c2252e112f01ca96426b Mon Sep 17 00:00:00 2001 From: DC Date: Sat, 16 Jul 2016 16:03:22 -0700 Subject: [PATCH] Refactor main.js: TorrentPlayer.isTorrent --- renderer/lib/torrent-player.js | 33 ++++++++++++++++++++++++--------- renderer/main.js | 9 +-------- renderer/views/torrent-list.js | 2 +- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/renderer/lib/torrent-player.js b/renderer/lib/torrent-player.js index fe81b020..c2ee4003 100644 --- a/renderer/lib/torrent-player.js +++ b/renderer/lib/torrent-player.js @@ -2,21 +2,21 @@ module.exports = { isPlayable, isVideo, isAudio, - isPlayableTorrent, + isTorrent, + isPlayableTorrentSummary, pickFileToPlay } var path = require('path') -/** - * Determines whether a file in a torrent is audio/video we can play - */ +// Checks whether a fileSummary or file path is audio/video that we can play, +// based on the file extension function isPlayable (file) { return isVideo(file) || isAudio(file) } +// Checks whether a fileSummary or file path is playable video function isVideo (file) { - var ext = path.extname(file.name).toLowerCase() return [ '.avi', '.m4v', @@ -27,21 +27,36 @@ function isVideo (file) { '.ogv', '.webm', '.wmv' - ].includes(ext) + ].includes(getFileExtension(file)) } +// Checks whether a fileSummary or file path is playable audio function isAudio (file) { - var ext = path.extname(file.name).toLowerCase() return [ '.aac', '.ac3', '.mp3', '.ogg', '.wav' - ].includes(ext) + ].includes(getFileExtension(file)) } -function isPlayableTorrent (torrentSummary) { +// Checks if the argument is either: +// - a string that's a valid filename ending in .torrent +// - a file object where obj.name is ends in .torrent +// - a string that's a magnet link (magnet://...) +function isTorrent (file) { + var isTorrentFile = getFileExtension(file) === '.torrent' + var isMagnet = typeof file === 'string' && /^(stream-)?magnet:/.test(file) + return isTorrentFile || isMagnet +} + +function getFileExtension (file) { + var name = typeof file === 'string' ? file : file.name + return path.extname(name).toLowerCase() +} + +function isPlayableTorrentSummary (torrentSummary) { return torrentSummary.files && torrentSummary.files.some(isPlayable) } diff --git a/renderer/main.js b/renderer/main.js index c5a60f0e..689cd3ca 100644 --- a/renderer/main.js +++ b/renderer/main.js @@ -326,13 +326,6 @@ function resumeTorrents () { .forEach((torrentSummary) => controllers.torrentList.startTorrentingSummary(torrentSummary)) } -function isTorrent (file) { - var name = typeof file === 'string' ? file : file.name - var isTorrentFile = path.extname(name).toLowerCase() === '.torrent' - var isMagnet = typeof file === 'string' && /^(stream-)?magnet:/.test(file) - return isTorrentFile || isMagnet -} - // Gets a torrent summary {name, infoHash, status} from state.saved.torrents // Returns undefined if we don't know that infoHash function getTorrentSummary (torrentKey) { @@ -561,7 +554,7 @@ function onOpen (files) { var subtitles = files.filter(controllers.subtitles.isSubtitle) if (state.location.url() === 'home' || subtitles.length === 0) { - if (files.every(isTorrent)) { + if (files.every(TorrentPlayer.isTorrent)) { if (state.location.url() !== 'home') { backToList() } diff --git a/renderer/views/torrent-list.js b/renderer/views/torrent-list.js index fad2ff32..6b6d68ac 100644 --- a/renderer/views/torrent-list.js +++ b/renderer/views/torrent-list.js @@ -148,7 +148,7 @@ function TorrentList (state) { // Only show the play button for torrents that contain playable media var playButton - if (TorrentPlayer.isPlayableTorrent(torrentSummary)) { + if (TorrentPlayer.isPlayableTorrentSummary(torrentSummary)) { playButton = hx`