2021-08-18 00:01:11 +02:00
|
|
|
const express = require('express')
|
2021-09-22 03:57:33 +02:00
|
|
|
const Path = require('path')
|
|
|
|
const fs = require('fs-extra')
|
2021-11-13 02:43:16 +01:00
|
|
|
const date = require('date-and-time')
|
2021-10-16 14:49:12 +02:00
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
const Logger = require('./Logger')
|
2021-11-16 03:09:42 +01:00
|
|
|
const { isObject, getId } = require('./utils/index')
|
2021-10-16 14:49:12 +02:00
|
|
|
const audioFileScanner = require('./utils/audioFileScanner')
|
|
|
|
|
2021-10-28 21:41:42 +02:00
|
|
|
const BookFinder = require('./BookFinder')
|
|
|
|
|
2021-10-05 05:11:42 +02:00
|
|
|
const Library = require('./objects/Library')
|
2021-10-16 14:49:12 +02:00
|
|
|
const User = require('./objects/User')
|
2021-11-06 02:24:02 +01:00
|
|
|
const UserCollection = require('./objects/UserCollection')
|
2021-08-18 00:01:11 +02:00
|
|
|
|
|
|
|
class ApiController {
|
2021-10-09 00:30:20 +02:00
|
|
|
constructor(MetadataPath, db, scanner, auth, streamManager, rssFeeds, downloadManager, coverController, backupManager, watcher, emitter, clientEmitter) {
|
2021-08-18 00:01:11 +02:00
|
|
|
this.db = db
|
|
|
|
this.scanner = scanner
|
|
|
|
this.auth = auth
|
|
|
|
this.streamManager = streamManager
|
2021-08-23 21:08:54 +02:00
|
|
|
this.rssFeeds = rssFeeds
|
2021-09-04 21:17:26 +02:00
|
|
|
this.downloadManager = downloadManager
|
2021-10-02 01:42:48 +02:00
|
|
|
this.coverController = coverController
|
2021-10-09 00:30:20 +02:00
|
|
|
this.backupManager = backupManager
|
2021-10-05 05:11:42 +02:00
|
|
|
this.watcher = watcher
|
2021-08-18 00:01:11 +02:00
|
|
|
this.emitter = emitter
|
2021-09-06 01:20:29 +02:00
|
|
|
this.clientEmitter = clientEmitter
|
2021-09-22 03:57:33 +02:00
|
|
|
this.MetadataPath = MetadataPath
|
2021-08-18 00:01:11 +02:00
|
|
|
|
2021-10-28 21:41:42 +02:00
|
|
|
this.bookFinder = new BookFinder()
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
this.router = express()
|
|
|
|
this.init()
|
|
|
|
}
|
|
|
|
|
|
|
|
init() {
|
2021-08-21 16:15:44 +02:00
|
|
|
this.router.get('/find/covers', this.findCovers.bind(this))
|
2021-08-18 00:01:11 +02:00
|
|
|
this.router.get('/find/:method', this.find.bind(this))
|
|
|
|
|
2021-10-05 05:11:42 +02:00
|
|
|
this.router.get('/libraries', this.getLibraries.bind(this))
|
2021-10-13 03:07:42 +02:00
|
|
|
this.router.patch('/libraries/order', this.reorderLibraries.bind(this))
|
2021-10-09 18:09:06 +02:00
|
|
|
this.router.get('/library/:id/search', this.searchLibrary.bind(this))
|
2021-10-05 05:11:42 +02:00
|
|
|
this.router.get('/library/:id', this.getLibrary.bind(this))
|
|
|
|
this.router.delete('/library/:id', this.deleteLibrary.bind(this))
|
|
|
|
this.router.patch('/library/:id', this.updateLibrary.bind(this))
|
|
|
|
this.router.get('/library/:id/audiobooks', this.getLibraryAudiobooks.bind(this))
|
|
|
|
this.router.post('/library', this.createNewLibrary.bind(this))
|
|
|
|
|
|
|
|
this.router.get('/audiobooks', this.getAudiobooks.bind(this)) // Old route should pass library id
|
2021-08-21 23:23:35 +02:00
|
|
|
this.router.delete('/audiobooks', this.deleteAllAudiobooks.bind(this))
|
2021-08-27 01:32:05 +02:00
|
|
|
this.router.post('/audiobooks/delete', this.batchDeleteAudiobooks.bind(this))
|
|
|
|
this.router.post('/audiobooks/update', this.batchUpdateAudiobooks.bind(this))
|
2021-08-21 23:23:35 +02:00
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
this.router.get('/audiobook/:id', this.getAudiobook.bind(this))
|
|
|
|
this.router.delete('/audiobook/:id', this.deleteAudiobook.bind(this))
|
|
|
|
this.router.patch('/audiobook/:id/tracks', this.updateAudiobookTracks.bind(this))
|
2021-09-22 03:57:33 +02:00
|
|
|
this.router.post('/audiobook/:id/cover', this.uploadAudiobookCover.bind(this))
|
2021-10-16 03:31:00 +02:00
|
|
|
this.router.patch('/audiobook/:id/coverfile', this.updateAudiobookCoverFromFile.bind(this))
|
2021-10-28 21:41:42 +02:00
|
|
|
this.router.get('/audiobook/:id/match', this.matchAudiobookBook.bind(this))
|
2021-11-11 15:39:21 +01:00
|
|
|
this.router.get('/audiobook/:id/stream', this.openAudiobookStream.bind(this))
|
2021-08-18 00:01:11 +02:00
|
|
|
this.router.patch('/audiobook/:id', this.updateAudiobook.bind(this))
|
|
|
|
|
|
|
|
this.router.patch('/match/:id', this.match.bind(this))
|
|
|
|
|
2021-10-29 00:19:09 +02:00
|
|
|
// Old Route : Wait until refactor of mobile app to replace with path /reset-progress
|
2021-08-18 00:01:11 +02:00
|
|
|
this.router.delete('/user/audiobook/:id', this.resetUserAudiobookProgress.bind(this))
|
2021-10-29 00:19:09 +02:00
|
|
|
|
|
|
|
this.router.patch('/user/audiobook/:id/reset-progress', this.resetUserAudiobookProgress.bind(this))
|
|
|
|
this.router.patch('/user/audiobook/:id', this.updateUserAudiobookData.bind(this))
|
|
|
|
this.router.patch('/user/audiobooks', this.batchUpdateUserAudiobookData.bind(this))
|
2021-09-16 15:37:09 +02:00
|
|
|
|
2021-08-22 17:46:04 +02:00
|
|
|
this.router.patch('/user/password', this.userChangePassword.bind(this))
|
2021-08-24 01:31:04 +02:00
|
|
|
this.router.patch('/user/settings', this.userUpdateSettings.bind(this))
|
2021-09-06 01:20:29 +02:00
|
|
|
this.router.get('/users', this.getUsers.bind(this))
|
|
|
|
this.router.post('/user', this.createUser.bind(this))
|
2021-10-23 03:08:02 +02:00
|
|
|
this.router.get('/user/:id', this.getUser.bind(this))
|
2021-11-13 02:43:16 +01:00
|
|
|
this.router.get('/user/:id/listeningSessions', this.getUserListeningSessions.bind(this))
|
|
|
|
this.router.get('/user/:id/listeningStats', this.getUserListeningStats.bind(this))
|
2021-09-06 01:20:29 +02:00
|
|
|
this.router.patch('/user/:id', this.updateUser.bind(this))
|
|
|
|
this.router.delete('/user/:id', this.deleteUser.bind(this))
|
2021-08-18 00:01:11 +02:00
|
|
|
|
2021-11-06 02:24:02 +01:00
|
|
|
this.router.get('/collections', this.getUserCollections.bind(this))
|
|
|
|
this.router.get('/collection/:id', this.getUserCollection.bind(this))
|
|
|
|
this.router.post('/collection', this.createUserCollection.bind(this))
|
|
|
|
this.router.post('/collection/:id/book', this.addBookToUserCollection.bind(this))
|
|
|
|
this.router.delete('/collection/:id/book/:bookId', this.removeBookFromUserCollection.bind(this))
|
|
|
|
this.router.patch('/collection/:id', this.updateUserCollection.bind(this))
|
|
|
|
this.router.delete('/collection/:id', this.deleteUserCollection.bind(this))
|
|
|
|
|
2021-09-05 02:58:39 +02:00
|
|
|
this.router.patch('/serverSettings', this.updateServerSettings.bind(this))
|
|
|
|
|
2021-10-09 00:30:20 +02:00
|
|
|
this.router.delete('/backup/:id', this.deleteBackup.bind(this))
|
|
|
|
this.router.post('/backup/upload', this.uploadBackup.bind(this))
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
this.router.post('/authorize', this.authorize.bind(this))
|
2021-08-20 00:29:36 +02:00
|
|
|
|
2021-08-23 21:08:54 +02:00
|
|
|
this.router.post('/feed', this.openRssFeed.bind(this))
|
2021-09-04 21:17:26 +02:00
|
|
|
|
|
|
|
this.router.get('/download/:id', this.download.bind(this))
|
2021-10-05 05:11:42 +02:00
|
|
|
|
|
|
|
this.router.get('/filesystem', this.getFileSystemPaths.bind(this))
|
2021-10-16 14:49:12 +02:00
|
|
|
|
|
|
|
this.router.get('/scantracks/:id', this.scanAudioTrackNums.bind(this))
|
2021-11-13 02:43:16 +01:00
|
|
|
|
|
|
|
this.router.get('/listeningSessions', this.getCurrentUserListeningSessions.bind(this))
|
|
|
|
this.router.get('/listeningStats', this.getCurrentUserListeningStats.bind(this))
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
2021-10-28 21:41:42 +02:00
|
|
|
async find(req, res) {
|
|
|
|
var provider = req.query.provider || 'google'
|
|
|
|
var title = req.query.title || ''
|
|
|
|
var author = req.query.author || ''
|
|
|
|
var results = await this.bookFinder.search(provider, title, author)
|
|
|
|
res.json(results)
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
2021-08-21 16:15:44 +02:00
|
|
|
findCovers(req, res) {
|
|
|
|
this.scanner.findCovers(req, res)
|
|
|
|
}
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
authorize(req, res) {
|
|
|
|
if (!req.user) {
|
|
|
|
Logger.error('Invalid user in authorize')
|
|
|
|
return res.sendStatus(401)
|
|
|
|
}
|
|
|
|
res.json({ user: req.user })
|
|
|
|
}
|
|
|
|
|
2021-10-05 05:11:42 +02:00
|
|
|
getLibraries(req, res) {
|
|
|
|
var libraries = this.db.libraries.map(lib => lib.toJSON())
|
|
|
|
res.json(libraries)
|
|
|
|
}
|
|
|
|
|
2021-10-13 03:07:42 +02:00
|
|
|
async reorderLibraries(req, res) {
|
|
|
|
if (!req.user || !req.user.isRoot) {
|
|
|
|
Logger.error('[ApiController] ReorderLibraries invalid user', req.user)
|
|
|
|
return res.sendStatus(401)
|
|
|
|
}
|
|
|
|
|
|
|
|
var orderdata = req.body
|
|
|
|
var hasUpdates = false
|
|
|
|
for (let i = 0; i < orderdata.length; i++) {
|
|
|
|
var library = this.db.libraries.find(lib => lib.id === orderdata[i].id)
|
|
|
|
if (!library) {
|
|
|
|
Logger.error(`[ApiController] Invalid library not found in reorder ${orderdata[i].id}`)
|
|
|
|
return res.sendStatus(500)
|
|
|
|
}
|
|
|
|
if (library.update({ displayOrder: orderdata[i].newOrder })) {
|
|
|
|
hasUpdates = true
|
|
|
|
await this.db.updateEntity('library', library)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasUpdates) {
|
|
|
|
Logger.info(`[ApiController] Updated library display orders`)
|
|
|
|
} else {
|
|
|
|
Logger.info(`[ApiController] Library orders were up to date`)
|
|
|
|
}
|
|
|
|
|
|
|
|
var libraries = this.db.libraries.map(lib => lib.toJSON())
|
|
|
|
res.json(libraries)
|
|
|
|
}
|
|
|
|
|
2021-10-09 18:09:06 +02:00
|
|
|
searchLibrary(req, res) {
|
|
|
|
var library = this.db.libraries.find(lib => lib.id === req.params.id)
|
|
|
|
if (!library) {
|
|
|
|
return res.status(404).send('Library not found')
|
|
|
|
}
|
|
|
|
if (!req.query.q) {
|
|
|
|
return res.status(400).send('No query string')
|
|
|
|
}
|
|
|
|
var maxResults = req.query.max || 3
|
|
|
|
|
|
|
|
var bookMatches = []
|
|
|
|
var authorMatches = {}
|
|
|
|
var seriesMatches = {}
|
2021-10-17 18:29:52 +02:00
|
|
|
var tagMatches = {}
|
2021-10-09 18:09:06 +02:00
|
|
|
|
|
|
|
var audiobooksInLibrary = this.db.audiobooks.filter(ab => ab.libraryId === library.id)
|
|
|
|
audiobooksInLibrary.forEach((ab) => {
|
|
|
|
var queryResult = ab.searchQuery(req.query.q)
|
|
|
|
if (queryResult.book) {
|
2021-10-17 18:29:52 +02:00
|
|
|
var bookMatchObj = {
|
2021-10-09 18:09:06 +02:00
|
|
|
audiobook: ab,
|
2021-10-17 18:29:52 +02:00
|
|
|
matchKey: queryResult.book,
|
|
|
|
matchText: queryResult.bookMatchText
|
|
|
|
}
|
|
|
|
bookMatches.push(bookMatchObj)
|
2021-10-09 18:09:06 +02:00
|
|
|
}
|
2021-10-31 22:40:27 +01:00
|
|
|
if (queryResult.authors) {
|
|
|
|
queryResult.authors.forEach((author) => {
|
|
|
|
if (!authorMatches[author]) {
|
|
|
|
authorMatches[author] = {
|
|
|
|
author: author
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2021-10-09 18:09:06 +02:00
|
|
|
}
|
|
|
|
if (queryResult.series) {
|
|
|
|
if (!seriesMatches[queryResult.series]) {
|
|
|
|
seriesMatches[queryResult.series] = {
|
|
|
|
series: queryResult.series,
|
|
|
|
audiobooks: [ab]
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
seriesMatches[queryResult.series].audiobooks.push(ab)
|
|
|
|
}
|
|
|
|
}
|
2021-10-17 18:29:52 +02:00
|
|
|
if (queryResult.tags && queryResult.tags.length) {
|
|
|
|
queryResult.tags.forEach((tag) => {
|
|
|
|
if (!tagMatches[tag]) {
|
|
|
|
tagMatches[tag] = {
|
|
|
|
tag,
|
|
|
|
audiobooks: [ab]
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tagMatches[tag].audiobooks.push(ab)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-10-09 18:09:06 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
audiobooks: bookMatches.slice(0, maxResults),
|
2021-10-17 18:29:52 +02:00
|
|
|
tags: Object.values(tagMatches).slice(0, maxResults),
|
2021-10-09 18:09:06 +02:00
|
|
|
authors: Object.values(authorMatches).slice(0, maxResults),
|
|
|
|
series: Object.values(seriesMatches).slice(0, maxResults)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-05 05:11:42 +02:00
|
|
|
getLibrary(req, res) {
|
|
|
|
var library = this.db.libraries.find(lib => lib.id === req.params.id)
|
|
|
|
if (!library) {
|
|
|
|
return res.status(404).send('Library not found')
|
|
|
|
}
|
|
|
|
return res.json(library.toJSON())
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteLibrary(req, res) {
|
|
|
|
var library = this.db.libraries.find(lib => lib.id === req.params.id)
|
|
|
|
if (!library) {
|
|
|
|
return res.status(404).send('Library not found')
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove library watcher
|
|
|
|
this.watcher.removeLibrary(library)
|
|
|
|
|
|
|
|
// Remove audiobooks in this library
|
|
|
|
var audiobooks = this.db.audiobooks.filter(ab => ab.libraryId === library.id)
|
|
|
|
Logger.info(`[Server] deleting library "${library.name}" with ${audiobooks.length} audiobooks"`)
|
|
|
|
for (let i = 0; i < audiobooks.length; i++) {
|
|
|
|
await this.handleDeleteAudiobook(audiobooks[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
var libraryJson = library.toJSON()
|
|
|
|
await this.db.removeEntity('library', library.id)
|
|
|
|
this.emitter('library_removed', libraryJson)
|
|
|
|
return res.json(libraryJson)
|
|
|
|
}
|
|
|
|
|
|
|
|
async updateLibrary(req, res) {
|
|
|
|
var library = this.db.libraries.find(lib => lib.id === req.params.id)
|
|
|
|
if (!library) {
|
|
|
|
return res.status(404).send('Library not found')
|
|
|
|
}
|
|
|
|
var hasUpdates = library.update(req.body)
|
|
|
|
if (hasUpdates) {
|
|
|
|
// Update watcher
|
|
|
|
this.watcher.updateLibrary(library)
|
|
|
|
|
|
|
|
// Remove audiobooks no longer in library
|
|
|
|
var audiobooksToRemove = this.db.audiobooks.filter(ab => !library.checkFullPathInLibrary(ab.fullPath))
|
|
|
|
if (audiobooksToRemove.length) {
|
|
|
|
Logger.info(`[Scanner] Updating library, removing ${audiobooksToRemove.length} audiobooks`)
|
|
|
|
for (let i = 0; i < audiobooksToRemove.length; i++) {
|
|
|
|
await this.handleDeleteAudiobook(audiobooksToRemove[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await this.db.updateEntity('library', library)
|
|
|
|
this.emitter('library_updated', library.toJSON())
|
|
|
|
}
|
|
|
|
return res.json(library.toJSON())
|
|
|
|
}
|
|
|
|
|
|
|
|
getLibraryAudiobooks(req, res) {
|
|
|
|
var libraryId = req.params.id
|
|
|
|
var library = this.db.libraries.find(lib => lib.id === libraryId)
|
|
|
|
if (!library) {
|
|
|
|
return res.status(400).send('Library does not exist')
|
|
|
|
}
|
|
|
|
|
|
|
|
var audiobooks = []
|
|
|
|
if (req.query.q) {
|
|
|
|
audiobooks = this.db.audiobooks.filter(ab => {
|
|
|
|
return ab.libraryId === libraryId && ab.isSearchMatch(req.query.q)
|
|
|
|
}).map(ab => ab.toJSONMinified())
|
|
|
|
} else {
|
|
|
|
audiobooks = this.db.audiobooks.filter(ab => ab.libraryId === libraryId).map(ab => ab.toJSONMinified())
|
|
|
|
}
|
|
|
|
res.json(audiobooks)
|
|
|
|
}
|
|
|
|
|
|
|
|
async createNewLibrary(req, res) {
|
|
|
|
var newLibraryPayload = {
|
|
|
|
...req.body
|
|
|
|
}
|
|
|
|
if (!newLibraryPayload.name || !newLibraryPayload.folders || !newLibraryPayload.folders.length) {
|
|
|
|
return res.status(500).send('Invalid request')
|
|
|
|
}
|
|
|
|
|
|
|
|
var library = new Library()
|
2021-10-13 03:07:42 +02:00
|
|
|
newLibraryPayload.displayOrder = this.db.libraries.length + 1
|
2021-10-05 05:11:42 +02:00
|
|
|
library.setData(newLibraryPayload)
|
|
|
|
await this.db.insertEntity('library', library)
|
|
|
|
this.emitter('library_added', library.toJSON())
|
|
|
|
|
|
|
|
// Add library watcher
|
|
|
|
this.watcher.addLibrary(library)
|
|
|
|
|
|
|
|
res.json(library)
|
|
|
|
}
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
getAudiobooks(req, res) {
|
2021-08-21 23:23:35 +02:00
|
|
|
var audiobooks = []
|
|
|
|
if (req.query.q) {
|
|
|
|
audiobooks = this.db.audiobooks.filter(ab => {
|
|
|
|
return ab.isSearchMatch(req.query.q)
|
|
|
|
}).map(ab => ab.toJSONMinified())
|
|
|
|
} else {
|
|
|
|
audiobooks = this.db.audiobooks.map(ab => ab.toJSONMinified())
|
|
|
|
}
|
|
|
|
res.json(audiobooks)
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteAllAudiobooks(req, res) {
|
2021-09-07 00:42:15 +02:00
|
|
|
if (!req.user.isRoot) {
|
|
|
|
Logger.warn('User other than root attempted to delete all audiobooks', req.user)
|
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
2021-08-21 23:23:35 +02:00
|
|
|
Logger.info('Removing all Audiobooks')
|
|
|
|
var success = await this.db.recreateAudiobookDb()
|
|
|
|
if (success) res.sendStatus(200)
|
|
|
|
else res.sendStatus(500)
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
getAudiobook(req, res) {
|
2021-10-23 03:08:02 +02:00
|
|
|
if (!req.user) {
|
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
2021-08-18 00:01:11 +02:00
|
|
|
var audiobook = this.db.audiobooks.find(a => a.id === req.params.id)
|
|
|
|
if (!audiobook) return res.sendStatus(404)
|
2021-10-23 03:08:02 +02:00
|
|
|
|
|
|
|
// Check user can access this audiobooks library
|
|
|
|
if (!req.user.checkCanAccessLibrary(audiobook.libraryId)) {
|
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
res.json(audiobook.toJSONExpanded())
|
|
|
|
}
|
|
|
|
|
2021-08-27 01:32:05 +02:00
|
|
|
async handleDeleteAudiobook(audiobook) {
|
2021-08-18 00:01:11 +02:00
|
|
|
// Remove audiobook from users
|
|
|
|
for (let i = 0; i < this.db.users.length; i++) {
|
|
|
|
var user = this.db.users[i]
|
2021-10-29 00:19:09 +02:00
|
|
|
var madeUpdates = user.deleteAudiobookData(audiobook.id)
|
2021-08-18 00:01:11 +02:00
|
|
|
if (madeUpdates) {
|
|
|
|
await this.db.updateEntity('user', user)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove any streams open for this audiobook
|
|
|
|
var streams = this.streamManager.streams.filter(stream => stream.audiobookId === audiobook.id)
|
|
|
|
for (let i = 0; i < streams.length; i++) {
|
|
|
|
var stream = streams[i]
|
|
|
|
var client = stream.client
|
|
|
|
await stream.close()
|
|
|
|
if (client && client.user) {
|
|
|
|
client.user.stream = null
|
|
|
|
client.stream = null
|
|
|
|
this.db.updateUserStream(client.user.id, null)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-06 02:24:02 +01:00
|
|
|
// remove book from collections
|
|
|
|
var collectionsWithBook = this.db.collections.filter(c => c.books.includes(audiobook.id))
|
|
|
|
for (let i = 0; i < collectionsWithBook.length; i++) {
|
|
|
|
var collection = collectionsWithBook[i]
|
|
|
|
collection.removeBook(audiobook.id)
|
|
|
|
await this.db.updateEntity('collection', collection)
|
|
|
|
this.clientEmitter(collection.userId, 'collection_updated', collection.toJSONExpanded(this.db.audiobooks))
|
|
|
|
}
|
|
|
|
|
2021-08-27 01:32:05 +02:00
|
|
|
var audiobookJSON = audiobook.toJSONMinified()
|
2021-08-18 00:01:11 +02:00
|
|
|
await this.db.removeEntity('audiobook', audiobook.id)
|
2021-08-27 01:32:05 +02:00
|
|
|
this.emitter('audiobook_removed', audiobookJSON)
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteAudiobook(req, res) {
|
2021-09-07 00:42:15 +02:00
|
|
|
if (!req.user.canDelete) {
|
|
|
|
Logger.warn('User attempted to delete without permission', req.user)
|
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
2021-08-27 01:32:05 +02:00
|
|
|
var audiobook = this.db.audiobooks.find(a => a.id === req.params.id)
|
|
|
|
if (!audiobook) return res.sendStatus(404)
|
2021-08-18 00:01:11 +02:00
|
|
|
|
2021-08-27 01:32:05 +02:00
|
|
|
await this.handleDeleteAudiobook(audiobook)
|
2021-08-18 00:01:11 +02:00
|
|
|
res.sendStatus(200)
|
|
|
|
}
|
|
|
|
|
2021-08-27 01:32:05 +02:00
|
|
|
async batchDeleteAudiobooks(req, res) {
|
2021-09-07 00:42:15 +02:00
|
|
|
if (!req.user.canDelete) {
|
|
|
|
Logger.warn('User attempted to delete without permission', req.user)
|
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
2021-08-27 01:32:05 +02:00
|
|
|
var { audiobookIds } = req.body
|
|
|
|
if (!audiobookIds || !audiobookIds.length) {
|
|
|
|
return res.sendStatus(500)
|
|
|
|
}
|
|
|
|
|
|
|
|
var audiobooksToDelete = this.db.audiobooks.filter(ab => audiobookIds.includes(ab.id))
|
|
|
|
if (!audiobooksToDelete.length) {
|
|
|
|
return res.sendStatus(404)
|
|
|
|
}
|
|
|
|
for (let i = 0; i < audiobooksToDelete.length; i++) {
|
|
|
|
Logger.info(`[ApiController] Deleting Audiobook "${audiobooksToDelete[i].title}"`)
|
|
|
|
await this.handleDeleteAudiobook(audiobooksToDelete[i])
|
|
|
|
}
|
|
|
|
res.sendStatus(200)
|
|
|
|
}
|
|
|
|
|
|
|
|
async batchUpdateAudiobooks(req, res) {
|
2021-09-07 00:42:15 +02:00
|
|
|
if (!req.user.canUpdate) {
|
|
|
|
Logger.warn('User attempted to batch update without permission', req.user)
|
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
2021-08-27 01:32:05 +02:00
|
|
|
var audiobooks = req.body
|
|
|
|
if (!audiobooks || !audiobooks.length) {
|
|
|
|
return res.sendStatus(500)
|
|
|
|
}
|
|
|
|
|
|
|
|
var audiobooksUpdated = 0
|
|
|
|
audiobooks = audiobooks.map((ab) => {
|
|
|
|
var _ab = this.db.audiobooks.find(__ab => __ab.id === ab.id)
|
|
|
|
if (!_ab) return null
|
|
|
|
var hasUpdated = _ab.update(ab)
|
|
|
|
if (!hasUpdated) return null
|
|
|
|
audiobooksUpdated++
|
|
|
|
return _ab
|
|
|
|
}).filter(ab => ab)
|
|
|
|
|
|
|
|
if (audiobooksUpdated) {
|
|
|
|
Logger.info(`[ApiController] ${audiobooksUpdated} Audiobooks have updates`)
|
|
|
|
for (let i = 0; i < audiobooks.length; i++) {
|
|
|
|
await this.db.updateAudiobook(audiobooks[i])
|
|
|
|
this.emitter('audiobook_updated', audiobooks[i].toJSONMinified())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
success: true,
|
|
|
|
updates: audiobooksUpdated
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
async updateAudiobookTracks(req, res) {
|
2021-09-07 00:42:15 +02:00
|
|
|
if (!req.user.canUpdate) {
|
|
|
|
Logger.warn('User attempted to update audiotracks without permission', req.user)
|
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
2021-08-18 00:01:11 +02:00
|
|
|
var audiobook = this.db.audiobooks.find(a => a.id === req.params.id)
|
|
|
|
if (!audiobook) return res.sendStatus(404)
|
2021-09-05 01:02:42 +02:00
|
|
|
var orderedFileData = req.body.orderedFileData
|
2021-08-18 00:01:11 +02:00
|
|
|
Logger.info(`Updating audiobook tracks called ${audiobook.id}`)
|
2021-09-05 01:02:42 +02:00
|
|
|
audiobook.updateAudioTracks(orderedFileData)
|
2021-08-18 00:01:11 +02:00
|
|
|
await this.db.updateAudiobook(audiobook)
|
|
|
|
this.emitter('audiobook_updated', audiobook.toJSONMinified())
|
|
|
|
res.json(audiobook.toJSON())
|
|
|
|
}
|
|
|
|
|
2021-09-22 03:57:33 +02:00
|
|
|
async uploadAudiobookCover(req, res) {
|
|
|
|
if (!req.user.canUpload || !req.user.canUpdate) {
|
|
|
|
Logger.warn('User attempted to upload a cover without permission', req.user)
|
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
2021-10-02 01:42:48 +02:00
|
|
|
|
2021-09-22 03:57:33 +02:00
|
|
|
var audiobookId = req.params.id
|
|
|
|
var audiobook = this.db.audiobooks.find(ab => ab.id === audiobookId)
|
|
|
|
if (!audiobook) {
|
|
|
|
return res.status(404).send('Audiobook not found')
|
|
|
|
}
|
|
|
|
|
2021-10-02 01:42:48 +02:00
|
|
|
var result = null
|
|
|
|
if (req.body && req.body.url) {
|
|
|
|
Logger.debug(`[ApiController] Requesting download cover from url "${req.body.url}"`)
|
|
|
|
result = await this.coverController.downloadCoverFromUrl(audiobook, req.body.url)
|
|
|
|
} else if (req.files && req.files.cover) {
|
|
|
|
Logger.debug(`[ApiController] Handling uploaded cover`)
|
|
|
|
var coverFile = req.files.cover
|
|
|
|
result = await this.coverController.uploadCover(audiobook, coverFile)
|
2021-09-22 03:57:33 +02:00
|
|
|
} else {
|
2021-10-02 01:42:48 +02:00
|
|
|
return res.status(400).send('Invalid request no file or url')
|
2021-09-22 03:57:33 +02:00
|
|
|
}
|
|
|
|
|
2021-10-02 01:42:48 +02:00
|
|
|
if (result && result.error) {
|
|
|
|
return res.status(400).send(result.error)
|
|
|
|
} else if (!result || !result.cover) {
|
|
|
|
return res.status(500).send('Unknown error occurred')
|
2021-09-22 03:57:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
await this.db.updateAudiobook(audiobook)
|
|
|
|
this.emitter('audiobook_updated', audiobook.toJSONMinified())
|
|
|
|
res.json({
|
|
|
|
success: true,
|
2021-10-02 01:42:48 +02:00
|
|
|
cover: result.cover
|
2021-09-22 03:57:33 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-16 03:31:00 +02:00
|
|
|
async updateAudiobookCoverFromFile(req, res) {
|
|
|
|
if (!req.user.canUpdate) {
|
|
|
|
Logger.warn('User attempted to update without permission', req.user)
|
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
|
|
|
var audiobook = this.db.audiobooks.find(a => a.id === req.params.id)
|
|
|
|
if (!audiobook) return res.sendStatus(404)
|
|
|
|
|
|
|
|
var coverFile = req.body
|
|
|
|
var updated = await audiobook.setCoverFromFile(coverFile)
|
|
|
|
|
|
|
|
if (updated) {
|
|
|
|
await this.db.updateAudiobook(audiobook)
|
|
|
|
this.emitter('audiobook_updated', audiobook.toJSONMinified())
|
|
|
|
}
|
|
|
|
|
|
|
|
if (updated) res.status(200).send('Cover updated successfully')
|
|
|
|
else res.status(200).send('No update was made to cover')
|
|
|
|
}
|
|
|
|
|
2021-10-28 21:41:42 +02:00
|
|
|
async matchAudiobookBook(req, res) {
|
|
|
|
var audiobook = this.db.audiobooks.find(a => a.id === req.params.id)
|
|
|
|
if (!audiobook) return res.sendStatus(404)
|
|
|
|
|
|
|
|
var provider = req.query.provider || 'google'
|
|
|
|
var excludeAuthor = req.query.excludeAuthor === '1'
|
|
|
|
var authorSearch = excludeAuthor ? null : audiobook.authorFL
|
|
|
|
|
|
|
|
var results = await this.bookFinder.search(provider, audiobook.title, authorSearch)
|
|
|
|
res.json(results)
|
|
|
|
}
|
|
|
|
|
2021-11-11 15:39:21 +01:00
|
|
|
async openAudiobookStream(req, res) {
|
|
|
|
var audiobook = this.db.audiobooks.find(a => a.id === req.params.id)
|
|
|
|
if (!audiobook) return res.sendStatus(404)
|
|
|
|
|
|
|
|
this.streamManager.openStreamApiRequest(res, req.user, audiobook)
|
|
|
|
}
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
async updateAudiobook(req, res) {
|
2021-09-07 00:42:15 +02:00
|
|
|
if (!req.user.canUpdate) {
|
|
|
|
Logger.warn('User attempted to update without permission', req.user)
|
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
2021-08-18 00:01:11 +02:00
|
|
|
var audiobook = this.db.audiobooks.find(a => a.id === req.params.id)
|
|
|
|
if (!audiobook) return res.sendStatus(404)
|
|
|
|
var hasUpdates = audiobook.update(req.body)
|
|
|
|
if (hasUpdates) {
|
|
|
|
await this.db.updateAudiobook(audiobook)
|
|
|
|
}
|
|
|
|
this.emitter('audiobook_updated', audiobook.toJSONMinified())
|
|
|
|
res.json(audiobook.toJSON())
|
|
|
|
}
|
|
|
|
|
|
|
|
async match(req, res) {
|
|
|
|
var body = req.body
|
|
|
|
var audiobookId = req.params.id
|
|
|
|
var audiobook = this.db.audiobooks.find(ab => ab.id === audiobookId)
|
|
|
|
var bookData = {
|
|
|
|
olid: body.id,
|
|
|
|
publish_year: body.first_publish_year,
|
|
|
|
description: body.description,
|
|
|
|
title: body.title,
|
|
|
|
author: body.author,
|
|
|
|
cover: body.cover
|
|
|
|
}
|
|
|
|
audiobook.setBook(bookData)
|
|
|
|
await this.db.updateAudiobook(audiobook)
|
|
|
|
|
|
|
|
this.emitter('audiobook_updated', audiobook.toJSONMinified())
|
|
|
|
|
|
|
|
res.sendStatus(200)
|
|
|
|
}
|
|
|
|
|
|
|
|
async resetUserAudiobookProgress(req, res) {
|
2021-10-23 03:08:02 +02:00
|
|
|
var audiobook = this.db.audiobooks.find(ab => ab.id === req.params.id)
|
|
|
|
if (!audiobook) {
|
|
|
|
return res.status(404).send('Audiobook not found')
|
|
|
|
}
|
|
|
|
req.user.resetAudiobookProgress(audiobook)
|
2021-08-18 00:01:11 +02:00
|
|
|
await this.db.updateEntity('user', req.user)
|
2021-09-06 21:13:01 +02:00
|
|
|
this.clientEmitter(req.user.id, 'user_updated', req.user.toJSONForBrowser())
|
|
|
|
res.sendStatus(200)
|
|
|
|
}
|
|
|
|
|
2021-10-29 00:19:09 +02:00
|
|
|
async updateUserAudiobookData(req, res) {
|
2021-10-23 03:08:02 +02:00
|
|
|
var audiobook = this.db.audiobooks.find(ab => ab.id === req.params.id)
|
|
|
|
if (!audiobook) {
|
|
|
|
return res.status(404).send('Audiobook not found')
|
|
|
|
}
|
2021-10-29 00:19:09 +02:00
|
|
|
var wasUpdated = req.user.updateAudiobookData(audiobook, req.body)
|
2021-09-06 21:13:01 +02:00
|
|
|
if (wasUpdated) {
|
|
|
|
await this.db.updateEntity('user', req.user)
|
|
|
|
this.clientEmitter(req.user.id, 'user_updated', req.user.toJSONForBrowser())
|
|
|
|
}
|
2021-08-18 00:01:11 +02:00
|
|
|
res.sendStatus(200)
|
|
|
|
}
|
2021-08-20 00:29:36 +02:00
|
|
|
|
2021-10-29 00:19:09 +02:00
|
|
|
async batchUpdateUserAudiobookData(req, res) {
|
|
|
|
var userAbDataPayloads = req.body
|
|
|
|
if (!userAbDataPayloads || !userAbDataPayloads.length) {
|
2021-09-16 15:37:09 +02:00
|
|
|
return res.sendStatus(500)
|
|
|
|
}
|
|
|
|
|
|
|
|
var shouldUpdate = false
|
2021-10-29 00:19:09 +02:00
|
|
|
userAbDataPayloads.forEach((userAbData) => {
|
|
|
|
var audiobook = this.db.audiobooks.find(ab => ab.id === userAbData.audiobookId)
|
2021-10-23 03:08:02 +02:00
|
|
|
if (audiobook) {
|
2021-10-29 00:19:09 +02:00
|
|
|
var wasUpdated = req.user.updateAudiobookData(audiobook, userAbData)
|
2021-10-23 03:08:02 +02:00
|
|
|
if (wasUpdated) shouldUpdate = true
|
|
|
|
}
|
2021-09-16 15:37:09 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
if (shouldUpdate) {
|
|
|
|
await this.db.updateEntity('user', req.user)
|
|
|
|
this.clientEmitter(req.user.id, 'user_updated', req.user.toJSONForBrowser())
|
|
|
|
}
|
|
|
|
|
|
|
|
res.sendStatus(200)
|
|
|
|
}
|
|
|
|
|
2021-08-22 17:46:04 +02:00
|
|
|
userChangePassword(req, res) {
|
|
|
|
this.auth.userChangePassword(req, res)
|
|
|
|
}
|
|
|
|
|
2021-08-23 21:08:54 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-08-24 01:31:04 +02:00
|
|
|
async userUpdateSettings(req, res) {
|
|
|
|
var settingsUpdate = req.body
|
|
|
|
if (!settingsUpdate || !isObject(settingsUpdate)) {
|
|
|
|
return res.sendStatus(500)
|
|
|
|
}
|
|
|
|
var madeUpdates = req.user.updateSettings(settingsUpdate)
|
|
|
|
if (madeUpdates) {
|
|
|
|
await this.db.updateEntity('user', req.user)
|
|
|
|
}
|
|
|
|
return res.json({
|
|
|
|
success: true,
|
|
|
|
settings: req.user.settings
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-23 03:08:02 +02:00
|
|
|
userJsonWithBookProgressDetails(user) {
|
|
|
|
var json = user.toJSONForBrowser()
|
|
|
|
|
|
|
|
// User audiobook progress attach book details
|
|
|
|
if (json.audiobooks && Object.keys(json.audiobooks).length) {
|
|
|
|
for (const audiobookId in json.audiobooks) {
|
|
|
|
var audiobook = this.db.audiobooks.find(ab => ab.id === audiobookId)
|
|
|
|
if (!audiobook) {
|
|
|
|
Logger.error('[ApiController] Audiobook not found for users progress ' + audiobookId)
|
|
|
|
} else {
|
|
|
|
json.audiobooks[audiobookId].book = audiobook.book.toJSON()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return json
|
|
|
|
}
|
|
|
|
|
|
|
|
getUsers(req, res) {
|
|
|
|
if (req.user.type !== 'root') return res.sendStatus(403)
|
|
|
|
var users = this.db.users.map(u => this.userJsonWithBookProgressDetails(u))
|
|
|
|
res.json(users)
|
|
|
|
}
|
|
|
|
|
2021-08-27 14:01:47 +02:00
|
|
|
async createUser(req, res) {
|
2021-09-07 00:42:15 +02:00
|
|
|
if (!req.user.isRoot) {
|
|
|
|
Logger.warn('Non-root user attempted to create user', req.user)
|
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
2021-08-27 14:01:47 +02:00
|
|
|
var account = req.body
|
2021-10-21 01:54:05 +02:00
|
|
|
|
|
|
|
var username = account.username
|
|
|
|
var usernameExists = this.db.users.find(u => u.username.toLowerCase() === username.toLowerCase())
|
|
|
|
if (usernameExists) {
|
|
|
|
return res.status(500).send('Username already taken')
|
|
|
|
}
|
|
|
|
|
2021-11-16 03:09:42 +01:00
|
|
|
account.id = getId('usr')
|
2021-08-27 14:01:47 +02:00
|
|
|
account.pash = await this.auth.hashPass(account.password)
|
|
|
|
delete account.password
|
|
|
|
account.token = await this.auth.generateAccessToken({ userId: account.id })
|
|
|
|
account.createdAt = Date.now()
|
|
|
|
var newUser = new User(account)
|
2021-10-05 05:11:42 +02:00
|
|
|
var success = await this.db.insertEntity('user', newUser)
|
2021-08-27 14:01:47 +02:00
|
|
|
if (success) {
|
2021-09-06 01:20:29 +02:00
|
|
|
this.clientEmitter(req.user.id, 'user_added', newUser)
|
2021-08-27 14:01:47 +02:00
|
|
|
res.json({
|
|
|
|
user: newUser.toJSONForBrowser()
|
|
|
|
})
|
|
|
|
} else {
|
2021-10-21 01:54:05 +02:00
|
|
|
return res.status(500).send('Failed to save new user')
|
2021-08-27 14:01:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-23 03:08:02 +02:00
|
|
|
async getUser(req, res) {
|
|
|
|
if (!req.user.isRoot) {
|
|
|
|
Logger.error('User other than root attempting to get user', req.user)
|
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
|
|
|
|
|
|
|
var user = this.db.users.find(u => u.id === req.params.id)
|
|
|
|
if (!user) {
|
|
|
|
return res.sendStatus(404)
|
|
|
|
}
|
|
|
|
|
|
|
|
res.json(this.userJsonWithBookProgressDetails(user))
|
|
|
|
}
|
|
|
|
|
2021-09-06 01:20:29 +02:00
|
|
|
async updateUser(req, res) {
|
2021-09-07 00:42:15 +02:00
|
|
|
if (!req.user.isRoot) {
|
2021-09-06 01:20:29 +02:00
|
|
|
Logger.error('User other than root attempting to update user', req.user)
|
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
|
|
|
|
|
|
|
var user = this.db.users.find(u => u.id === req.params.id)
|
|
|
|
if (!user) {
|
|
|
|
return res.sendStatus(404)
|
|
|
|
}
|
|
|
|
|
|
|
|
var account = req.body
|
2021-10-21 01:54:05 +02:00
|
|
|
|
|
|
|
if (account.username !== undefined && account.username !== user.username) {
|
|
|
|
var usernameExists = this.db.users.find(u => u.username.toLowerCase() === account.username.toLowerCase())
|
|
|
|
if (usernameExists) {
|
|
|
|
return res.status(500).send('Username already taken')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-06 01:20:29 +02:00
|
|
|
// Updating password
|
|
|
|
if (account.password) {
|
|
|
|
account.pash = await this.auth.hashPass(account.password)
|
|
|
|
delete account.password
|
|
|
|
}
|
|
|
|
|
|
|
|
var hasUpdated = user.update(account)
|
|
|
|
if (hasUpdated) {
|
|
|
|
await this.db.updateEntity('user', user)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.clientEmitter(req.user.id, 'user_updated', user.toJSONForBrowser())
|
|
|
|
res.json({
|
|
|
|
success: true,
|
|
|
|
user: user.toJSONForBrowser()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-27 14:01:47 +02:00
|
|
|
async deleteUser(req, res) {
|
2021-09-07 00:42:15 +02:00
|
|
|
if (!req.user.isRoot) {
|
|
|
|
Logger.error('User other than root attempting to delete user', req.user)
|
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
2021-08-27 14:01:47 +02:00
|
|
|
if (req.params.id === 'root') {
|
|
|
|
return res.sendStatus(500)
|
|
|
|
}
|
|
|
|
if (req.user.id === req.params.id) {
|
|
|
|
Logger.error('Attempting to delete themselves...')
|
|
|
|
return res.sendStatus(500)
|
|
|
|
}
|
|
|
|
var user = this.db.users.find(u => u.id === req.params.id)
|
|
|
|
if (!user) {
|
|
|
|
Logger.error('User not found')
|
|
|
|
return res.json({
|
|
|
|
error: 'User not found'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-06 02:24:02 +01:00
|
|
|
// delete user collections
|
|
|
|
var userCollections = this.db.collections.filter(c => c.userId === user.id)
|
|
|
|
var collectionsToRemove = userCollections.map(uc => uc.id)
|
|
|
|
for (let i = 0; i < collectionsToRemove.length; i++) {
|
|
|
|
await this.db.removeEntity('collection', collectionsToRemove[i])
|
|
|
|
}
|
|
|
|
|
2021-08-27 14:01:47 +02:00
|
|
|
// Todo: check if user is logged in and cancel streams
|
|
|
|
|
|
|
|
var userJson = user.toJSONForBrowser()
|
|
|
|
await this.db.removeEntity('user', user.id)
|
2021-09-06 01:20:29 +02:00
|
|
|
this.clientEmitter(req.user.id, 'user_removed', userJson)
|
2021-08-27 14:01:47 +02:00
|
|
|
res.json({
|
|
|
|
success: true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-06 02:24:02 +01:00
|
|
|
async getUserCollections(req, res) {
|
|
|
|
var collections = this.db.collections.filter(c => c.userId === req.user.id)
|
|
|
|
var expandedCollections = collections.map(c => c.toJSONExpanded(this.db.audiobooks))
|
|
|
|
res.json(expandedCollections)
|
|
|
|
}
|
|
|
|
|
|
|
|
async getUserCollection(req, res) {
|
|
|
|
var collection = this.db.collections.find(c => c.id === req.params.id)
|
|
|
|
if (!collection) {
|
|
|
|
return res.status(404).send('Collection not found')
|
|
|
|
}
|
|
|
|
res.json(collection.toJSONExpanded(this.db.audiobooks))
|
|
|
|
}
|
|
|
|
|
|
|
|
async createUserCollection(req, res) {
|
|
|
|
var newCollection = new UserCollection()
|
|
|
|
req.body.userId = req.user.id
|
|
|
|
var success = newCollection.setData(req.body)
|
|
|
|
if (!success) {
|
|
|
|
return res.status(500).send('Invalid collection data')
|
|
|
|
}
|
|
|
|
var jsonExpanded = newCollection.toJSONExpanded(this.db.audiobooks)
|
|
|
|
await this.db.insertEntity('collection', newCollection)
|
|
|
|
this.clientEmitter(req.user.id, 'collection_added', jsonExpanded)
|
|
|
|
res.json(jsonExpanded)
|
|
|
|
}
|
|
|
|
|
|
|
|
async addBookToUserCollection(req, res) {
|
|
|
|
var collection = this.db.collections.find(c => c.id === req.params.id)
|
|
|
|
if (!collection) {
|
|
|
|
return res.status(404).send('Collection not found')
|
|
|
|
}
|
|
|
|
var audiobook = this.db.audiobooks.find(ab => ab.id === req.body.id)
|
|
|
|
if (!audiobook) {
|
|
|
|
return res.status(500).send('Book not found')
|
|
|
|
}
|
|
|
|
if (audiobook.libraryId !== collection.libraryId) {
|
|
|
|
return res.status(500).send('Book in different library')
|
|
|
|
}
|
|
|
|
if (collection.books.includes(req.body.id)) {
|
|
|
|
return res.status(500).send('Book already in collection')
|
|
|
|
}
|
|
|
|
collection.addBook(req.body.id)
|
|
|
|
var jsonExpanded = collection.toJSONExpanded(this.db.audiobooks)
|
|
|
|
await this.db.updateEntity('collection', collection)
|
|
|
|
this.clientEmitter(req.user.id, 'collection_updated', jsonExpanded)
|
|
|
|
res.json(jsonExpanded)
|
|
|
|
}
|
|
|
|
|
|
|
|
async removeBookFromUserCollection(req, res) {
|
|
|
|
var collection = this.db.collections.find(c => c.id === req.params.id)
|
|
|
|
if (!collection) {
|
|
|
|
return res.status(404).send('Collection not found')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (collection.books.includes(req.params.bookId)) {
|
|
|
|
collection.removeBook(req.params.bookId)
|
|
|
|
var jsonExpanded = collection.toJSONExpanded(this.db.audiobooks)
|
|
|
|
await this.db.updateEntity('collection', collection)
|
|
|
|
this.clientEmitter(req.user.id, 'collection_updated', jsonExpanded)
|
|
|
|
}
|
|
|
|
res.json(collection.toJSONExpanded(this.db.audiobooks))
|
|
|
|
}
|
|
|
|
|
|
|
|
async updateUserCollection(req, res) {
|
|
|
|
var collection = this.db.collections.find(c => c.id === req.params.id)
|
|
|
|
if (!collection) {
|
|
|
|
return res.status(404).send('Collection not found')
|
|
|
|
}
|
|
|
|
var wasUpdated = collection.update(req.body)
|
|
|
|
var jsonExpanded = collection.toJSONExpanded(this.db.audiobooks)
|
|
|
|
if (wasUpdated) {
|
|
|
|
await this.db.updateEntity('collection', collection)
|
|
|
|
this.clientEmitter(req.user.id, 'collection_updated', jsonExpanded)
|
|
|
|
}
|
|
|
|
res.json(jsonExpanded)
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteUserCollection(req, res) {
|
|
|
|
var collection = this.db.collections.find(c => c.id === req.params.id)
|
|
|
|
if (!collection) {
|
|
|
|
return res.status(404).send('Collection not found')
|
|
|
|
}
|
|
|
|
var jsonExpanded = collection.toJSONExpanded(this.db.audiobooks)
|
|
|
|
await this.db.removeEntity('collection', collection.id)
|
|
|
|
this.clientEmitter(req.user.id, 'collection_removed', jsonExpanded)
|
|
|
|
res.sendStatus(200)
|
|
|
|
}
|
|
|
|
|
2021-09-05 02:58:39 +02:00
|
|
|
async updateServerSettings(req, res) {
|
2021-09-07 00:42:15 +02:00
|
|
|
if (!req.user.isRoot) {
|
|
|
|
Logger.error('User other than root attempting to update server settings', req.user)
|
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
2021-09-05 02:58:39 +02:00
|
|
|
var settingsUpdate = req.body
|
|
|
|
if (!settingsUpdate || !isObject(settingsUpdate)) {
|
2021-10-06 04:10:49 +02:00
|
|
|
return res.status(500).send('Invalid settings update object')
|
2021-09-05 02:58:39 +02:00
|
|
|
}
|
2021-10-09 18:09:06 +02:00
|
|
|
|
2021-09-05 02:58:39 +02:00
|
|
|
var madeUpdates = this.db.serverSettings.update(settingsUpdate)
|
|
|
|
if (madeUpdates) {
|
2021-10-09 18:09:06 +02:00
|
|
|
// If backup schedule is updated - update backup manager
|
|
|
|
if (settingsUpdate.backupSchedule !== undefined) {
|
|
|
|
this.backupManager.updateCronSchedule()
|
|
|
|
}
|
|
|
|
|
2021-09-05 02:58:39 +02:00
|
|
|
await this.db.updateEntity('settings', this.db.serverSettings)
|
|
|
|
}
|
|
|
|
return res.json({
|
|
|
|
success: true,
|
|
|
|
serverSettings: this.db.serverSettings
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-09 00:30:20 +02:00
|
|
|
async deleteBackup(req, res) {
|
|
|
|
if (!req.user.isRoot) {
|
|
|
|
Logger.error(`[ApiController] Non-Root user attempting to delete backup`, req.user)
|
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
|
|
|
var backup = this.backupManager.backups.find(b => b.id === req.params.id)
|
|
|
|
if (!backup) {
|
|
|
|
return res.sendStatus(404)
|
|
|
|
}
|
|
|
|
await this.backupManager.removeBackup(backup)
|
|
|
|
res.json(this.backupManager.backups.map(b => b.toJSON()))
|
|
|
|
}
|
|
|
|
|
|
|
|
async uploadBackup(req, res) {
|
|
|
|
if (!req.user.isRoot) {
|
|
|
|
Logger.error(`[ApiController] Non-Root user attempting to upload backup`, req.user)
|
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
|
|
|
if (!req.files.file) {
|
|
|
|
Logger.error('[ApiController] Upload backup invalid')
|
|
|
|
return res.sendStatus(500)
|
|
|
|
}
|
|
|
|
this.backupManager.uploadBackup(req, res)
|
|
|
|
}
|
|
|
|
|
2021-09-04 21:17:26 +02:00
|
|
|
async download(req, res) {
|
2021-09-07 00:42:15 +02:00
|
|
|
if (!req.user.canDownload) {
|
|
|
|
Logger.error('User attempting to download without permission', req.user)
|
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
2021-09-04 21:17:26 +02:00
|
|
|
var downloadId = req.params.id
|
|
|
|
Logger.info('Download Request', downloadId)
|
|
|
|
var download = this.downloadManager.getDownload(downloadId)
|
|
|
|
if (!download) {
|
|
|
|
Logger.error('Download request not found', downloadId)
|
|
|
|
return res.sendStatus(404)
|
|
|
|
}
|
|
|
|
|
|
|
|
var options = {
|
|
|
|
headers: {
|
|
|
|
'Content-Type': download.mimeType
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res.download(download.fullPath, download.filename, options, (err) => {
|
|
|
|
if (err) {
|
|
|
|
Logger.error('Download Error', err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-05 05:11:42 +02:00
|
|
|
async getDirectories(dir, relpath, excludedDirs, level = 0) {
|
|
|
|
try {
|
|
|
|
var paths = await fs.readdir(dir)
|
|
|
|
|
|
|
|
var dirs = await Promise.all(paths.map(async dirname => {
|
|
|
|
var fullPath = Path.join(dir, dirname)
|
|
|
|
var path = Path.join(relpath, dirname)
|
|
|
|
|
|
|
|
var isDir = (await fs.lstat(fullPath)).isDirectory()
|
2021-10-11 02:29:22 +02:00
|
|
|
if (isDir && !excludedDirs.includes(path) && dirname !== 'node_modules') {
|
2021-10-05 05:11:42 +02:00
|
|
|
return {
|
|
|
|
path,
|
|
|
|
dirname,
|
|
|
|
fullPath,
|
|
|
|
level,
|
|
|
|
dirs: level < 4 ? (await this.getDirectories(fullPath, path, excludedDirs, level + 1)) : []
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
dirs = dirs.filter(d => d)
|
|
|
|
return dirs
|
|
|
|
} catch (error) {
|
|
|
|
Logger.error('Failed to readdir', dir, error)
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async getFileSystemPaths(req, res) {
|
2021-10-11 02:29:22 +02:00
|
|
|
var excludedDirs = ['node_modules', 'client', 'server', '.git', 'static', 'build', 'dist', 'metadata', 'config', 'sys', 'proc'].map(dirname => {
|
|
|
|
return Path.sep + dirname
|
|
|
|
})
|
2021-10-05 05:11:42 +02:00
|
|
|
|
|
|
|
// Do not include existing mapped library paths in response
|
|
|
|
this.db.libraries.forEach(lib => {
|
|
|
|
lib.folders.forEach((folder) => {
|
2021-10-11 02:29:22 +02:00
|
|
|
var dir = folder.fullPath
|
|
|
|
if (dir.includes(global.appRoot)) dir = dir.replace(global.appRoot, '')
|
|
|
|
excludedDirs.push(dir)
|
2021-10-05 05:11:42 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Logger.debug(`[Server] get file system paths, excluded: ${excludedDirs.join(', ')}`)
|
|
|
|
var dirs = await this.getDirectories(global.appRoot, '/', excludedDirs)
|
|
|
|
res.json(dirs)
|
|
|
|
}
|
2021-10-16 14:49:12 +02:00
|
|
|
|
|
|
|
async scanAudioTrackNums(req, res) {
|
|
|
|
if (!req.user || !req.user.isRoot) {
|
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
|
|
|
var audiobook = this.db.audiobooks.find(ab => ab.id === req.params.id)
|
|
|
|
if (!audiobook) {
|
|
|
|
return res.status(404).send('Audiobook not found')
|
|
|
|
}
|
|
|
|
|
|
|
|
var scandata = await audioFileScanner.scanTrackNumbers(audiobook)
|
|
|
|
res.json(scandata)
|
|
|
|
}
|
2021-11-13 02:43:16 +01:00
|
|
|
|
|
|
|
async getUserListeningSessionsHelper(userId) {
|
|
|
|
var userSessions = await this.db.selectUserSessions(userId)
|
|
|
|
var listeningSessions = userSessions.filter(us => us.sessionType === 'listeningSession')
|
|
|
|
return listeningSessions.sort((a, b) => b.lastUpdate - a.lastUpdate)
|
|
|
|
}
|
|
|
|
|
|
|
|
async getUserListeningSessions(req, res) {
|
|
|
|
if (!req.user || !req.user.isRoot) {
|
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
|
|
|
var listeningSessions = await this.getUserListeningSessionsHelper(req.params.id)
|
|
|
|
res.json(listeningSessions.slice(0, 10))
|
|
|
|
}
|
|
|
|
|
|
|
|
async getCurrentUserListeningSessions(req, res) {
|
|
|
|
if (!req.user) {
|
|
|
|
return res.sendStatus(500)
|
|
|
|
}
|
|
|
|
var listeningSessions = await this.getUserListeningSessionsHelper(req.user.id)
|
|
|
|
res.json(listeningSessions.slice(0, 10))
|
|
|
|
}
|
|
|
|
|
|
|
|
async getUserListeningStatsHelpers(userId) {
|
|
|
|
const today = date.format(new Date(), 'YYYY-MM-DD')
|
|
|
|
|
|
|
|
var listeningSessions = await this.getUserListeningSessionsHelper(userId)
|
|
|
|
var listeningStats = {
|
|
|
|
totalTime: 0,
|
|
|
|
books: {},
|
|
|
|
days: {},
|
|
|
|
dayOfWeek: {},
|
|
|
|
today: 0
|
|
|
|
}
|
|
|
|
listeningSessions.forEach((s) => {
|
|
|
|
if (s.dayOfWeek) {
|
|
|
|
if (!listeningStats.dayOfWeek[s.dayOfWeek]) listeningStats.dayOfWeek[s.dayOfWeek] = 0
|
|
|
|
listeningStats.dayOfWeek[s.dayOfWeek] += s.timeListening
|
|
|
|
}
|
|
|
|
if (s.date) {
|
|
|
|
if (!listeningStats.days[s.date]) listeningStats.days[s.date] = 0
|
|
|
|
listeningStats.days[s.date] += s.timeListening
|
|
|
|
|
|
|
|
if (s.date === today) {
|
|
|
|
listeningStats.today += s.timeListening
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!listeningStats.books[s.audiobookId]) listeningStats.books[s.audiobookId] = 0
|
|
|
|
listeningStats.books[s.audiobookId] += s.timeListening
|
|
|
|
|
|
|
|
listeningStats.totalTime += s.timeListening
|
|
|
|
})
|
|
|
|
return listeningStats
|
|
|
|
}
|
|
|
|
|
|
|
|
async getUserListeningStats(req, res) {
|
|
|
|
if (!req.user || !req.user.isRoot) {
|
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
|
|
|
var listeningStats = await this.getUserListeningStatsHelpers(req.params.id)
|
|
|
|
res.json(listeningStats)
|
|
|
|
}
|
|
|
|
|
|
|
|
async getCurrentUserListeningStats(req, res) {
|
|
|
|
if (!req.user) {
|
|
|
|
return res.sendStatus(500)
|
|
|
|
}
|
|
|
|
var listeningStats = await this.getUserListeningStatsHelpers(req.user.id)
|
|
|
|
res.json(listeningStats)
|
|
|
|
}
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
module.exports = ApiController
|