Refactor main.js: playback and subtitles controllers

This commit is contained in:
DC
2016-07-01 17:11:46 -07:00
parent f85e0a61b1
commit 24f5856649
7 changed files with 497 additions and 427 deletions

View File

@@ -2,7 +2,8 @@ module.exports = {
isPlayable,
isVideo,
isAudio,
isPlayableTorrent
isPlayableTorrent,
pickFileToPlay
}
var path = require('path')
@@ -43,3 +44,25 @@ function isAudio (file) {
function isPlayableTorrent (torrentSummary) {
return torrentSummary.files && torrentSummary.files.some(isPlayable)
}
// Picks the default file to play from a list of torrent or torrentSummary files
// Returns an index or undefined, if no files are playable
function pickFileToPlay (files) {
// first, try to find the biggest video file
var videoFiles = files.filter(isVideo)
if (videoFiles.length > 0) {
var largestVideoFile = videoFiles.reduce(function (a, b) {
return a.length > b.length ? a : b
})
return files.indexOf(largestVideoFile)
}
// if there are no videos, play the first audio file
var audioFiles = files.filter(isAudio)
if (audioFiles.length > 0) {
return files.indexOf(audioFiles[0])
}
// no video or audio means nothing is playable
return undefined
}