Refactor main.js: playback and subtitles controllers
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user