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,
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)
}

View File

@@ -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()
}

View File

@@ -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`
<i.button-round.icon.play
title=${playTooltip}