2021-08-18 00:01:11 +02:00
|
|
|
const Path = require('path')
|
2023-08-06 21:18:51 +02:00
|
|
|
const Sequelize = require('sequelize')
|
2021-08-18 00:01:11 +02:00
|
|
|
const express = require('express')
|
|
|
|
const http = require('http')
|
2024-02-15 23:46:19 +01:00
|
|
|
const util = require('util')
|
2022-07-06 02:53:01 +02:00
|
|
|
const fs = require('./libs/fsExtra')
|
2022-07-07 02:10:25 +02:00
|
|
|
const fileUpload = require('./libs/expressFileupload')
|
2023-11-22 18:00:11 +01:00
|
|
|
const cookieParser = require("cookie-parser")
|
2021-08-18 00:01:11 +02:00
|
|
|
|
2021-10-05 05:11:42 +02:00
|
|
|
const { version } = require('../package.json')
|
|
|
|
|
|
|
|
// Utils
|
2023-01-06 00:45:27 +01:00
|
|
|
const fileUtils = require('./utils/fileUtils')
|
2021-10-05 05:11:42 +02:00
|
|
|
const Logger = require('./Logger')
|
2021-10-01 01:52:32 +02:00
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
const Auth = require('./Auth')
|
|
|
|
const Watcher = require('./Watcher')
|
2023-07-05 01:14:44 +02:00
|
|
|
const Database = require('./Database')
|
2022-11-24 22:53:58 +01:00
|
|
|
const SocketAuthority = require('./SocketAuthority')
|
2022-03-20 22:41:06 +01:00
|
|
|
|
2022-03-18 01:10:47 +01:00
|
|
|
const ApiRouter = require('./routers/ApiRouter')
|
|
|
|
const HlsRouter = require('./routers/HlsRouter')
|
2022-03-20 22:41:06 +01:00
|
|
|
|
2024-02-15 23:46:19 +01:00
|
|
|
const LogManager = require('./managers/LogManager')
|
2022-09-21 01:08:41 +02:00
|
|
|
const NotificationManager = require('./managers/NotificationManager')
|
2023-05-30 00:38:38 +02:00
|
|
|
const EmailManager = require('./managers/EmailManager')
|
2022-04-22 01:52:28 +02:00
|
|
|
const AbMergeManager = require('./managers/AbMergeManager')
|
2022-03-20 22:41:06 +01:00
|
|
|
const CacheManager = require('./managers/CacheManager')
|
2023-07-08 21:40:49 +02:00
|
|
|
const BackupManager = require('./managers/BackupManager')
|
2022-03-20 22:41:06 +01:00
|
|
|
const PlaybackSessionManager = require('./managers/PlaybackSessionManager')
|
|
|
|
const PodcastManager = require('./managers/PodcastManager')
|
2022-05-02 01:33:46 +02:00
|
|
|
const AudioMetadataMangaer = require('./managers/AudioMetadataManager')
|
2022-05-02 21:41:59 +02:00
|
|
|
const RssFeedManager = require('./managers/RssFeedManager')
|
2022-08-18 01:44:21 +02:00
|
|
|
const CronManager = require('./managers/CronManager')
|
2023-11-17 07:49:40 +01:00
|
|
|
const ApiCacheManager = require('./managers/ApiCacheManager')
|
2023-12-05 20:19:17 +01:00
|
|
|
const BinaryManager = require('./managers/BinaryManager')
|
2023-09-04 18:50:55 +02:00
|
|
|
const LibraryScanner = require('./scanner/LibraryScanner')
|
2021-10-05 05:11:42 +02:00
|
|
|
|
2023-11-22 18:00:11 +01:00
|
|
|
//Import the main Passport and Express-Session library
|
|
|
|
const passport = require('passport')
|
|
|
|
const expressSession = require('express-session')
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
class Server {
|
2024-03-11 17:11:13 +01:00
|
|
|
constructor(SOURCE, PORT, HOST, CONFIG_PATH, METADATA_PATH, ROUTER_BASE_PATH) {
|
2021-08-18 00:01:11 +02:00
|
|
|
this.Port = PORT
|
2022-03-17 12:06:52 +01:00
|
|
|
this.Host = HOST
|
2022-05-21 18:21:03 +02:00
|
|
|
global.Source = SOURCE
|
2023-01-05 23:44:34 +01:00
|
|
|
global.isWin = process.platform === 'win32'
|
2023-01-06 00:45:27 +01:00
|
|
|
global.ConfigPath = fileUtils.filePathToPOSIX(Path.normalize(CONFIG_PATH))
|
|
|
|
global.MetadataPath = fileUtils.filePathToPOSIX(Path.normalize(METADATA_PATH))
|
2022-10-01 23:07:30 +02:00
|
|
|
global.RouterBasePath = ROUTER_BASE_PATH
|
Implement X-Accel Redirect
This patch implements [X-Accel](https://www.nginx.com/resources/wiki/start/topics/examples/x-accel/)
redirect headers as an optional way for offloading static file delivery
from Express to Nginx, which is far better optimized for static file
delivery.
This provides a really easy to configure way for getting a huge
performance boost over delivering all files through Audiobookshelf.
How it works
------------
The way this works is basically that Audiobookshelf gets an HTTP request
for delivering a static file (let's say an audiobook). It will first
check the user is authorized and then convert the API path to a local
file path.
Now, instead of reading and delivering the file, Audiobookshelf will
return just the HTTP header with an additional `X-Accel-Redirect`
pointing to the file location on the file syste.
This header is picked up by Nginx which will then deliver the file.
Configuration
-------------
The configuration for this is very simple. You need to run Nginx as
reverse proxy and it must have access to your Audiobookshelf data
folder.
You then configure Audiobookshelf to use X-Accel by setting
`USE_X_ACCEL=/protected`. The path is the internal redirect path used by
Nginx.
In the Nginx configuration you then configure this location and map it
to the storage area to serve like this:
```
location /protected/ {
internal;
alias /;
}
```
That's all.
Impact
------
I just did a very simple performance test, downloading a 1170620819
bytes large audiobook file from another machine on the same network
like this, using `time -p` to measure how log the process took:
```sh
URL='https://url to audiobook…'
for i in `seq 1 50`
do
echo "$i"
curl -s -o /dev/null "${URL}"
done
```
This sequential test with 50 iterations and without x-accel resulted in:
```
real 413.42
user 197.11
sys 82.04
```
That is an average download speed of about 1080 MBit/s.
With X-Accel enabled, serving the files through Nginx, the same test
yielded the following results:
```
real 200.37
user 86.95
sys 29.79
```
That is an average download speed of about 2229 MBit/s, more than
doubling the previous speed.
I have also run the same test with 4 parallel processes and 25 downloads
each. Without x-accel, that test resulted in:
```
real 364.89
user 273.09
sys 112.75
```
That is an average speed of about 2448 MBit/s.
With X-Accel enabled, the parallel test also shows a significant
speedup:
```
real 167.19
user 195.62
sys 78.61
```
That is an average speed of about 5342 MBit/s.
While doing that, I also peaked at the system load which was a bit lower
when using X-Accel. Even though the system was delivering far more data.
But I just looked at the `load1` values and did not build a proper test
for that. That means, I cant provide any definitive data.
Supported Media
---------------
The current implementation works for audio files and book covers. There
are other media files which would benefit from this mechanism like feed
covers or author pictures.
But that's something for a future developer ;-)
2022-11-25 23:41:35 +01:00
|
|
|
global.XAccel = process.env.USE_X_ACCEL
|
2022-05-15 00:23:22 +02:00
|
|
|
|
2022-04-25 02:12:00 +02:00
|
|
|
if (!fs.pathExistsSync(global.ConfigPath)) {
|
|
|
|
fs.mkdirSync(global.ConfigPath)
|
|
|
|
}
|
|
|
|
if (!fs.pathExistsSync(global.MetadataPath)) {
|
|
|
|
fs.mkdirSync(global.MetadataPath)
|
|
|
|
}
|
2021-08-18 00:01:11 +02:00
|
|
|
|
2022-03-20 22:41:06 +01:00
|
|
|
this.watcher = new Watcher()
|
2023-07-05 01:14:44 +02:00
|
|
|
this.auth = new Auth()
|
2022-03-20 22:41:06 +01:00
|
|
|
|
|
|
|
// Managers
|
2023-07-05 01:14:44 +02:00
|
|
|
this.notificationManager = new NotificationManager()
|
|
|
|
this.emailManager = new EmailManager()
|
2023-07-08 21:40:49 +02:00
|
|
|
this.backupManager = new BackupManager()
|
2023-10-20 23:39:32 +02:00
|
|
|
this.abMergeManager = new AbMergeManager()
|
2023-07-05 01:14:44 +02:00
|
|
|
this.playbackSessionManager = new PlaybackSessionManager()
|
2023-10-20 23:39:32 +02:00
|
|
|
this.podcastManager = new PodcastManager(this.watcher, this.notificationManager)
|
|
|
|
this.audioMetadataManager = new AudioMetadataMangaer()
|
2023-07-05 01:14:44 +02:00
|
|
|
this.rssFeedManager = new RssFeedManager()
|
2023-09-04 18:50:55 +02:00
|
|
|
this.cronManager = new CronManager(this.podcastManager)
|
2023-11-17 07:49:40 +01:00
|
|
|
this.apiCacheManager = new ApiCacheManager()
|
2023-12-05 20:19:17 +01:00
|
|
|
this.binaryManager = new BinaryManager()
|
2022-03-18 01:10:47 +01:00
|
|
|
|
|
|
|
// Routers
|
2022-11-24 22:53:58 +01:00
|
|
|
this.apiRouter = new ApiRouter(this)
|
2023-07-05 01:14:44 +02:00
|
|
|
this.hlsRouter = new HlsRouter(this.auth, this.playbackSessionManager)
|
2021-10-02 01:42:48 +02:00
|
|
|
|
2024-02-15 23:46:19 +01:00
|
|
|
Logger.logManager = new LogManager()
|
2021-10-31 23:55:28 +01:00
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
this.server = null
|
|
|
|
this.io = null
|
2021-09-06 01:20:29 +02:00
|
|
|
}
|
|
|
|
|
2021-10-13 03:07:42 +02:00
|
|
|
authMiddleware(req, res, next) {
|
2023-11-22 18:00:11 +01:00
|
|
|
// ask passportjs if the current request is authenticated
|
|
|
|
this.auth.isAuthenticated(req, res, next)
|
2021-10-13 03:07:42 +02:00
|
|
|
}
|
|
|
|
|
2023-09-04 18:50:55 +02:00
|
|
|
cancelLibraryScan(libraryId) {
|
|
|
|
LibraryScanner.setCancelLibraryScan(libraryId)
|
|
|
|
}
|
|
|
|
|
2023-07-22 21:25:20 +02:00
|
|
|
/**
|
|
|
|
* Initialize database, backups, logs, rss feeds, cron jobs & watcher
|
|
|
|
* Cleanup stale/invalid data
|
|
|
|
*/
|
2021-08-18 00:01:11 +02:00
|
|
|
async init() {
|
2021-10-05 05:11:42 +02:00
|
|
|
Logger.info('[Server] Init v' + version)
|
2024-02-15 23:46:19 +01:00
|
|
|
|
2022-04-16 19:37:10 +02:00
|
|
|
await this.playbackSessionManager.removeOrphanStreams()
|
2021-09-23 03:40:35 +02:00
|
|
|
|
2023-07-05 01:14:44 +02:00
|
|
|
await Database.init(false)
|
2022-03-10 02:23:17 +01:00
|
|
|
|
2024-02-15 23:46:19 +01:00
|
|
|
await Logger.logManager.init()
|
|
|
|
|
2022-07-19 00:19:16 +02:00
|
|
|
// Create token secret if does not exist (Added v2.1.0)
|
2023-07-05 01:14:44 +02:00
|
|
|
if (!Database.serverSettings.tokenSecret) {
|
2022-07-19 00:19:16 +02:00
|
|
|
await this.auth.initTokenSecret()
|
|
|
|
}
|
|
|
|
|
2022-09-29 00:12:27 +02:00
|
|
|
await this.cleanUserData() // Remove invalid user item progress
|
2023-09-07 00:48:50 +02:00
|
|
|
await CacheManager.ensureCachePaths()
|
2022-06-18 20:11:15 +02:00
|
|
|
|
2023-07-08 21:40:49 +02:00
|
|
|
await this.backupManager.init()
|
2022-06-08 01:29:43 +02:00
|
|
|
await this.rssFeedManager.init()
|
2023-07-22 21:25:20 +02:00
|
|
|
|
2023-08-20 20:34:03 +02:00
|
|
|
const libraries = await Database.libraryModel.getAllOldLibraries()
|
2023-08-12 22:52:09 +02:00
|
|
|
await this.cronManager.init(libraries)
|
2023-11-17 07:49:40 +01:00
|
|
|
this.apiCacheManager.init()
|
2024-01-02 21:24:59 +01:00
|
|
|
|
|
|
|
// Download ffmpeg & ffprobe if not found (Currently only in use for Windows installs)
|
|
|
|
if (global.isWin || Logger.isDev) {
|
|
|
|
await this.binaryManager.init()
|
|
|
|
}
|
2021-10-02 01:42:48 +02:00
|
|
|
|
2023-07-05 01:14:44 +02:00
|
|
|
if (Database.serverSettings.scannerDisableWatcher) {
|
2022-02-24 00:52:21 +01:00
|
|
|
Logger.info(`[Server] Watcher is disabled`)
|
|
|
|
this.watcher.disabled = true
|
|
|
|
} else {
|
2023-07-22 21:25:20 +02:00
|
|
|
this.watcher.initWatcher(libraries)
|
2022-02-24 00:52:21 +01:00
|
|
|
}
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
2024-02-15 23:46:19 +01:00
|
|
|
/**
|
|
|
|
* Listen for SIGINT and uncaught exceptions
|
|
|
|
*/
|
|
|
|
initProcessEventListeners() {
|
|
|
|
let sigintAlreadyReceived = false
|
|
|
|
process.on('SIGINT', async () => {
|
|
|
|
if (!sigintAlreadyReceived) {
|
|
|
|
sigintAlreadyReceived = true
|
|
|
|
Logger.info('SIGINT (Ctrl+C) received. Shutting down...')
|
|
|
|
await this.stop()
|
|
|
|
Logger.info('Server stopped. Exiting.')
|
|
|
|
} else {
|
|
|
|
Logger.info('SIGINT (Ctrl+C) received again. Exiting immediately.')
|
|
|
|
}
|
|
|
|
process.exit(0)
|
|
|
|
})
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see https://nodejs.org/api/process.html#event-uncaughtexceptionmonitor
|
|
|
|
*/
|
|
|
|
process.on('uncaughtExceptionMonitor', async (error, origin) => {
|
|
|
|
await Logger.fatal(`[Server] Uncaught exception origin: ${origin}, error:`, util.format('%O', error))
|
|
|
|
})
|
|
|
|
/**
|
|
|
|
* @see https://nodejs.org/api/process.html#event-unhandledrejection
|
|
|
|
*/
|
|
|
|
process.on('unhandledRejection', async (reason, promise) => {
|
|
|
|
await Logger.fatal(`[Server] Unhandled rejection: ${reason}, promise:`, util.format('%O', promise))
|
|
|
|
process.exit(1)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
async start() {
|
|
|
|
Logger.info('=== Starting Server ===')
|
2024-02-15 23:46:19 +01:00
|
|
|
this.initProcessEventListeners()
|
2021-08-18 00:01:11 +02:00
|
|
|
await this.init()
|
|
|
|
|
|
|
|
const app = express()
|
2023-11-22 18:00:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @temporary
|
2023-12-30 00:05:35 +01:00
|
|
|
* This is necessary for the ebook & cover API endpoint in the mobile apps
|
2023-11-22 18:00:11 +01:00
|
|
|
* The mobile app ereader is using fetch api in Capacitor that is currently difficult to switch to native requests
|
|
|
|
* so we have to allow cors for specific origins to the /api/items/:id/ebook endpoint
|
2023-12-30 00:05:35 +01:00
|
|
|
* The cover image is fetched with XMLHttpRequest in the mobile apps to load into a canvas and extract colors
|
2023-11-22 18:00:11 +01:00
|
|
|
* @see https://ionicframework.com/docs/troubleshooting/cors
|
2023-11-22 19:37:18 +01:00
|
|
|
*
|
|
|
|
* Running in development allows cors to allow testing the mobile apps in the browser
|
2023-11-22 18:00:11 +01:00
|
|
|
*/
|
|
|
|
app.use((req, res, next) => {
|
2023-12-30 00:05:35 +01:00
|
|
|
if (Logger.isDev || req.path.match(/\/api\/items\/([a-z0-9-]{36})\/(ebook|cover)(\/[0-9]+)?/)) {
|
2023-11-22 18:00:11 +01:00
|
|
|
const allowedOrigins = ['capacitor://localhost', 'http://localhost']
|
2023-11-22 19:37:18 +01:00
|
|
|
if (Logger.isDev || allowedOrigins.some(o => o === req.get('origin'))) {
|
2023-11-22 18:00:11 +01:00
|
|
|
res.header('Access-Control-Allow-Origin', req.get('origin'))
|
|
|
|
res.header("Access-Control-Allow-Methods", 'GET, POST, PATCH, PUT, DELETE, OPTIONS')
|
|
|
|
res.header('Access-Control-Allow-Headers', '*')
|
|
|
|
res.header('Access-Control-Allow-Credentials', true)
|
|
|
|
if (req.method === 'OPTIONS') {
|
|
|
|
return res.sendStatus(200)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
next()
|
|
|
|
})
|
|
|
|
|
|
|
|
// parse cookies in requests
|
|
|
|
app.use(cookieParser())
|
|
|
|
// enable express-session
|
|
|
|
app.use(expressSession({
|
|
|
|
secret: global.ServerSettings.tokenSecret,
|
|
|
|
resave: false,
|
|
|
|
saveUninitialized: false,
|
|
|
|
cookie: {
|
|
|
|
// also send the cookie if were are not on https (not every use has https)
|
|
|
|
secure: false
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
// init passport.js
|
|
|
|
app.use(passport.initialize())
|
|
|
|
// register passport in express-session
|
|
|
|
app.use(passport.session())
|
|
|
|
// config passport.js
|
|
|
|
await this.auth.initPassportJs()
|
|
|
|
|
2022-10-01 23:07:30 +02:00
|
|
|
const router = express.Router()
|
|
|
|
app.use(global.RouterBasePath, router)
|
2023-01-21 23:18:06 +01:00
|
|
|
app.disable('x-powered-by')
|
2022-10-01 23:07:30 +02:00
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
this.server = http.createServer(app)
|
|
|
|
|
2023-06-04 06:44:13 +02:00
|
|
|
router.use(fileUpload({
|
|
|
|
defCharset: 'utf8',
|
2023-06-05 00:56:41 +02:00
|
|
|
defParamCharset: 'utf8',
|
|
|
|
useTempFiles: true,
|
2023-06-06 22:40:52 +02:00
|
|
|
tempFileDir: Path.join(global.MetadataPath, 'tmp')
|
2023-06-04 06:44:13 +02:00
|
|
|
}))
|
2023-11-22 18:00:11 +01:00
|
|
|
router.use(express.urlencoded({ extended: true, limit: "5mb" }))
|
2022-10-01 23:07:30 +02:00
|
|
|
router.use(express.json({ limit: "5mb" }))
|
2021-08-18 00:01:11 +02:00
|
|
|
|
|
|
|
// Static path to generated nuxt
|
2021-08-24 02:37:40 +02:00
|
|
|
const distPath = Path.join(global.appRoot, '/client/dist')
|
2022-10-01 23:07:30 +02:00
|
|
|
router.use(express.static(distPath))
|
2021-10-05 05:11:42 +02:00
|
|
|
|
|
|
|
// Static folder
|
2022-10-01 23:07:30 +02:00
|
|
|
router.use(express.static(Path.join(global.appRoot, 'static')))
|
2021-08-18 00:01:11 +02:00
|
|
|
|
2022-10-01 23:07:30 +02:00
|
|
|
router.use('/api', this.authMiddleware.bind(this), this.apiRouter.router)
|
|
|
|
router.use('/hls', this.authMiddleware.bind(this), this.hlsRouter.router)
|
2023-05-28 19:34:22 +02:00
|
|
|
|
2022-05-02 21:41:59 +02:00
|
|
|
// RSS Feed temp route
|
2023-07-07 00:07:10 +02:00
|
|
|
router.get('/feed/:slug', (req, res) => {
|
|
|
|
Logger.info(`[Server] Requesting rss feed ${req.params.slug}`)
|
2022-05-02 21:41:59 +02:00
|
|
|
this.rssFeedManager.getFeed(req, res)
|
|
|
|
})
|
2023-11-01 12:11:24 +01:00
|
|
|
router.get('/feed/:slug/cover*', (req, res) => {
|
2022-05-02 23:42:30 +02:00
|
|
|
this.rssFeedManager.getFeedCover(req, res)
|
|
|
|
})
|
2023-07-07 00:07:10 +02:00
|
|
|
router.get('/feed/:slug/item/:episodeId/*', (req, res) => {
|
|
|
|
Logger.debug(`[Server] Requesting rss feed episode ${req.params.slug}/${req.params.episodeId}`)
|
2022-05-02 21:41:59 +02:00
|
|
|
this.rssFeedManager.getFeedItem(req, res)
|
|
|
|
})
|
|
|
|
|
2023-11-22 18:00:11 +01:00
|
|
|
// Auth routes
|
|
|
|
await this.auth.initAuthRoutes(router)
|
|
|
|
|
2021-11-13 02:43:16 +01:00
|
|
|
// Client dynamic routes
|
2022-05-14 20:08:56 +02:00
|
|
|
const dyanimicRoutes = [
|
|
|
|
'/item/:id',
|
2022-06-01 23:29:29 +02:00
|
|
|
'/author/:id',
|
2022-05-29 19:55:14 +02:00
|
|
|
'/audiobook/:id/chapters',
|
2022-05-14 20:08:56 +02:00
|
|
|
'/audiobook/:id/edit',
|
2022-10-02 18:53:53 +02:00
|
|
|
'/audiobook/:id/manage',
|
2022-05-14 20:08:56 +02:00
|
|
|
'/library/:library',
|
|
|
|
'/library/:library/search',
|
|
|
|
'/library/:library/bookshelf/:id?',
|
|
|
|
'/library/:library/authors',
|
2023-11-28 23:39:52 +01:00
|
|
|
'/library/:library/narrators',
|
2022-05-14 20:08:56 +02:00
|
|
|
'/library/:library/series/:id?',
|
2022-09-17 22:23:33 +02:00
|
|
|
'/library/:library/podcast/search',
|
|
|
|
'/library/:library/podcast/latest',
|
2023-09-12 22:35:14 +02:00
|
|
|
'/library/:library/podcast/download-queue',
|
2022-05-14 20:08:56 +02:00
|
|
|
'/config/users/:id',
|
2022-05-29 19:55:14 +02:00
|
|
|
'/config/users/:id/sessions',
|
2022-12-18 21:17:52 +01:00
|
|
|
'/config/item-metadata-utils/:id',
|
2022-11-27 21:23:28 +01:00
|
|
|
'/collection/:id',
|
|
|
|
'/playlist/:id'
|
2022-05-14 20:08:56 +02:00
|
|
|
]
|
2022-10-01 23:07:30 +02:00
|
|
|
dyanimicRoutes.forEach((route) => router.get(route, (req, res) => res.sendFile(Path.join(distPath, 'index.html'))))
|
2021-08-24 02:37:40 +02:00
|
|
|
|
2022-10-01 23:07:30 +02:00
|
|
|
router.post('/init', (req, res) => {
|
2023-07-05 01:14:44 +02:00
|
|
|
if (Database.hasRootUser) {
|
2022-05-15 00:23:22 +02:00
|
|
|
Logger.error(`[Server] attempt to init server when server already has a root user`)
|
|
|
|
return res.sendStatus(500)
|
|
|
|
}
|
|
|
|
this.initializeServer(req, res)
|
|
|
|
})
|
2022-10-01 23:07:30 +02:00
|
|
|
router.get('/status', (req, res) => {
|
2022-05-15 00:23:22 +02:00
|
|
|
// status check for client to see if server has been initialized
|
|
|
|
// server has been initialized if a root user exists
|
|
|
|
const payload = {
|
2023-11-22 18:00:11 +01:00
|
|
|
app: 'audiobookshelf',
|
|
|
|
serverVersion: version,
|
2023-07-05 01:14:44 +02:00
|
|
|
isInit: Database.hasRootUser,
|
2023-11-22 18:00:11 +01:00
|
|
|
language: Database.serverSettings.language,
|
|
|
|
authMethods: Database.serverSettings.authActiveAuthMethods,
|
|
|
|
authFormData: Database.serverSettings.authFormData
|
2022-05-15 00:23:22 +02:00
|
|
|
}
|
|
|
|
if (!payload.isInit) {
|
|
|
|
payload.ConfigPath = global.ConfigPath
|
|
|
|
payload.MetadataPath = global.MetadataPath
|
|
|
|
}
|
|
|
|
res.json(payload)
|
|
|
|
})
|
2022-10-01 23:07:30 +02:00
|
|
|
router.get('/ping', (req, res) => {
|
2022-07-30 15:37:35 +02:00
|
|
|
Logger.info('Received ping')
|
2021-08-18 00:01:11 +02:00
|
|
|
res.json({ success: true })
|
|
|
|
})
|
2022-07-24 22:46:19 +02:00
|
|
|
app.get('/healthcheck', (req, res) => res.sendStatus(200))
|
2021-08-18 00:01:11 +02:00
|
|
|
|
|
|
|
this.server.listen(this.Port, this.Host, () => {
|
2022-12-17 22:55:53 +01:00
|
|
|
if (this.Host) Logger.info(`Listening on http://${this.Host}:${this.Port}`)
|
|
|
|
else Logger.info(`Listening on port :${this.Port}`)
|
2021-08-18 00:01:11 +02:00
|
|
|
})
|
|
|
|
|
2022-11-24 22:53:58 +01:00
|
|
|
// Start listening for socket connections
|
|
|
|
SocketAuthority.initialize(this)
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
2022-05-15 00:23:22 +02:00
|
|
|
async initializeServer(req, res) {
|
|
|
|
Logger.info(`[Server] Initializing new server`)
|
|
|
|
const newRoot = req.body.newRoot
|
2023-07-09 18:39:15 +02:00
|
|
|
const rootUsername = newRoot.username || 'root'
|
|
|
|
const rootPash = newRoot.password ? await this.auth.hashPass(newRoot.password) : ''
|
2022-05-15 00:23:22 +02:00
|
|
|
if (!rootPash) Logger.warn(`[Server] Creating root user with no password`)
|
2023-07-09 18:39:15 +02:00
|
|
|
await Database.createRootUser(rootUsername, rootPash, this.auth)
|
2022-05-15 00:23:22 +02:00
|
|
|
|
|
|
|
res.sendStatus(200)
|
|
|
|
}
|
|
|
|
|
2023-08-06 21:18:51 +02:00
|
|
|
/**
|
|
|
|
* Remove user media progress for items that no longer exist & remove seriesHideFrom that no longer exist
|
|
|
|
*/
|
2022-09-29 00:12:27 +02:00
|
|
|
async cleanUserData() {
|
2023-08-06 21:18:51 +02:00
|
|
|
// Get all media progress without an associated media item
|
2023-08-20 20:34:03 +02:00
|
|
|
const mediaProgressToRemove = await Database.mediaProgressModel.findAll({
|
2023-08-06 21:18:51 +02:00
|
|
|
where: {
|
|
|
|
'$podcastEpisode.id$': null,
|
|
|
|
'$book.id$': null
|
|
|
|
},
|
|
|
|
attributes: ['id'],
|
|
|
|
include: [
|
|
|
|
{
|
2023-08-20 20:34:03 +02:00
|
|
|
model: Database.bookModel,
|
2023-08-06 21:18:51 +02:00
|
|
|
attributes: ['id']
|
|
|
|
},
|
|
|
|
{
|
2023-08-20 20:34:03 +02:00
|
|
|
model: Database.podcastEpisodeModel,
|
2023-08-06 21:18:51 +02:00
|
|
|
attributes: ['id']
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
|
|
|
if (mediaProgressToRemove.length) {
|
|
|
|
// Remove media progress
|
2023-08-20 20:34:03 +02:00
|
|
|
const mediaProgressRemoved = await Database.mediaProgressModel.destroy({
|
2023-08-06 21:18:51 +02:00
|
|
|
where: {
|
|
|
|
id: {
|
|
|
|
[Sequelize.Op.in]: mediaProgressToRemove.map(mp => mp.id)
|
2023-07-05 01:14:44 +02:00
|
|
|
}
|
2021-11-04 13:59:28 +01:00
|
|
|
}
|
2023-08-06 21:18:51 +02:00
|
|
|
})
|
|
|
|
if (mediaProgressRemoved) {
|
|
|
|
Logger.info(`[Server] Removed ${mediaProgressRemoved} media progress for media items that no longer exist in db`)
|
2021-11-04 13:59:28 +01:00
|
|
|
}
|
2023-08-06 21:18:51 +02:00
|
|
|
}
|
2023-07-05 01:14:44 +02:00
|
|
|
|
2023-08-06 21:18:51 +02:00
|
|
|
// Remove series from hide from continue listening that no longer exist
|
2023-08-20 20:34:03 +02:00
|
|
|
const users = await Database.userModel.getOldUsers()
|
2023-08-06 21:18:51 +02:00
|
|
|
for (const _user of users) {
|
2023-07-05 01:14:44 +02:00
|
|
|
let hasUpdated = false
|
2022-09-29 00:12:27 +02:00
|
|
|
if (_user.seriesHideFromContinueListening.length) {
|
2023-09-03 17:49:02 +02:00
|
|
|
const seriesHiding = (await Database.seriesModel.findAll({
|
|
|
|
where: {
|
|
|
|
id: _user.seriesHideFromContinueListening
|
|
|
|
},
|
|
|
|
attributes: ['id'],
|
|
|
|
raw: true
|
|
|
|
})).map(se => se.id)
|
2022-09-29 00:12:27 +02:00
|
|
|
_user.seriesHideFromContinueListening = _user.seriesHideFromContinueListening.filter(seriesId => {
|
2023-09-03 17:49:02 +02:00
|
|
|
if (!seriesHiding.includes(seriesId)) { // Series removed
|
2022-09-29 00:12:27 +02:00
|
|
|
hasUpdated = true
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (hasUpdated) {
|
2023-07-05 01:14:44 +02:00
|
|
|
await Database.updateUser(_user)
|
2022-09-29 00:12:27 +02:00
|
|
|
}
|
2021-11-04 13:59:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-28 23:32:21 +01:00
|
|
|
/**
|
|
|
|
* Gracefully stop server
|
|
|
|
* Stops watcher and socket server
|
|
|
|
*/
|
2021-08-18 00:01:11 +02:00
|
|
|
async stop() {
|
2023-12-25 08:25:04 +01:00
|
|
|
Logger.info('=== Stopping Server ===')
|
2021-08-18 00:01:11 +02:00
|
|
|
await this.watcher.close()
|
|
|
|
Logger.info('Watcher Closed')
|
|
|
|
|
|
|
|
return new Promise((resolve) => {
|
2023-12-27 14:33:33 +01:00
|
|
|
SocketAuthority.close((err) => {
|
2021-08-18 00:01:11 +02:00
|
|
|
if (err) {
|
|
|
|
Logger.error('Failed to close server', err)
|
|
|
|
} else {
|
|
|
|
Logger.info('Server successfully closed')
|
|
|
|
}
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-03-17 12:06:52 +01:00
|
|
|
module.exports = Server
|