Add socket event to remove download, fix clearInterval on stream loop

This commit is contained in:
advplyr 2021-09-12 16:10:12 -05:00
parent c30955f909
commit 9107620b3c
5 changed files with 26 additions and 3 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "audiobookshelf-client", "name": "audiobookshelf-client",
"version": "1.1.4", "version": "1.1.5",
"description": "Audiobook manager and player", "description": "Audiobook manager and player",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {

View File

@ -1,6 +1,6 @@
{ {
"name": "audiobookshelf", "name": "audiobookshelf",
"version": "1.1.4", "version": "1.1.5",
"description": "Self-hosted audiobook server for managing and playing audiobooks.", "description": "Self-hosted audiobook server for managing and playing audiobooks.",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {

View File

@ -49,6 +49,15 @@ class DownloadManager {
this.prepareDownload(client, audiobook, options) this.prepareDownload(client, audiobook, options)
} }
removeSocketRequest(socket, downloadId) {
var download = this.downloads.find(d => d.id === downloadId)
if (!download) {
Logger.error('Remove download request download not found ' + downloadId)
return
}
this.removeDownload(download)
}
async prepareDownload(client, audiobook, options = {}) { async prepareDownload(client, audiobook, options = {}) {
var downloadId = (Math.trunc(Math.random() * 1000) + Date.now()).toString(36) var downloadId = (Math.trunc(Math.random() * 1000) + Date.now()).toString(36)
var dlpath = Path.join(this.downloadDirPath, downloadId) var dlpath = Path.join(this.downloadDirPath, downloadId)

View File

@ -199,14 +199,23 @@ class Server {
Logger.info('[SOCKET] Socket Connected', socket.id) Logger.info('[SOCKET] Socket Connected', socket.id)
socket.on('auth', (token) => this.authenticateSocket(socket, token)) socket.on('auth', (token) => this.authenticateSocket(socket, token))
// Scanning
socket.on('scan', this.scan.bind(this)) socket.on('scan', this.scan.bind(this))
socket.on('scan_covers', this.scanCovers.bind(this)) socket.on('scan_covers', this.scanCovers.bind(this))
socket.on('cancel_scan', this.cancelScan.bind(this)) socket.on('cancel_scan', this.cancelScan.bind(this))
// Streaming
socket.on('open_stream', (audiobookId) => this.streamManager.openStreamSocketRequest(socket, audiobookId)) socket.on('open_stream', (audiobookId) => this.streamManager.openStreamSocketRequest(socket, audiobookId))
socket.on('close_stream', () => this.streamManager.closeStreamRequest(socket)) socket.on('close_stream', () => this.streamManager.closeStreamRequest(socket))
socket.on('stream_update', (payload) => this.streamManager.streamUpdate(socket, payload)) socket.on('stream_update', (payload) => this.streamManager.streamUpdate(socket, payload))
socket.on('progress_update', (payload) => this.audiobookProgressUpdate(socket.sheepClient, payload)) socket.on('progress_update', (payload) => this.audiobookProgressUpdate(socket.sheepClient, payload))
// Downloading
socket.on('download', (payload) => this.downloadManager.downloadSocketRequest(socket, payload)) socket.on('download', (payload) => this.downloadManager.downloadSocketRequest(socket, payload))
socket.on('remove_download', (downloadId) => this.downloadManager.removeSocketRequest(socket, downloadId))
socket.on('test', () => { socket.on('test', () => {
socket.emit('test_received', socket.id) socket.emit('test_received', socket.id)
}) })

View File

@ -43,6 +43,10 @@ class Stream extends EventEmitter {
return this.audiobook.id return this.audiobook.id
} }
get audiobookTitle() {
return this.audiobook ? this.audiobook.title : null
}
get totalDuration() { get totalDuration() {
return this.audiobook.totalDuration return this.audiobook.totalDuration
} }
@ -206,8 +210,9 @@ class Stream extends EventEmitter {
if (!this.isTranscodeComplete) { if (!this.isTranscodeComplete) {
this.checkFiles() this.checkFiles()
} else { } else {
Logger.info(`[Stream] ${this.audiobookTitle} sending stream_ready`)
this.socket.emit('stream_ready') this.socket.emit('stream_ready')
clearTimeout(this.loop) clearInterval(this.loop)
} }
}, 2000) }, 2000)
} }