2024-08-11 22:15:34 +02:00
|
|
|
const { Request, Response, NextFunction } = require('express')
|
2022-11-19 20:28:06 +01:00
|
|
|
const Logger = require('../Logger')
|
2023-07-05 01:14:44 +02:00
|
|
|
const Database = require('../Database')
|
2022-11-19 20:28:06 +01:00
|
|
|
|
2024-08-11 22:15:34 +02:00
|
|
|
/**
|
2024-08-12 00:01:25 +02:00
|
|
|
* @typedef RequestUserObject
|
2024-08-11 23:07:29 +02:00
|
|
|
* @property {import('../models/User')} user
|
2024-08-11 22:15:34 +02:00
|
|
|
*
|
2024-08-12 00:01:25 +02:00
|
|
|
* @typedef {Request & RequestUserObject} RequestWithUser
|
2024-08-11 22:15:34 +02:00
|
|
|
*/
|
|
|
|
|
2022-11-19 20:28:06 +01:00
|
|
|
class ToolsController {
|
2024-08-01 00:32:51 +02:00
|
|
|
constructor() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* POST: /api/tools/item/:id/encode-m4b
|
|
|
|
* Start an audiobook merge to m4b task
|
|
|
|
*
|
|
|
|
* @this import('../routers/ApiRouter')
|
|
|
|
*
|
2024-08-11 22:15:34 +02:00
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
2024-08-01 00:32:51 +02:00
|
|
|
*/
|
2022-11-19 20:28:06 +01:00
|
|
|
async encodeM4b(req, res) {
|
|
|
|
if (req.libraryItem.isMissing || req.libraryItem.isInvalid) {
|
|
|
|
Logger.error(`[MiscController] encodeM4b: library item not found or invalid ${req.params.id}`)
|
|
|
|
return res.status(404).send('Audiobook not found')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.libraryItem.mediaType !== 'book') {
|
|
|
|
Logger.error(`[MiscController] encodeM4b: Invalid library item ${req.params.id}: not a book`)
|
2024-08-26 00:13:09 +02:00
|
|
|
return res.status(400).send('Invalid library item: not a book')
|
2022-11-19 20:28:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (req.libraryItem.media.tracks.length <= 0) {
|
|
|
|
Logger.error(`[MiscController] encodeM4b: Invalid audiobook ${req.params.id}: no audio tracks`)
|
2024-08-26 00:13:09 +02:00
|
|
|
return res.status(400).send('Invalid audiobook: no audio tracks')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.abMergeManager.getPendingTaskByLibraryItemId(req.libraryItem.id)) {
|
2024-08-27 00:02:29 +02:00
|
|
|
Logger.error(`[MiscController] encodeM4b: Audiobook ${req.params.id} is already processing`)
|
|
|
|
return res.status(400).send('Audiobook is already processing')
|
2022-11-19 20:28:06 +01:00
|
|
|
}
|
|
|
|
|
2022-12-20 00:13:04 +01:00
|
|
|
const options = req.query || {}
|
2024-08-11 23:07:29 +02:00
|
|
|
this.abMergeManager.startAudiobookMerge(req.user.id, req.libraryItem, options)
|
2022-11-19 20:28:06 +01:00
|
|
|
|
|
|
|
res.sendStatus(200)
|
|
|
|
}
|
|
|
|
|
2024-08-01 00:32:51 +02:00
|
|
|
/**
|
|
|
|
* DELETE: /api/tools/item/:id/encode-m4b
|
|
|
|
* Cancel a running m4b merge task
|
|
|
|
*
|
|
|
|
* @this import('../routers/ApiRouter')
|
|
|
|
*
|
2024-08-11 22:15:34 +02:00
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
2024-08-01 00:32:51 +02:00
|
|
|
*/
|
2022-11-19 20:28:06 +01:00
|
|
|
async cancelM4bEncode(req, res) {
|
|
|
|
const workerTask = this.abMergeManager.getPendingTaskByLibraryItemId(req.params.id)
|
|
|
|
if (!workerTask) return res.sendStatus(404)
|
|
|
|
|
|
|
|
this.abMergeManager.cancelEncode(workerTask.task)
|
|
|
|
|
|
|
|
res.sendStatus(200)
|
|
|
|
}
|
|
|
|
|
2024-08-01 00:32:51 +02:00
|
|
|
/**
|
|
|
|
* POST: /api/tools/item/:id/embed-metadata
|
|
|
|
* Start audiobook embed task
|
|
|
|
*
|
|
|
|
* @this import('../routers/ApiRouter')
|
|
|
|
*
|
2024-08-11 22:15:34 +02:00
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
2024-08-01 00:32:51 +02:00
|
|
|
*/
|
2022-11-19 20:28:06 +01:00
|
|
|
async embedAudioFileMetadata(req, res) {
|
|
|
|
if (req.libraryItem.isMissing || !req.libraryItem.hasAudioFiles || !req.libraryItem.isBook) {
|
2023-04-02 23:13:18 +02:00
|
|
|
Logger.error(`[ToolsController] Invalid library item`)
|
2024-08-27 00:02:29 +02:00
|
|
|
return res.sendStatus(400)
|
2022-11-19 20:28:06 +01:00
|
|
|
}
|
|
|
|
|
2023-04-02 23:13:18 +02:00
|
|
|
if (this.audioMetadataManager.getIsLibraryItemQueuedOrProcessing(req.libraryItem.id)) {
|
|
|
|
Logger.error(`[ToolsController] Library item (${req.libraryItem.id}) is already in queue or processing`)
|
2024-08-27 00:02:29 +02:00
|
|
|
return res.status(400).send('Library item is already in queue or processing')
|
2023-04-02 23:13:18 +02:00
|
|
|
}
|
|
|
|
|
2023-01-07 22:16:52 +01:00
|
|
|
const options = {
|
|
|
|
forceEmbedChapters: req.query.forceEmbedChapters === '1',
|
|
|
|
backup: req.query.backup === '1'
|
|
|
|
}
|
2024-08-11 23:07:29 +02:00
|
|
|
this.audioMetadataManager.updateMetadataForItem(req.user.id, req.libraryItem, options)
|
2022-11-19 20:28:06 +01:00
|
|
|
res.sendStatus(200)
|
|
|
|
}
|
|
|
|
|
2024-08-01 00:32:51 +02:00
|
|
|
/**
|
|
|
|
* POST: /api/tools/batch/embed-metadata
|
|
|
|
* Start batch audiobook embed task
|
|
|
|
*
|
|
|
|
* @this import('../routers/ApiRouter')
|
|
|
|
*
|
2024-08-11 22:15:34 +02:00
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
2024-08-01 00:32:51 +02:00
|
|
|
*/
|
2023-04-02 23:13:18 +02:00
|
|
|
async batchEmbedMetadata(req, res) {
|
|
|
|
const libraryItemIds = req.body.libraryItemIds || []
|
|
|
|
if (!libraryItemIds.length) {
|
|
|
|
return res.status(400).send('Invalid request payload')
|
|
|
|
}
|
|
|
|
|
|
|
|
const libraryItems = []
|
|
|
|
for (const libraryItemId of libraryItemIds) {
|
2023-09-04 23:33:55 +02:00
|
|
|
const libraryItem = await Database.libraryItemModel.getOldById(libraryItemId)
|
2023-04-02 23:13:18 +02:00
|
|
|
if (!libraryItem) {
|
|
|
|
Logger.error(`[ToolsController] Batch embed metadata library item (${libraryItemId}) not found`)
|
|
|
|
return res.sendStatus(404)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check user can access this library item
|
2024-08-11 23:07:29 +02:00
|
|
|
if (!req.user.checkCanAccessLibraryItem(libraryItem)) {
|
|
|
|
Logger.error(`[ToolsController] Batch embed metadata library item (${libraryItemId}) not accessible to user "${req.user.username}"`)
|
2023-04-02 23:13:18 +02:00
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (libraryItem.isMissing || !libraryItem.hasAudioFiles || !libraryItem.isBook) {
|
|
|
|
Logger.error(`[ToolsController] Batch embed invalid library item (${libraryItemId})`)
|
2024-08-27 00:02:29 +02:00
|
|
|
return res.sendStatus(400)
|
2023-04-02 23:13:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.audioMetadataManager.getIsLibraryItemQueuedOrProcessing(libraryItemId)) {
|
|
|
|
Logger.error(`[ToolsController] Batch embed library item (${libraryItemId}) is already in queue or processing`)
|
2024-08-27 00:02:29 +02:00
|
|
|
return res.status(400).send('Library item is already in queue or processing')
|
2023-04-02 23:13:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
libraryItems.push(libraryItem)
|
|
|
|
}
|
2022-11-19 20:28:06 +01:00
|
|
|
|
2023-04-02 23:13:18 +02:00
|
|
|
const options = {
|
|
|
|
forceEmbedChapters: req.query.forceEmbedChapters === '1',
|
|
|
|
backup: req.query.backup === '1'
|
|
|
|
}
|
2024-08-11 23:07:29 +02:00
|
|
|
this.audioMetadataManager.handleBatchEmbed(req.user.id, libraryItems, options)
|
2023-04-02 23:13:18 +02:00
|
|
|
res.sendStatus(200)
|
|
|
|
}
|
|
|
|
|
2024-08-01 00:32:51 +02:00
|
|
|
/**
|
|
|
|
*
|
2024-08-11 22:15:34 +02:00
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
* @param {NextFunction} next
|
2024-08-01 00:32:51 +02:00
|
|
|
*/
|
2023-08-13 22:10:26 +02:00
|
|
|
async middleware(req, res, next) {
|
2024-08-11 23:07:29 +02:00
|
|
|
if (!req.user.isAdminOrUp) {
|
|
|
|
Logger.error(`[LibraryItemController] Non-root user "${req.user.username}" attempted to access tools route`)
|
2022-11-19 20:28:06 +01:00
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
|
|
|
|
2023-04-02 23:13:18 +02:00
|
|
|
if (req.params.id) {
|
2023-08-20 20:34:03 +02:00
|
|
|
const item = await Database.libraryItemModel.getOldById(req.params.id)
|
2023-08-13 22:10:26 +02:00
|
|
|
if (!item?.media) return res.sendStatus(404)
|
2023-04-02 23:13:18 +02:00
|
|
|
|
|
|
|
// Check user can access this library item
|
2024-08-11 23:07:29 +02:00
|
|
|
if (!req.user.checkCanAccessLibraryItem(item)) {
|
2023-04-02 23:13:18 +02:00
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.libraryItem = item
|
|
|
|
}
|
|
|
|
|
2022-11-19 20:28:06 +01:00
|
|
|
next()
|
|
|
|
}
|
|
|
|
}
|
2024-08-01 00:32:51 +02:00
|
|
|
module.exports = new ToolsController()
|