Cleaned up torrent client routine on stream

This commit is contained in:
Kylart 2018-04-24 09:31:33 +02:00
parent 558d54059d
commit 14da569a34
4 changed files with 43 additions and 29 deletions

View File

@ -68,7 +68,8 @@
autoplay: true,
isAss: false,
styles: null,
info: null
info: null,
isMagnetRe: /^magnet:\?/
}
},
computed: {
@ -77,15 +78,21 @@
return this.$store.state.config.config.video
},
set () {}
},
isMagnet () {
return this.isMagnetRe.test(this.value)
}
},
async created () {
if (this.isMagnet) await this.$axios.get('torrent/init')
},
mounted () {
const { video } = this.$refs
const textTracks = {}
video.addEventListener('loadedmetadata', () => {
// We need to get the subtitles only when the torrent is ready to be read. Otherwise,
// there is no file to get the subtitles from.
// We need to get the subtitles only when the torrent is ready to be read.
// Otherwise, there is no file to get the subtitles from.
this.eventSource = new window.EventSource(`/tracks/${this.value}`)
this.eventSource.addEventListener('tracks', ({ data }) => {
@ -135,7 +142,7 @@
const { head } = document
head.removeChild(head.children[head.childElementCount - 1])
if (/^magnet:\?/.test(this.value)) {
if (this.isMagnet) {
this.$axios.delete('torrent/remove', {
params: {
magnet: this.value

View File

@ -1,11 +1,10 @@
const {init, add, remove, events} = require('./torrent.js')
const sseExpress = require('sse-express')
const {init, add, remove, infoClient} = require('./torrent.js')
const routes = [
(app) => app.get('/torrent/init', init),
(app) => app.get('/torrent/add', add),
(app) => app.delete('/torrent/remove', remove),
(app) => app.all(/torrent\/listen(.*)/, sseExpress, events)
(app) => app.all('torrent/client/info', infoClient)
]
module.exports = routes

View File

@ -4,24 +4,34 @@ const logger = new Logger('Torrent Client')
// TODO Limit download speed, check https://github.com/webtorrent/webtorrent/issues/163
const isClientExisting = () => {
const client = process.torrentClient
return !client || (client && client.destroyed)
}
const init = (req, res) => {
process.torrentClient = new WebTorrent()
logger.info('Instanciated torrent client.')
if (isClientExisting()) {
process.torrentClient = new WebTorrent()
logger.info('Instanciated torrent client.')
// Setting up all listeners
process.torrentClient.on('torrent', (torrent) => {
logger.info(`${torrent.infoHash} is ready to be used.`)
})
// Setting up all listeners
process.torrentClient.on('torrent', (torrent) => {
logger.info(`${torrent.infoHash} is ready to be used.`)
})
process.torrentClient.on('error', (err) => {
logger.error('Client encounered an error.', err)
})
process.torrentClient.on('error', (err) => {
logger.error('Client encounered an error.', err)
})
} else {
logger.info('Torrent client already instanciated.')
}
res.send()
}
const add = ({query: {magnet}}, res) => {
if (!process.torrentClient) {
if (isClientExisting()) {
init(null, res)
}
@ -33,6 +43,8 @@ const add = ({query: {magnet}}, res) => {
}
const remove = ({query: {magnet}}, res) => {
// Be careful calling this one.
process.torrentClient.remove(magnet, (err) => {
err
? logger.error(`Error while removing ${magnet}`, err)
@ -50,13 +62,19 @@ const remove = ({query: {magnet}}, res) => {
res.send()
}
const events = (req, res) => {
const infoClient = (req, res) => {
const result = {}
if (isClientExisting()) {
}
res.send(result)
}
module.exports = {
init,
add,
remove,
events
infoClient
}

View File

@ -1,5 +1,4 @@
const fs = require('fs')
const WebTorrent = require('webtorrent')
const parseRange = require('range-parser')
const mime = require('mime')
const {Logger} = require('../utils')
@ -8,11 +7,6 @@ const decode = require('urldecode')
const MatroskaSubtitles = require('matroska-subtitles')
const stream = (req, res) => {
const client = process.torrentClient
if (!client || (client && client.destroyed)) {
process.torrentClient = new WebTorrent()
}
const info = decode(req.url.slice('/stream/'.length))
const isMagnet = /^magnet:\?/.test(info)
const type = isMagnet ? 'magnet' : 'file'
@ -63,10 +57,7 @@ const stream = (req, res) => {
if (stream) {
logger.info(`Closing stream of range: ${JSON.stringify(range)} for ${type}: ${isMagnet ? magnet : path}`)
stream.destroy()
torrent && torrent.deselect && torrent.deselect(range)
stream = null
process.torrent = null
}
}
@ -126,7 +117,6 @@ const tracks = (req, res) => {
if (stream) {
logger.info(`Closing stream for ${type} tracks: ${isMagnet ? magnet : path}`)
stream.destroy()
torrent && torrent.deselect && torrent.deselect()
stream = null
}
}