mirror of https://github.com/Kylart/KawAnime.git
Cleaned up torrent client routine on stream
This commit is contained in:
parent
558d54059d
commit
14da569a34
|
@ -68,7 +68,8 @@
|
||||||
autoplay: true,
|
autoplay: true,
|
||||||
isAss: false,
|
isAss: false,
|
||||||
styles: null,
|
styles: null,
|
||||||
info: null
|
info: null,
|
||||||
|
isMagnetRe: /^magnet:\?/
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
|
@ -77,15 +78,21 @@
|
||||||
return this.$store.state.config.config.video
|
return this.$store.state.config.config.video
|
||||||
},
|
},
|
||||||
set () {}
|
set () {}
|
||||||
|
},
|
||||||
|
isMagnet () {
|
||||||
|
return this.isMagnetRe.test(this.value)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
async created () {
|
||||||
|
if (this.isMagnet) await this.$axios.get('torrent/init')
|
||||||
|
},
|
||||||
mounted () {
|
mounted () {
|
||||||
const { video } = this.$refs
|
const { video } = this.$refs
|
||||||
const textTracks = {}
|
const textTracks = {}
|
||||||
|
|
||||||
video.addEventListener('loadedmetadata', () => {
|
video.addEventListener('loadedmetadata', () => {
|
||||||
// We need to get the subtitles only when the torrent is ready to be read. Otherwise,
|
// We need to get the subtitles only when the torrent is ready to be read.
|
||||||
// there is no file to get the subtitles from.
|
// Otherwise, there is no file to get the subtitles from.
|
||||||
this.eventSource = new window.EventSource(`/tracks/${this.value}`)
|
this.eventSource = new window.EventSource(`/tracks/${this.value}`)
|
||||||
|
|
||||||
this.eventSource.addEventListener('tracks', ({ data }) => {
|
this.eventSource.addEventListener('tracks', ({ data }) => {
|
||||||
|
@ -135,7 +142,7 @@
|
||||||
const { head } = document
|
const { head } = document
|
||||||
head.removeChild(head.children[head.childElementCount - 1])
|
head.removeChild(head.children[head.childElementCount - 1])
|
||||||
|
|
||||||
if (/^magnet:\?/.test(this.value)) {
|
if (this.isMagnet) {
|
||||||
this.$axios.delete('torrent/remove', {
|
this.$axios.delete('torrent/remove', {
|
||||||
params: {
|
params: {
|
||||||
magnet: this.value
|
magnet: this.value
|
||||||
|
|
|
@ -1,11 +1,10 @@
|
||||||
const {init, add, remove, events} = require('./torrent.js')
|
const {init, add, remove, infoClient} = require('./torrent.js')
|
||||||
const sseExpress = require('sse-express')
|
|
||||||
|
|
||||||
const routes = [
|
const routes = [
|
||||||
(app) => app.get('/torrent/init', init),
|
(app) => app.get('/torrent/init', init),
|
||||||
(app) => app.get('/torrent/add', add),
|
(app) => app.get('/torrent/add', add),
|
||||||
(app) => app.delete('/torrent/remove', remove),
|
(app) => app.delete('/torrent/remove', remove),
|
||||||
(app) => app.all(/torrent\/listen(.*)/, sseExpress, events)
|
(app) => app.all('torrent/client/info', infoClient)
|
||||||
]
|
]
|
||||||
|
|
||||||
module.exports = routes
|
module.exports = routes
|
||||||
|
|
|
@ -4,7 +4,14 @@ const logger = new Logger('Torrent Client')
|
||||||
|
|
||||||
// TODO Limit download speed, check https://github.com/webtorrent/webtorrent/issues/163
|
// 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) => {
|
const init = (req, res) => {
|
||||||
|
if (isClientExisting()) {
|
||||||
process.torrentClient = new WebTorrent()
|
process.torrentClient = new WebTorrent()
|
||||||
logger.info('Instanciated torrent client.')
|
logger.info('Instanciated torrent client.')
|
||||||
|
|
||||||
|
@ -16,12 +23,15 @@ const init = (req, res) => {
|
||||||
process.torrentClient.on('error', (err) => {
|
process.torrentClient.on('error', (err) => {
|
||||||
logger.error('Client encounered an error.', err)
|
logger.error('Client encounered an error.', err)
|
||||||
})
|
})
|
||||||
|
} else {
|
||||||
|
logger.info('Torrent client already instanciated.')
|
||||||
|
}
|
||||||
|
|
||||||
res.send()
|
res.send()
|
||||||
}
|
}
|
||||||
|
|
||||||
const add = ({query: {magnet}}, res) => {
|
const add = ({query: {magnet}}, res) => {
|
||||||
if (!process.torrentClient) {
|
if (isClientExisting()) {
|
||||||
init(null, res)
|
init(null, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,6 +43,8 @@ const add = ({query: {magnet}}, res) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const remove = ({query: {magnet}}, res) => {
|
const remove = ({query: {magnet}}, res) => {
|
||||||
|
// Be careful calling this one.
|
||||||
|
|
||||||
process.torrentClient.remove(magnet, (err) => {
|
process.torrentClient.remove(magnet, (err) => {
|
||||||
err
|
err
|
||||||
? logger.error(`Error while removing ${magnet}`, err)
|
? logger.error(`Error while removing ${magnet}`, err)
|
||||||
|
@ -50,13 +62,19 @@ const remove = ({query: {magnet}}, res) => {
|
||||||
res.send()
|
res.send()
|
||||||
}
|
}
|
||||||
|
|
||||||
const events = (req, res) => {
|
const infoClient = (req, res) => {
|
||||||
|
const result = {}
|
||||||
|
|
||||||
|
if (isClientExisting()) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
res.send(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
init,
|
init,
|
||||||
add,
|
add,
|
||||||
remove,
|
remove,
|
||||||
events
|
infoClient
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const WebTorrent = require('webtorrent')
|
|
||||||
const parseRange = require('range-parser')
|
const parseRange = require('range-parser')
|
||||||
const mime = require('mime')
|
const mime = require('mime')
|
||||||
const {Logger} = require('../utils')
|
const {Logger} = require('../utils')
|
||||||
|
@ -8,11 +7,6 @@ const decode = require('urldecode')
|
||||||
const MatroskaSubtitles = require('matroska-subtitles')
|
const MatroskaSubtitles = require('matroska-subtitles')
|
||||||
|
|
||||||
const stream = (req, res) => {
|
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 info = decode(req.url.slice('/stream/'.length))
|
||||||
const isMagnet = /^magnet:\?/.test(info)
|
const isMagnet = /^magnet:\?/.test(info)
|
||||||
const type = isMagnet ? 'magnet' : 'file'
|
const type = isMagnet ? 'magnet' : 'file'
|
||||||
|
@ -63,10 +57,7 @@ const stream = (req, res) => {
|
||||||
if (stream) {
|
if (stream) {
|
||||||
logger.info(`Closing stream of range: ${JSON.stringify(range)} for ${type}: ${isMagnet ? magnet : path}`)
|
logger.info(`Closing stream of range: ${JSON.stringify(range)} for ${type}: ${isMagnet ? magnet : path}`)
|
||||||
stream.destroy()
|
stream.destroy()
|
||||||
torrent && torrent.deselect && torrent.deselect(range)
|
|
||||||
stream = null
|
stream = null
|
||||||
|
|
||||||
process.torrent = null
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -126,7 +117,6 @@ const tracks = (req, res) => {
|
||||||
if (stream) {
|
if (stream) {
|
||||||
logger.info(`Closing stream for ${type} tracks: ${isMagnet ? magnet : path}`)
|
logger.info(`Closing stream for ${type} tracks: ${isMagnet ? magnet : path}`)
|
||||||
stream.destroy()
|
stream.destroy()
|
||||||
torrent && torrent.deselect && torrent.deselect()
|
|
||||||
stream = null
|
stream = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue