Refactor main.js: TorrentPlayer.isTorrent
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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}
|
||||
|
||||
Reference in New Issue
Block a user