Fix scan for audiobook directories in root dir

This commit is contained in:
Mark Cooper 2021-08-23 14:08:54 -05:00
parent f4cb5d101e
commit 73a786879e
11 changed files with 157 additions and 11 deletions

View file

@ -2,11 +2,12 @@ const express = require('express')
const Logger = require('./Logger')
class ApiController {
constructor(db, scanner, auth, streamManager, emitter) {
constructor(db, scanner, auth, streamManager, rssFeeds, emitter) {
this.db = db
this.scanner = scanner
this.auth = auth
this.streamManager = streamManager
this.rssFeeds = rssFeeds
this.emitter = emitter
this.router = express()
@ -35,6 +36,8 @@ class ApiController {
this.router.post('/authorize', this.authorize.bind(this))
this.router.get('/genres', this.getGenres.bind(this))
this.router.post('/feed', this.openRssFeed.bind(this))
}
find(req, res) {
@ -42,7 +45,6 @@ class ApiController {
}
findCovers(req, res) {
console.log('Find covers', req.query)
this.scanner.findCovers(req, res)
}
@ -174,6 +176,15 @@ class ApiController {
this.auth.userChangePassword(req, res)
}
async openRssFeed(req, res) {
var audiobookId = req.body.audiobookId
var audiobook = this.db.audiobooks.find(ab => ab.id === audiobookId)
if (!audiobook) return res.sendStatus(404)
var feed = await this.rssFeeds.openFeed(audiobook)
console.log('Feed open', feed)
res.json(feed)
}
getGenres(req, res) {
res.json({
genres: this.db.getGenres()

View file

@ -75,6 +75,10 @@ class Auth {
verifyToken(token) {
return new Promise((resolve) => {
jwt.verify(token, process.env.TOKEN_SECRET, (err, payload) => {
if (!payload || err) {
Logger.error('JWT Verify Token Failed', err)
return resolve(null)
}
var user = this.users.find(u => u.id === payload.userId)
resolve(user || null)
})

53
server/RssFeeds.js Normal file
View file

@ -0,0 +1,53 @@
const Podcast = require('podcast')
const express = require('express')
const ip = require('ip')
const Logger = require('./Logger')
class RssFeeds {
constructor(Port, db) {
this.Port = Port
this.db = db
this.feeds = {}
this.router = express()
this.init()
}
init() {
this.router.get('/:id', this.getFeed.bind(this))
}
getFeed(req, res) {
var feed = this.feeds[req.params.id]
if (!feed) return null
var xml = feed.buildXml()
res.set('Content-Type', 'text/xml')
res.send(xml)
}
openFeed(audiobook) {
var serverAddress = 'http://' + ip.address('public', 'ipv4') + ':' + this.Port
Logger.info('Open RSS Feed', 'Server address', serverAddress)
var feedId = (Date.now() + Math.floor(Math.random() * 1000)).toString(36)
const feed = new Podcast({
title: audiobook.title,
description: 'AudioBookshelf RSS Feed',
feedUrl: `${serverAddress}/feeds/${feedId}`,
imageUrl: `${serverAddress}/Logo.png`,
author: 'advplyr',
language: 'en'
})
audiobook.tracks.forEach((track) => {
feed.addItem({
title: `Track ${track.index}`,
description: `AudioBookshelf Audiobook Track #${track.index}`,
url: `${serverAddress}/feeds/${feedId}?track=${track.index}`,
author: 'advplyr'
})
})
this.feeds[feedId] = feed
return feed
}
}
module.exports = RssFeeds

View file

@ -11,6 +11,7 @@ const Db = require('./Db')
const ApiController = require('./ApiController')
const HlsController = require('./HlsController')
const StreamManager = require('./StreamManager')
const RssFeeds = require('./RssFeeds')
const Logger = require('./Logger')
class Server {
@ -30,9 +31,11 @@ class Server {
this.watcher = new Watcher(this.AudiobookPath)
this.scanner = new Scanner(this.AudiobookPath, this.MetadataPath, this.db, this.emitter.bind(this))
this.streamManager = new StreamManager(this.db, this.MetadataPath)
this.apiController = new ApiController(this.db, this.scanner, this.auth, this.streamManager, this.emitter.bind(this))
this.rssFeeds = new RssFeeds(this.Port, this.db)
this.apiController = new ApiController(this.db, this.scanner, this.auth, this.streamManager, this.rssFeeds, this.emitter.bind(this))
this.hlsController = new HlsController(this.db, this.scanner, this.auth, this.streamManager, this.emitter.bind(this), this.MetadataPath)
this.server = null
this.io = null
@ -112,11 +115,13 @@ class Server {
}
app.use(express.static(this.MetadataPath))
app.use(express.static(Path.join(global.appRoot, 'static')))
app.use(express.urlencoded({ extended: true }));
app.use(express.json())
app.use('/api', this.authMiddleware.bind(this), this.apiController.router)
app.use('/hls', this.authMiddleware.bind(this), this.hlsController.router)
app.use('/feeds', this.rssFeeds.router)
app.get('/', (req, res) => {
res.sendFile('/index.html')

View file

@ -39,13 +39,15 @@ async function getAllAudiobookFiles(abRootPath) {
var pathformat = Path.parse(relpath)
var path = pathformat.dir
// If relative file directory has 3 folders, then the middle folder will be series
var splitDir = pathformat.dir.split(Path.sep)
if (splitDir.length === 1) {
Logger.error('Invalid file in root dir', filepath)
if (!path) {
Logger.error('Ignoring file in root dir', filepath)
return
}
var author = splitDir.shift()
// If relative file directory has 3 folders, then the middle folder will be series
var splitDir = pathformat.dir.split(Path.sep)
var author = null
if (splitDir.length > 1) author = splitDir.shift()
var series = null
if (splitDir.length > 1) series = splitDir.shift()
var title = splitDir.shift()