Refactor main.js: TorrentPlayer.isTorrent

This commit is contained in:
DC
2016-07-16 16:03:22 -07:00
parent 4ebf7e25b7
commit 6d664f2086
3 changed files with 26 additions and 18 deletions

View File

@@ -2,21 +2,21 @@ module.exports = {
isPlayable, isPlayable,
isVideo, isVideo,
isAudio, isAudio,
isPlayableTorrent, isTorrent,
isPlayableTorrentSummary,
pickFileToPlay pickFileToPlay
} }
var path = require('path') var path = require('path')
/** // Checks whether a fileSummary or file path is audio/video that we can play,
* Determines whether a file in a torrent is audio/video we can play // based on the file extension
*/
function isPlayable (file) { function isPlayable (file) {
return isVideo(file) || isAudio(file) return isVideo(file) || isAudio(file)
} }
// Checks whether a fileSummary or file path is playable video
function isVideo (file) { function isVideo (file) {
var ext = path.extname(file.name).toLowerCase()
return [ return [
'.avi', '.avi',
'.m4v', '.m4v',
@@ -27,21 +27,36 @@ function isVideo (file) {
'.ogv', '.ogv',
'.webm', '.webm',
'.wmv' '.wmv'
].includes(ext) ].includes(getFileExtension(file))
} }
// Checks whether a fileSummary or file path is playable audio
function isAudio (file) { function isAudio (file) {
var ext = path.extname(file.name).toLowerCase()
return [ return [
'.aac', '.aac',
'.ac3', '.ac3',
'.mp3', '.mp3',
'.ogg', '.ogg',
'.wav' '.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) return torrentSummary.files && torrentSummary.files.some(isPlayable)
} }

View File

@@ -326,13 +326,6 @@ function resumeTorrents () {
.forEach((torrentSummary) => controllers.torrentList.startTorrentingSummary(torrentSummary)) .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 // Gets a torrent summary {name, infoHash, status} from state.saved.torrents
// Returns undefined if we don't know that infoHash // Returns undefined if we don't know that infoHash
function getTorrentSummary (torrentKey) { function getTorrentSummary (torrentKey) {
@@ -561,7 +554,7 @@ function onOpen (files) {
var subtitles = files.filter(controllers.subtitles.isSubtitle) var subtitles = files.filter(controllers.subtitles.isSubtitle)
if (state.location.url() === 'home' || subtitles.length === 0) { if (state.location.url() === 'home' || subtitles.length === 0) {
if (files.every(isTorrent)) { if (files.every(TorrentPlayer.isTorrent)) {
if (state.location.url() !== 'home') { if (state.location.url() !== 'home') {
backToList() backToList()
} }

View File

@@ -148,7 +148,7 @@ function TorrentList (state) {
// Only show the play button for torrents that contain playable media // Only show the play button for torrents that contain playable media
var playButton var playButton
if (TorrentPlayer.isPlayableTorrent(torrentSummary)) { if (TorrentPlayer.isPlayableTorrentSummary(torrentSummary)) {
playButton = hx` playButton = hx`
<i.button-round.icon.play <i.button-round.icon.play
title=${playTooltip} title=${playTooltip}