audiobookshelf/server/controllers/LibraryItemController.js

2037 lines
74 KiB
JavaScript
Raw Normal View History

const { Request, Response, NextFunction } = require('express')
2023-05-28 10:47:28 -05:00
const Path = require('path')
const fs = require('../libs/fsExtra')
const uaParserJs = require('../libs/uaParser')
const Logger = require('../Logger')
const SocketAuthority = require('../SocketAuthority')
2023-07-04 18:14:44 -05:00
const Database = require('../Database')
const zipHelpers = require('../utils/zipHelpers')
2023-07-04 18:14:44 -05:00
const { reqSupportsWebp } = require('../utils/index')
const { ScanResult, AudioMimeType } = require('../utils/constants')
2026-02-13 14:15:18 +02:00
const { getAudioMimeTypeFromExtname, encodeUriPath, sanitizeFilename } = require('../utils/fileUtils')
2023-09-03 17:51:58 -05:00
const LibraryItemScanner = require('../scanner/LibraryItemScanner')
const AudioFileScanner = require('../scanner/AudioFileScanner')
2023-09-06 17:48:50 -05:00
const Scanner = require('../scanner/Scanner')
2026-02-06 22:21:22 +02:00
const Watcher = require('../Watcher')
const libraryItemsBookFilters = require('../utils/queries/libraryItemsBookFilters')
const libraryItemsPodcastFilters = require('../utils/queries/libraryItemsPodcastFilters')
const RssFeedManager = require('../managers/RssFeedManager')
2023-09-06 17:48:50 -05:00
const CacheManager = require('../managers/CacheManager')
const CoverManager = require('../managers/CoverManager')
const ShareManager = require('../managers/ShareManager')
/**
2024-08-11 17:01:25 -05:00
* @typedef RequestUserObject
* @property {import('../models/User')} user
*
2024-08-11 17:01:25 -05:00
* @typedef {Request & RequestUserObject} RequestWithUser
*
* @typedef RequestEntityObject
* @property {import('../models/LibraryItem')} libraryItem
*
* @typedef {RequestWithUser & RequestEntityObject} LibraryItemControllerRequest
*
* @typedef RequestLibraryFileObject
* @property {import('../objects/files/LibraryFile')} libraryFile
*
* @typedef {RequestWithUser & RequestEntityObject & RequestLibraryFileObject} LibraryItemControllerRequestWithFile
*/
2026-02-06 15:01:07 +02:00
/**
* Internal helper to move a single library item to a target library/folder
*
* @param {import('../models/LibraryItem')} libraryItem
* @param {import('../models/Library')} targetLibrary
* @param {import('../models/LibraryFolder')} targetFolder
*/
2026-02-13 14:15:18 +02:00
async function handleMoveLibraryItem(libraryItem, targetLibrary, targetFolder, newItemFolderName = null) {
2026-02-06 15:01:07 +02:00
const oldPath = libraryItem.path
const oldLibraryId = libraryItem.libraryId
// Calculate new paths
2026-02-13 14:15:18 +02:00
const itemFolderName = newItemFolderName || Path.basename(libraryItem.path)
2026-02-06 15:01:07 +02:00
const newPath = Path.join(targetFolder.path, itemFolderName)
const newRelPath = itemFolderName
// Check if destination already exists
const destinationExists = await fs.pathExists(newPath)
if (destinationExists) {
throw new Error(`Destination already exists: ${newPath}`)
}
try {
2026-02-06 22:21:22 +02:00
Watcher.addIgnoreDir(oldPath)
Watcher.addIgnoreDir(newPath)
const oldRelPath = libraryItem.relPath
2026-02-06 15:01:07 +02:00
// Move files on disk
Logger.info(`[LibraryItemController] Moving item "${libraryItem.media.title}" from "${oldPath}" to "${newPath}"`)
await fs.move(oldPath, newPath)
2026-02-06 22:35:03 +02:00
// Update database within a transaction
const transaction = await Database.sequelize.transaction()
try {
// Update library item in database
libraryItem.libraryId = targetLibrary.id
libraryItem.libraryFolderId = targetFolder.id
libraryItem.path = newPath
libraryItem.relPath = newRelPath
libraryItem.isMissing = false
libraryItem.isInvalid = false
libraryItem.isNotConsolidated = libraryItem.checkIsNotConsolidated()
2026-02-06 22:35:03 +02:00
libraryItem.changed('updatedAt', true)
2026-02-06 22:25:44 +02:00
await libraryItem.save({ transaction })
2026-02-06 15:01:07 +02:00
2026-02-06 22:35:03 +02:00
// Update library files paths
if (libraryItem.libraryFiles?.length) {
libraryItem.libraryFiles = libraryItem.libraryFiles.map((lf) => {
if (lf.metadata?.path) {
lf.metadata.path = lf.metadata.path.replace(oldPath, newPath)
2026-02-06 15:01:07 +02:00
}
2026-02-06 22:35:03 +02:00
if (lf.metadata?.relPath) {
lf.metadata.relPath = lf.metadata.relPath.replace(oldRelPath, newRelPath)
2026-02-06 22:21:22 +02:00
}
2026-02-06 22:35:03 +02:00
return lf
2026-02-06 15:01:07 +02:00
})
2026-02-06 22:35:03 +02:00
libraryItem.changed('libraryFiles', true)
await libraryItem.save({ transaction })
2026-02-06 15:01:07 +02:00
}
2026-02-06 22:35:03 +02:00
// Update media file paths (audioFiles, ebookFile for books; podcastEpisodes for podcasts)
if (libraryItem.isBook) {
// Update audioFiles paths
if (libraryItem.media.audioFiles?.length) {
libraryItem.media.audioFiles = libraryItem.media.audioFiles.map((af) => {
if (af.metadata?.path) {
af.metadata.path = af.metadata.path.replace(oldPath, newPath)
}
if (af.metadata?.relPath) {
af.metadata.relPath = af.metadata.relPath.replace(oldRelPath, newRelPath)
}
return af
})
libraryItem.media.changed('audioFiles', true)
2026-02-06 22:21:22 +02:00
}
2026-02-06 22:35:03 +02:00
// Update ebookFile path
if (libraryItem.media.ebookFile?.metadata?.path) {
libraryItem.media.ebookFile.metadata.path = libraryItem.media.ebookFile.metadata.path.replace(oldPath, newPath)
if (libraryItem.media.ebookFile.metadata?.relPath) {
libraryItem.media.ebookFile.metadata.relPath = libraryItem.media.ebookFile.metadata.relPath.replace(oldRelPath, newRelPath)
}
libraryItem.media.changed('ebookFile', true)
}
// Update coverPath
if (libraryItem.media.coverPath) {
libraryItem.media.coverPath = libraryItem.media.coverPath.replace(oldPath, newPath)
2026-02-06 22:21:22 +02:00
}
2026-02-06 22:35:03 +02:00
await libraryItem.media.save({ transaction })
} else if (libraryItem.isPodcast) {
// Update coverPath
if (libraryItem.media.coverPath) {
libraryItem.media.coverPath = libraryItem.media.coverPath.replace(oldPath, newPath)
2026-02-06 22:21:22 +02:00
}
2026-02-06 22:35:03 +02:00
await libraryItem.media.save({ transaction })
// Update podcast episode audio file paths
for (const episode of libraryItem.media.podcastEpisodes || []) {
let episodeUpdated = false
if (episode.audioFile?.metadata?.path) {
episode.audioFile.metadata.path = episode.audioFile.metadata.path.replace(oldPath, newPath)
episodeUpdated = true
}
if (episode.audioFile?.metadata?.relPath) {
episode.audioFile.metadata.relPath = episode.audioFile.metadata.relPath.replace(oldRelPath, newRelPath)
episodeUpdated = true
}
if (episodeUpdated) {
episode.changed('audioFile', true)
await episode.save({ transaction })
}
2026-02-06 15:01:07 +02:00
}
}
2026-02-06 22:35:03 +02:00
// Handle Series and Authors when moving a book
if (libraryItem.isBook) {
// Handle Series
const bookSeries = await Database.bookSeriesModel.findAll({
where: { bookId: libraryItem.media.id },
transaction
})
for (const bs of bookSeries) {
const sourceSeries = await Database.seriesModel.findByPk(bs.seriesId, { transaction })
if (sourceSeries) {
const targetSeries = await Database.seriesModel.findOrCreateByNameAndLibrary(sourceSeries.name, targetLibrary.id, transaction)
// If target series doesn't have a description but source does, copy it
if (!targetSeries.description && sourceSeries.description) {
targetSeries.description = sourceSeries.description
await targetSeries.save({ transaction })
}
2026-02-06 15:01:07 +02:00
2026-02-06 22:35:03 +02:00
// Update link
bs.seriesId = targetSeries.id
await bs.save({ transaction })
2026-02-06 15:01:07 +02:00
2026-02-06 22:35:03 +02:00
// Check if source series is now empty
const sourceSeriesBooksCount = await Database.bookSeriesModel.count({ where: { seriesId: sourceSeries.id }, transaction })
if (sourceSeriesBooksCount === 0) {
Logger.info(`[LibraryItemController] Source series "${sourceSeries.name}" in library ${oldLibraryId} is now empty. Deleting.`)
await sourceSeries.destroy({ transaction })
Database.removeSeriesFromFilterData(oldLibraryId, sourceSeries.id)
2026-02-06 22:25:44 +02:00
2026-02-06 22:35:03 +02:00
SocketAuthority.emitter('series_removed', { id: sourceSeries.id, libraryId: oldLibraryId })
}
2026-02-06 15:01:07 +02:00
}
}
2026-02-06 22:35:03 +02:00
// Handle Authors
const bookAuthors = await Database.bookAuthorModel.findAll({
where: { bookId: libraryItem.media.id },
transaction
})
for (const ba of bookAuthors) {
const sourceAuthor = await Database.authorModel.findByPk(ba.authorId, { transaction })
if (sourceAuthor) {
const targetAuthor = await Database.authorModel.findOrCreateByNameAndLibrary(sourceAuthor.name, targetLibrary.id, transaction)
// Copy description and ASIN if target doesn't have them
let targetAuthorUpdated = false
if (!targetAuthor.description && sourceAuthor.description) {
targetAuthor.description = sourceAuthor.description
targetAuthorUpdated = true
}
if (!targetAuthor.asin && sourceAuthor.asin) {
targetAuthor.asin = sourceAuthor.asin
2026-02-06 15:01:07 +02:00
targetAuthorUpdated = true
}
2026-02-06 22:35:03 +02:00
// Copy image if target doesn't have one
if (!targetAuthor.imagePath && sourceAuthor.imagePath && (await fs.pathExists(sourceAuthor.imagePath))) {
const ext = Path.extname(sourceAuthor.imagePath)
const newImagePath = Path.posix.join(Path.join(global.MetadataPath, 'authors'), targetAuthor.id + ext)
try {
await fs.copy(sourceAuthor.imagePath, newImagePath)
targetAuthor.imagePath = newImagePath
targetAuthorUpdated = true
} catch (err) {
Logger.error(`[LibraryItemController] Failed to copy author image`, err)
}
}
2026-02-06 15:01:07 +02:00
2026-02-06 22:35:03 +02:00
if (targetAuthorUpdated) await targetAuthor.save({ transaction })
2026-02-06 15:01:07 +02:00
2026-02-06 22:35:03 +02:00
// Update link
ba.authorId = targetAuthor.id
await ba.save({ transaction })
// Check if source author is now empty
const sourceAuthorBooksCount = await Database.bookAuthorModel.getCountForAuthor(sourceAuthor.id, transaction)
if (sourceAuthorBooksCount === 0) {
Logger.info(`[LibraryItemController] Source author "${sourceAuthor.name}" in library ${oldLibraryId} is now empty. Deleting.`)
if (sourceAuthor.imagePath) {
await fs.remove(sourceAuthor.imagePath).catch((err) => Logger.error(`[LibraryItemController] Failed to remove source author image`, err))
}
await sourceAuthor.destroy({ transaction })
Database.removeAuthorFromFilterData(oldLibraryId, sourceAuthor.id)
2026-02-06 22:25:44 +02:00
2026-02-06 22:35:03 +02:00
SocketAuthority.emitter('author_removed', { id: sourceAuthor.id, libraryId: oldLibraryId })
}
2026-02-06 15:01:07 +02:00
}
}
}
2026-02-06 22:35:03 +02:00
await transaction.commit()
} catch (dbError) {
if (transaction) await transaction.rollback()
throw dbError
2026-02-06 15:01:07 +02:00
}
// Emit socket events for UI updates
if (oldLibraryId !== targetLibrary.id) {
SocketAuthority.emitter('item_removed', {
id: libraryItem.id,
libraryId: oldLibraryId
})
SocketAuthority.libraryItemEmitter('item_added', libraryItem)
} else {
SocketAuthority.libraryItemEmitter('item_updated', libraryItem)
}
2026-02-06 15:01:07 +02:00
Logger.info(`[LibraryItemController] Successfully moved item "${libraryItem.media.title}" to library "${targetLibrary.name}"`)
} catch (error) {
Logger.error(`[LibraryItemController] Failed to move item "${libraryItem.media.title}"`, error)
// Attempt to rollback file move if database update failed
if (await fs.pathExists(newPath)) {
try {
await fs.move(newPath, oldPath)
Logger.info(`[LibraryItemController] Rolled back file move for item "${libraryItem.media.title}"`)
} catch (rollbackError) {
Logger.error(`[LibraryItemController] Failed to rollback file move`, rollbackError)
}
}
throw error
2026-02-06 22:21:22 +02:00
} finally {
Watcher.removeIgnoreDir(oldPath)
Watcher.removeIgnoreDir(newPath)
2026-02-06 15:01:07 +02:00
}
}
class LibraryItemController {
constructor() {}
/**
* GET: /api/items/:id
* Optional query params:
* ?include=progress,rssfeed,downloads,share
* ?expanded=1
*
* @param {LibraryItemControllerRequest} req
* @param {Response} res
*/
2023-07-17 16:48:46 -05:00
async findOne(req, res) {
const includeEntities = (req.query.include || '').split(',')
if (req.query.expanded == 1) {
const item = req.libraryItem.toOldJSONExpanded()
// Include users media progress
if (includeEntities.includes('progress')) {
const episodeId = req.query.episode || null
item.userMediaProgress = req.user.getOldMediaProgress(item.id, episodeId)
}
if (includeEntities.includes('rssfeed')) {
const feedData = await RssFeedManager.findFeedForEntityId(item.id)
item.rssFeed = feedData?.toOldJSONMinified() || null
}
if (item.mediaType === 'book' && req.user.isAdminOrUp && includeEntities.includes('share')) {
item.mediaItemShare = ShareManager.findByMediaItemId(item.media.id)
}
if (item.mediaType === 'podcast' && includeEntities.includes('downloads')) {
const downloadsInQueue = this.podcastManager.getEpisodeDownloadsInQueue(req.libraryItem.id)
item.episodeDownloadsQueued = downloadsInQueue.map((d) => d.toJSONForClient())
if (this.podcastManager.currentDownload?.libraryItemId === req.libraryItem.id) {
item.episodesDownloading = [this.podcastManager.currentDownload.toJSONForClient()]
}
}
return res.json(item)
}
res.json(req.libraryItem.toOldJSON())
}
/**
* DELETE: /api/items/:id
* Delete library item. Will delete from database and file system if hard delete is requested.
* Optional query params:
* ?hard=1
*
* @this {import('../routers/ApiRouter')}
*
* @param {LibraryItemControllerRequest} req
* @param {Response} res
*/
async delete(req, res) {
const hardDelete = req.query.hard == 1 // Delete from file system
const libraryItemPath = req.libraryItem.path
const mediaItemIds = []
const authorIds = []
const seriesIds = []
if (req.libraryItem.isPodcast) {
mediaItemIds.push(...req.libraryItem.media.podcastEpisodes.map((ep) => ep.id))
} else {
mediaItemIds.push(req.libraryItem.media.id)
if (req.libraryItem.media.authors?.length) {
authorIds.push(...req.libraryItem.media.authors.map((au) => au.id))
}
if (req.libraryItem.media.series?.length) {
seriesIds.push(...req.libraryItem.media.series.map((se) => se.id))
}
}
await this.handleDeleteLibraryItem(req.libraryItem.id, mediaItemIds)
if (hardDelete) {
Logger.info(`[LibraryItemController] Deleting library item from file system at "${libraryItemPath}"`)
await fs.remove(libraryItemPath).catch((error) => {
Logger.error(`[LibraryItemController] Failed to delete library item from file system at "${libraryItemPath}"`, error)
})
}
if (authorIds.length) {
await this.checkRemoveAuthorsWithNoBooks(authorIds)
}
if (seriesIds.length) {
await this.checkRemoveEmptySeries(seriesIds)
}
await Database.resetLibraryIssuesFilterData(req.libraryItem.libraryId)
res.sendStatus(200)
}
static handleDownloadError(error, res) {
if (!res.headersSent) {
if (error.code === 'ENOENT') {
return res.status(404).send('File not found')
} else {
return res.status(500).send('Download failed')
}
}
}
/**
* GET: /api/items/:id/download
* Download library item. Zip file if multiple files.
*
* @param {LibraryItemControllerRequest} req
* @param {Response} res
*/
async download(req, res) {
if (!req.user.canDownload) {
Logger.warn(`User "${req.user.username}" attempted to download without permission`)
return res.sendStatus(403)
}
2024-08-09 16:48:21 -05:00
const libraryItemPath = req.libraryItem.path
const itemTitle = req.libraryItem.media.title
Logger.info(`[LibraryItemController] User "${req.user.username}" requested download for item "${itemTitle}" at "${libraryItemPath}"`)
try {
// If library item is a single file in root dir then no need to zip
if (req.libraryItem.isFile) {
// Express does not set the correct mimetype for m4b files so use our defined mimetypes if available
const audioMimeType = getAudioMimeTypeFromExtname(Path.extname(libraryItemPath))
if (audioMimeType) {
res.setHeader('Content-Type', audioMimeType)
}
await new Promise((resolve, reject) => res.download(libraryItemPath, req.libraryItem.relPath, (error) => (error ? reject(error) : resolve())))
} else {
const filename = `${itemTitle}.zip`
await zipHelpers.zipDirectoryPipe(libraryItemPath, filename, res)
}
Logger.info(`[LibraryItemController] Downloaded item "${itemTitle}" at "${libraryItemPath}"`)
} catch (error) {
Logger.error(`[LibraryItemController] Download failed for item "${itemTitle}" at "${libraryItemPath}"`, error)
LibraryItemController.handleDownloadError(error, res)
}
}
/**
* PATCH: /items/:id/media
* Update media for a library item. Will create new authors & series when necessary
*
* @this {import('../routers/ApiRouter')}
*
* @param {LibraryItemControllerRequest} req
* @param {Response} res
*/
async updateMedia(req, res) {
const mediaPayload = req.body
if (mediaPayload.url) {
await LibraryItemController.prototype.uploadCover.bind(this)(req, res, false)
if (res.writableEnded || res.headersSent) return
}
// Podcast specific
let isPodcastAutoDownloadUpdated = false
if (req.libraryItem.isPodcast) {
if (mediaPayload.autoDownloadEpisodes !== undefined && req.libraryItem.media.autoDownloadEpisodes !== mediaPayload.autoDownloadEpisodes) {
isPodcastAutoDownloadUpdated = true
} else if (mediaPayload.autoDownloadSchedule !== undefined && req.libraryItem.media.autoDownloadSchedule !== mediaPayload.autoDownloadSchedule) {
isPodcastAutoDownloadUpdated = true
}
}
2025-01-05 12:05:01 -06:00
let hasUpdates = (await req.libraryItem.media.updateFromRequest(mediaPayload)) || mediaPayload.url
if (req.libraryItem.isBook && Array.isArray(mediaPayload.metadata?.series)) {
const seriesUpdateData = await req.libraryItem.media.updateSeriesFromRequest(mediaPayload.metadata.series, req.libraryItem.libraryId)
if (seriesUpdateData?.seriesRemoved.length) {
// Check remove empty series
Logger.debug(`[LibraryItemController] Series were removed from book. Check if series are now empty.`)
await this.checkRemoveEmptySeries(seriesUpdateData.seriesRemoved.map((se) => se.id))
}
if (seriesUpdateData?.seriesAdded.length) {
// Add series to filter data
seriesUpdateData.seriesAdded.forEach((se) => {
Database.addSeriesToFilterData(req.libraryItem.libraryId, se.name, se.id)
})
}
if (seriesUpdateData?.hasUpdates) {
hasUpdates = true
}
}
2025-01-05 12:05:01 -06:00
if (req.libraryItem.isBook && Array.isArray(mediaPayload.metadata?.authors)) {
const authorNames = mediaPayload.metadata.authors.map((au) => (typeof au.name === 'string' ? au.name.trim() : null)).filter((au) => au)
const authorUpdateData = await req.libraryItem.media.updateAuthorsFromRequest(authorNames, req.libraryItem.libraryId)
if (authorUpdateData?.authorsRemoved.length) {
// Check remove empty authors
Logger.debug(`[LibraryItemController] Authors were removed from book. Check if authors are now empty.`)
await this.checkRemoveAuthorsWithNoBooks(authorUpdateData.authorsRemoved.map((au) => au.id))
hasUpdates = true
}
if (authorUpdateData?.authorsAdded.length) {
// Add authors to filter data
authorUpdateData.authorsAdded.forEach((au) => {
Database.addAuthorToFilterData(req.libraryItem.libraryId, au.name, au.id)
})
hasUpdates = true
}
}
if (hasUpdates) {
req.libraryItem.changed('updatedAt', true)
await req.libraryItem.save()
2025-01-05 12:05:01 -06:00
await req.libraryItem.saveMetadataFile()
if (isPodcastAutoDownloadUpdated) {
this.cronManager.checkUpdatePodcastCron(req.libraryItem)
}
Logger.debug(`[LibraryItemController] Updated library item media ${req.libraryItem.media.title}`)
SocketAuthority.libraryItemEmitter('item_updated', req.libraryItem)
}
2022-03-14 08:12:28 -05:00
res.json({
updated: hasUpdates,
libraryItem: req.libraryItem.toOldJSON()
2022-03-14 08:12:28 -05:00
})
}
/**
* POST: /api/items/:id/cover
*
* @param {LibraryItemControllerRequest} req
* @param {Response} res
* @param {boolean} [updateAndReturnJson=true] - Allows the function to be used for both direct API calls and internally
*/
async uploadCover(req, res, updateAndReturnJson = true) {
if (!req.user.canUpload) {
Logger.warn(`User "${req.user.username}" attempted to upload a cover without permission`)
return res.sendStatus(403)
}
let result = null
if (req.body?.url) {
Logger.debug(`[LibraryItemController] Requesting download cover from url "${req.body.url}"`)
result = await CoverManager.downloadCoverFromUrlNew(req.body.url, req.libraryItem.id, req.libraryItem.isFile ? null : req.libraryItem.path)
} else if (req.files?.cover) {
Logger.debug(`[LibraryItemController] Handling uploaded cover`)
result = await CoverManager.uploadCover(req.libraryItem, req.files.cover)
} else {
return res.status(400).send('Invalid request no file or url')
}
if (result?.error) {
return res.status(400).send(result.error)
} else if (!result?.cover) {
return res.status(500).send('Unknown error occurred')
}
req.libraryItem.media.coverPath = result.cover
req.libraryItem.media.changed('coverPath', true)
await req.libraryItem.media.save()
if (updateAndReturnJson) {
// client uses updatedAt timestamp in URL to force refresh cover
req.libraryItem.changed('updatedAt', true)
await req.libraryItem.save()
SocketAuthority.libraryItemEmitter('item_updated', req.libraryItem)
res.json({
success: true,
cover: result.cover
})
}
}
/**
* PATCH: /api/items/:id/cover
*
* @param {LibraryItemControllerRequest} req
* @param {Response} res
*/
async updateCover(req, res) {
if (!req.body.cover) {
return res.status(400).send('Invalid request no cover path')
}
const validationResult = await CoverManager.validateCoverPath(req.body.cover, req.libraryItem)
if (validationResult.error) {
return res.status(500).send(validationResult.error)
}
if (validationResult.updated) {
req.libraryItem.media.coverPath = validationResult.cover
req.libraryItem.media.changed('coverPath', true)
await req.libraryItem.media.save()
// client uses updatedAt timestamp in URL to force refresh cover
req.libraryItem.changed('updatedAt', true)
await req.libraryItem.save()
SocketAuthority.libraryItemEmitter('item_updated', req.libraryItem)
}
res.json({
success: true,
cover: validationResult.cover
})
}
/**
* DELETE: /api/items/:id/cover
*
* @param {LibraryItemControllerRequest} req
* @param {Response} res
*/
async removeCover(req, res) {
if (req.libraryItem.media.coverPath) {
req.libraryItem.media.coverPath = null
req.libraryItem.media.changed('coverPath', true)
await req.libraryItem.media.save()
// client uses updatedAt timestamp in URL to force refresh cover
req.libraryItem.changed('updatedAt', true)
await req.libraryItem.save()
await CacheManager.purgeCoverCache(req.libraryItem.id)
SocketAuthority.libraryItemEmitter('item_updated', req.libraryItem)
}
res.sendStatus(200)
}
/**
* GET: /api/items/:id/cover
*
* @param {LibraryItemControllerRequest} req
* @param {Response} res
*/
async getCover(req, res) {
const {
query: { width, height, format, raw }
} = req
if (req.query.ts) res.set('Cache-Control', 'private, max-age=86400')
const libraryItemId = req.params.id
if (!libraryItemId) {
return res.sendStatus(400)
}
if (raw) {
const coverPath = await Database.libraryItemModel.getCoverPath(libraryItemId)
if (!coverPath || !(await fs.pathExists(coverPath))) {
return res.sendStatus(404)
}
// any value
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
if (global.XAccel) {
const encodedURI = encodeUriPath(global.XAccel + coverPath)
Logger.debug(`Use X-Accel to serve static file ${encodedURI}`)
return res.status(204).header({ 'X-Accel-Redirect': encodedURI }).send()
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
}
return res.sendFile(coverPath)
}
const options = {
format: format || (reqSupportsWebp(req) ? 'webp' : 'jpeg'),
height: height ? parseInt(height) : null,
width: width ? parseInt(width) : null
}
return CacheManager.handleCoverCache(res, libraryItemId, options)
}
/**
* POST: /api/items/:id/play
*
* @this {import('../routers/ApiRouter')}
*
* @param {LibraryItemControllerRequest} req
* @param {Response} res
*/
startPlaybackSession(req, res) {
if (!req.libraryItem.hasAudioTracks) {
Logger.error(`[LibraryItemController] startPlaybackSession cannot playback ${req.libraryItem.id}`)
return res.sendStatus(404)
}
this.playbackSessionManager.startSessionRequest(req, res, null)
}
/**
* POST: /api/items/:id/play/:episodeId
*
* @this {import('../routers/ApiRouter')}
*
* @param {LibraryItemControllerRequest} req
* @param {Response} res
*/
startEpisodePlaybackSession(req, res) {
if (!req.libraryItem.isPodcast) {
Logger.error(`[LibraryItemController] startEpisodePlaybackSession invalid media type ${req.libraryItem.id}`)
return res.sendStatus(400)
}
const episodeId = req.params.episodeId
if (!req.libraryItem.media.podcastEpisodes.some((ep) => ep.id === episodeId)) {
Logger.error(`[LibraryItemController] startPlaybackSession episode ${episodeId} not found for item ${req.libraryItem.id}`)
return res.sendStatus(404)
}
this.playbackSessionManager.startSessionRequest(req, res, episodeId)
}
/**
* PATCH: /api/items/:id/tracks
*
* @param {LibraryItemControllerRequest} req
* @param {Response} res
*/
async updateTracks(req, res) {
const orderedFileData = req.body?.orderedFileData
if (!req.libraryItem.isBook) {
Logger.error(`[LibraryItemController] updateTracks invalid media type ${req.libraryItem.id}`)
return res.sendStatus(400)
}
if (!Array.isArray(orderedFileData) || !orderedFileData.length) {
Logger.error(`[LibraryItemController] updateTracks invalid orderedFileData ${req.libraryItem.id}`)
return res.sendStatus(400)
}
// Ensure that each orderedFileData has a valid ino and is in the book audioFiles
if (orderedFileData.some((fileData) => !fileData?.ino || !req.libraryItem.media.audioFiles.some((af) => af.ino === fileData.ino))) {
Logger.error(`[LibraryItemController] updateTracks invalid orderedFileData ${req.libraryItem.id}`)
return res.sendStatus(400)
}
let index = 1
const updatedAudioFiles = orderedFileData.map((fileData) => {
const audioFile = req.libraryItem.media.audioFiles.find((af) => af.ino === fileData.ino)
audioFile.manuallyVerified = true
audioFile.exclude = !!fileData.exclude
if (audioFile.exclude) {
audioFile.index = -1
} else {
audioFile.index = index++
}
return audioFile
})
updatedAudioFiles.sort((a, b) => a.index - b.index)
req.libraryItem.media.audioFiles = updatedAudioFiles
req.libraryItem.media.changed('audioFiles', true)
await req.libraryItem.media.save()
SocketAuthority.libraryItemEmitter('item_updated', req.libraryItem)
res.json(req.libraryItem.toOldJSON())
}
/**
* POST /api/items/:id/match
*
* @param {LibraryItemControllerRequest} req
* @param {Response} res
*/
async match(req, res) {
const reqBody = req.body || {}
const options = {}
const matchOptions = ['provider', 'title', 'author', 'isbn', 'asin']
for (const key of matchOptions) {
if (reqBody[key] && typeof reqBody[key] === 'string') {
options[key] = reqBody[key]
}
}
if (reqBody.overrideCover !== undefined) {
options.overrideCover = !!reqBody.overrideCover
}
if (reqBody.overrideDetails !== undefined) {
options.overrideDetails = !!reqBody.overrideDetails
}
2025-01-05 12:05:01 -06:00
const matchResult = await Scanner.quickMatchLibraryItem(this, req.libraryItem, options)
res.json(matchResult)
}
2026-02-14 21:57:54 +02:00
/**
* POST /api/items/:id/reset-metadata
*
* @param {LibraryItemControllerRequest} req
* @param {Response} res
*/
async resetMetadata(req, res) {
if (!req.user.canUpdate) {
Logger.warn(`[LibraryItemController] User "${req.user.username}" attempted to reset metadata without permission`)
return res.sendStatus(403)
}
if (global.MetadataPath) {
const metadataPath = Path.join(global.MetadataPath, 'items', req.libraryItem.id, 'metadata.json')
if (await fs.pathExists(metadataPath)) {
Logger.info(`[LibraryItemController] Removing metadata file at "${metadataPath}"`)
await fs.remove(metadataPath)
}
}
if (req.libraryItem.path && !req.libraryItem.isFile) {
const localMetadataPath = Path.join(req.libraryItem.path, 'metadata.json')
if (await fs.pathExists(localMetadataPath)) {
Logger.info(`[LibraryItemController] Removing local metadata file at "${localMetadataPath}"`)
await fs.remove(localMetadataPath)
}
}
// Clear cover path to force re-scan of cover
if (req.libraryItem.media.coverPath) {
req.libraryItem.media.coverPath = null
await req.libraryItem.media.save()
}
// Trigger a scan ensuring we don't rely on cache/timestamps if possible
// scanLibraryItem checks mtime but since we deleted metadata.json which might have been the source,
// the "comparison" logic in BookScanner should now fallback to other sources (tags/folder).
// If those sources yield different data than DB, it updates.
const result = await LibraryItemScanner.scanLibraryItem(req.libraryItem.id)
// Respond with updated item
await req.libraryItem.reload()
res.json(req.libraryItem.toOldJSONExpanded())
}
/**
* POST: /api/items/batch/delete
* Batch delete library items. Will delete from database and file system if hard delete is requested.
* Optional query params:
* ?hard=1
*
* @this {import('../routers/ApiRouter')}
*
* @param {RequestWithUser} req
* @param {Response} res
*/
async batchDelete(req, res) {
if (!req.user.canDelete) {
Logger.warn(`[LibraryItemController] User "${req.user.username}" attempted to delete without permission`)
return res.sendStatus(403)
}
const hardDelete = req.query.hard == 1 // Delete files from filesystem
const { libraryItemIds } = req.body
if (!libraryItemIds?.length || !Array.isArray(libraryItemIds)) {
return res.status(400).send('Invalid request body')
}
const itemsToDelete = await Database.libraryItemModel.findAllExpandedWhere({
id: libraryItemIds
})
if (!itemsToDelete.length) {
return res.sendStatus(404)
}
const libraryId = itemsToDelete[0].libraryId
for (const libraryItem of itemsToDelete) {
const libraryItemPath = libraryItem.path
Logger.info(`[LibraryItemController] (${hardDelete ? 'Hard' : 'Soft'}) deleting Library Item "${libraryItem.media.title}" with id "${libraryItem.id}"`)
const mediaItemIds = []
const seriesIds = []
const authorIds = []
if (libraryItem.isPodcast) {
mediaItemIds.push(...libraryItem.media.podcastEpisodes.map((ep) => ep.id))
} else {
mediaItemIds.push(libraryItem.media.id)
if (libraryItem.media.series?.length) {
seriesIds.push(...libraryItem.media.series.map((se) => se.id))
}
if (libraryItem.media.authors?.length) {
authorIds.push(...libraryItem.media.authors.map((au) => au.id))
}
}
await this.handleDeleteLibraryItem(libraryItem.id, mediaItemIds)
if (hardDelete) {
Logger.info(`[LibraryItemController] Deleting library item from file system at "${libraryItemPath}"`)
await fs.remove(libraryItemPath).catch((error) => {
Logger.error(`[LibraryItemController] Failed to delete library item from file system at "${libraryItemPath}"`, error)
})
}
if (seriesIds.length) {
await this.checkRemoveEmptySeries(seriesIds)
}
if (authorIds.length) {
await this.checkRemoveAuthorsWithNoBooks(authorIds)
}
}
await Database.resetLibraryIssuesFilterData(libraryId)
res.sendStatus(200)
}
/**
* POST: /api/items/batch/update
*
* @this {import('../routers/ApiRouter')}
*
* @param {RequestWithUser} req
* @param {Response} res
*/
async batchUpdate(req, res) {
const updatePayloads = req.body
if (!Array.isArray(updatePayloads) || !updatePayloads.length) {
Logger.error(`[LibraryItemController] Batch update failed. Invalid payload`)
return res.sendStatus(400)
}
// Ensure that each update payload has a unique library item id
const libraryItemIds = [...new Set(updatePayloads.map((up) => up?.id).filter((id) => id))]
if (!libraryItemIds.length || libraryItemIds.length !== updatePayloads.length) {
Logger.error(`[LibraryItemController] Batch update failed. Each update payload must have a unique library item id`)
return res.sendStatus(400)
}
// Get all library items to update
const libraryItems = await Database.libraryItemModel.findAllExpandedWhere({
id: libraryItemIds
})
if (updatePayloads.length !== libraryItems.length) {
Logger.error(`[LibraryItemController] Batch update failed. Not all library items found`)
return res.sendStatus(404)
}
let itemsUpdated = 0
const seriesIdsRemoved = []
const authorIdsRemoved = []
for (const updatePayload of updatePayloads) {
const mediaPayload = updatePayload.mediaPayload
const libraryItem = libraryItems.find((li) => li.id === updatePayload.id)
2025-01-05 12:05:01 -06:00
let hasUpdates = await libraryItem.media.updateFromRequest(mediaPayload)
2025-01-05 12:05:01 -06:00
if (libraryItem.isBook && Array.isArray(mediaPayload.metadata?.series)) {
const seriesUpdateData = await libraryItem.media.updateSeriesFromRequest(mediaPayload.metadata.series, libraryItem.libraryId)
if (seriesUpdateData?.seriesRemoved.length) {
seriesIdsRemoved.push(...seriesUpdateData.seriesRemoved.map((se) => se.id))
}
if (seriesUpdateData?.seriesAdded.length) {
seriesUpdateData.seriesAdded.forEach((se) => {
Database.addSeriesToFilterData(libraryItem.libraryId, se.name, se.id)
})
}
2025-01-05 12:05:01 -06:00
if (seriesUpdateData?.hasUpdates) {
hasUpdates = true
}
}
if (libraryItem.isBook && Array.isArray(mediaPayload.metadata?.authors)) {
const authorNames = mediaPayload.metadata.authors.map((au) => (typeof au.name === 'string' ? au.name.trim() : null)).filter((au) => au)
const authorUpdateData = await libraryItem.media.updateAuthorsFromRequest(authorNames, libraryItem.libraryId)
if (authorUpdateData?.authorsRemoved.length) {
authorIdsRemoved.push(...authorUpdateData.authorsRemoved.map((au) => au.id))
hasUpdates = true
}
if (authorUpdateData?.authorsAdded.length) {
authorUpdateData.authorsAdded.forEach((au) => {
Database.addAuthorToFilterData(libraryItem.libraryId, au.name, au.id)
})
hasUpdates = true
}
}
if (hasUpdates) {
libraryItem.changed('updatedAt', true)
await libraryItem.save()
2025-01-05 12:05:01 -06:00
await libraryItem.saveMetadataFile()
Logger.debug(`[LibraryItemController] Updated library item media "${libraryItem.media.title}"`)
SocketAuthority.libraryItemEmitter('item_updated', libraryItem)
itemsUpdated++
}
}
if (seriesIdsRemoved.length) {
await this.checkRemoveEmptySeries(seriesIdsRemoved)
}
if (authorIdsRemoved.length) {
await this.checkRemoveAuthorsWithNoBooks(authorIdsRemoved)
}
res.json({
success: true,
updates: itemsUpdated
})
}
/**
* POST: /api/items/batch/get
*
* @param {RequestWithUser} req
* @param {Response} res
*/
async batchGet(req, res) {
2022-12-12 17:36:53 -06:00
const libraryItemIds = req.body.libraryItemIds || []
if (!libraryItemIds.length) {
return res.status(403).send('Invalid payload')
}
const libraryItems = await Database.libraryItemModel.findAllExpandedWhere({
id: libraryItemIds
})
res.json({
libraryItems: libraryItems.map((li) => li.toOldJSONExpanded())
})
}
/**
* POST: /api/items/batch/quickmatch
*
* @param {RequestWithUser} req
* @param {Response} res
*/
async batchQuickMatch(req, res) {
if (!req.user.isAdminOrUp) {
Logger.warn(`Non-admin user "${req.user.username}" other than admin attempted to batch quick match library items`)
return res.sendStatus(403)
}
2023-05-27 14:51:03 -05:00
let itemsUpdated = 0
let itemsUnmatched = 0
2023-05-27 14:51:03 -05:00
if (!req.body.libraryItemIds?.length) {
return res.sendStatus(400)
}
2023-05-27 14:51:03 -05:00
const libraryItems = await Database.libraryItemModel.findAllExpandedWhere({
id: req.body.libraryItemIds
})
2023-05-27 14:51:03 -05:00
if (!libraryItems?.length) {
return res.sendStatus(400)
}
res.sendStatus(200)
const reqBodyOptions = req.body.options || {}
const options = {}
if (reqBodyOptions.provider && typeof reqBodyOptions.provider === 'string') {
options.provider = reqBodyOptions.provider
}
if (reqBodyOptions.overrideCover !== undefined) {
options.overrideCover = !!reqBodyOptions.overrideCover
}
if (reqBodyOptions.overrideDetails !== undefined) {
options.overrideDetails = !!reqBodyOptions.overrideDetails
}
2023-05-27 14:51:03 -05:00
for (const libraryItem of libraryItems) {
2025-01-05 12:05:01 -06:00
const matchResult = await Scanner.quickMatchLibraryItem(this, libraryItem, options)
if (matchResult.updated) {
itemsUpdated++
} else if (matchResult.warning) {
itemsUnmatched++
}
2022-09-23 19:37:30 +01:00
}
2023-05-27 14:51:03 -05:00
const result = {
2022-09-23 19:37:30 +01:00
success: itemsUpdated > 0,
updates: itemsUpdated,
unmatched: itemsUnmatched
}
SocketAuthority.clientEmitter(req.user.id, 'batch_quickmatch_complete', result)
}
/**
* POST: /api/items/batch/scan
*
* @param {RequestWithUser} req
* @param {Response} res
*/
2023-05-27 14:51:03 -05:00
async batchScan(req, res) {
if (!req.user.isAdminOrUp) {
Logger.warn(`Non-admin user "${req.user.username}" other than admin attempted to batch scan library items`)
2023-05-27 14:51:03 -05:00
return res.sendStatus(403)
}
if (!req.body.libraryItemIds?.length) {
return res.sendStatus(400)
}
const libraryItems = await Database.libraryItemModel.findAll({
where: {
id: req.body.libraryItemIds
},
attributes: ['id', 'libraryId', 'isFile']
})
2023-05-27 14:51:03 -05:00
if (!libraryItems?.length) {
return res.sendStatus(400)
}
res.sendStatus(200)
const libraryId = libraryItems[0].libraryId
2023-05-27 14:51:03 -05:00
for (const libraryItem of libraryItems) {
if (libraryItem.isFile) {
Logger.warn(`[LibraryItemController] Re-scanning file library items not yet supported`)
} else {
await LibraryItemScanner.scanLibraryItem(libraryItem.id)
2023-05-27 14:51:03 -05:00
}
}
await Database.resetLibraryIssuesFilterData(libraryId)
2023-05-27 14:51:03 -05:00
}
2026-02-06 14:51:54 +02:00
/**
* POST: /api/items/batch/move
* Move multiple library items to a different library
*
* @this {import('../routers/ApiRouter')}
*
* @param {RequestWithUser} req
* @param {Response} res
*/
async batchMove(req, res) {
if (!req.user.canDelete) {
Logger.warn(`[LibraryItemController] User "${req.user.username}" attempted to batch move items without permission`)
return res.sendStatus(403)
}
const { libraryItemIds, targetLibraryId, targetFolderId } = req.body
if (!libraryItemIds?.length || !Array.isArray(libraryItemIds)) {
return res.status(400).send('libraryItemIds must be an array')
}
if (!targetLibraryId) {
return res.status(400).send('targetLibraryId is required')
}
const targetLibrary = await Database.libraryModel.findByIdWithFolders(targetLibraryId)
if (!targetLibrary) {
return res.status(404).send('Target library not found')
}
let targetFolder = null
if (targetFolderId) {
targetFolder = targetLibrary.libraryFolders.find((f) => f.id === targetFolderId)
if (!targetFolder) {
return res.status(400).send('Target folder not found in library')
}
} else {
targetFolder = targetLibrary.libraryFolders[0]
}
if (!targetFolder) {
return res.status(400).send('Target library has no folders')
}
const libraryItems = await Database.libraryItemModel.findAllExpandedWhere({
id: libraryItemIds
})
if (!libraryItems.length) {
return res.sendStatus(404)
}
let successCount = 0
let failCount = 0
const errors = []
2026-02-06 22:25:44 +02:00
const sourceLibraryIds = new Set()
2026-02-06 14:51:54 +02:00
for (const libraryItem of libraryItems) {
try {
if (libraryItem.libraryId === targetLibrary.id) {
Logger.warn(`[LibraryItemController] Item "${libraryItem.media.title}" is already in library ${targetLibrary.id}`)
continue
}
const sourceLibrary = await Database.libraryModel.findByPk(libraryItem.libraryId)
2026-02-06 22:25:44 +02:00
if (!sourceLibrary) {
Logger.error(`[LibraryItemController] Source library not found for item ${libraryItem.id}`)
failCount++
errors.push({ id: libraryItem.id, error: 'Source library not found' })
continue
}
sourceLibraryIds.add(sourceLibrary.id)
2026-02-06 14:51:54 +02:00
if (sourceLibrary.mediaType !== targetLibrary.mediaType) {
2026-02-06 22:25:44 +02:00
Logger.warn(`[LibraryItemController] Cannot move ${sourceLibrary.mediaType} to ${targetLibrary.mediaType} library`)
failCount++
errors.push({ id: libraryItem.id, error: 'Incompatible media type' })
continue
2026-02-06 14:51:54 +02:00
}
2026-02-06 22:35:03 +02:00
await handleMoveLibraryItem(libraryItem, targetLibrary, targetFolder)
successCount++
2026-02-06 22:25:44 +02:00
} catch (error) {
2026-02-06 14:51:54 +02:00
failCount++
2026-02-06 22:35:03 +02:00
errors.push({ id: libraryItem.id, error: error.message || 'Failed to move item' })
2026-02-06 14:51:54 +02:00
}
}
2026-02-06 22:25:44 +02:00
// Reset filter data and clear caches once after batch move
for (const sourceLibraryId of sourceLibraryIds) {
await Database.resetLibraryIssuesFilterData(sourceLibraryId)
if (Database.libraryFilterData[sourceLibraryId]) delete Database.libraryFilterData[sourceLibraryId]
}
await Database.resetLibraryIssuesFilterData(targetLibrary.id)
if (Database.libraryFilterData[targetLibrary.id]) delete Database.libraryFilterData[targetLibrary.id]
const firstItem = libraryItems[0]
if (firstItem.isBook) {
libraryItemsBookFilters.clearCountCache('batch_move_items')
} else if (firstItem.isPodcast) {
libraryItemsPodcastFilters.clearCountCache('podcast', 'batch_move_items')
}
2026-02-06 14:51:54 +02:00
res.json({
success: true,
successCount,
failCount,
errors
})
}
/**
* POST: /api/items/:id/scan
*
* @param {LibraryItemControllerRequest} req
* @param {Response} res
*/
async scan(req, res) {
if (!req.user.isAdminOrUp) {
Logger.error(`[LibraryItemController] Non-admin user "${req.user.username}" attempted to scan library item`)
return res.sendStatus(403)
}
if (req.libraryItem.isFile) {
Logger.error(`[LibraryItemController] Re-scanning file library items not yet supported`)
return res.sendStatus(500)
}
const result = await LibraryItemScanner.scanLibraryItem(req.libraryItem.id)
await Database.resetLibraryIssuesFilterData(req.libraryItem.libraryId)
res.json({
result: Object.keys(ScanResult).find((key) => ScanResult[key] == result)
})
}
/**
* GET: /api/items/:id/metadata-object
*
* @param {LibraryItemControllerRequest} req
* @param {Response} res
*/
getMetadataObject(req, res) {
if (!req.user.isAdminOrUp) {
Logger.error(`[LibraryItemController] Non-admin user "${req.user.username}" attempted to get metadata object`)
return res.sendStatus(403)
}
if (req.libraryItem.isMissing || !req.libraryItem.isBook || !req.libraryItem.media.includedAudioFiles.length) {
Logger.error(`[LibraryItemController] getMetadataObject: Invalid library item "${req.libraryItem.media.title}"`)
return res.sendStatus(400)
}
res.json(this.audioMetadataManager.getMetadataObjectForApi(req.libraryItem))
}
/**
* POST: /api/items/:id/chapters
*
* @param {LibraryItemControllerRequest} req
* @param {Response} res
*/
async updateMediaChapters(req, res) {
if (!req.user.canUpdate) {
Logger.error(`[LibraryItemController] User "${req.user.username}" attempted to update chapters with invalid permissions`)
return res.sendStatus(403)
}
if (req.libraryItem.isMissing || !req.libraryItem.isBook || !req.libraryItem.media.hasAudioTracks) {
Logger.error(`[LibraryItemController] Invalid library item`)
return res.sendStatus(500)
}
if (!Array.isArray(req.body.chapters) || req.body.chapters.some((c) => !c.title || typeof c.title !== 'string' || c.start === undefined || typeof c.start !== 'number' || c.end === undefined || typeof c.end !== 'number')) {
Logger.error(`[LibraryItemController] Invalid payload`)
return res.sendStatus(400)
}
const chapters = req.body.chapters || []
let hasUpdates = false
if (chapters.length !== req.libraryItem.media.chapters.length) {
req.libraryItem.media.chapters = chapters.map((c, index) => {
return {
id: index,
title: c.title,
start: c.start,
end: c.end
}
})
hasUpdates = true
} else {
for (const [index, chapter] of chapters.entries()) {
const currentChapter = req.libraryItem.media.chapters[index]
if (currentChapter.title !== chapter.title || currentChapter.start !== chapter.start || currentChapter.end !== chapter.end) {
currentChapter.title = chapter.title
currentChapter.start = chapter.start
currentChapter.end = chapter.end
hasUpdates = true
}
}
}
if (hasUpdates) {
req.libraryItem.media.changed('chapters', true)
await req.libraryItem.media.save()
2025-01-05 12:05:01 -06:00
await req.libraryItem.saveMetadataFile()
SocketAuthority.libraryItemEmitter('item_updated', req.libraryItem)
}
res.json({
success: true,
updated: hasUpdates
})
}
2023-06-25 16:16:11 -05:00
/**
* GET: /api/items/:id/ffprobe/:fileid
2023-06-25 16:16:11 -05:00
* FFProbe JSON result from audio file
*
* @param {LibraryItemControllerRequest} req
* @param {Response} res
2023-06-25 16:16:11 -05:00
*/
async getFFprobeData(req, res) {
if (!req.user.isAdminOrUp) {
Logger.error(`[LibraryItemController] Non-admin user "${req.user.username}" attempted to get ffprobe data`)
2023-06-25 16:16:11 -05:00
return res.sendStatus(403)
}
const audioFile = req.libraryItem.getAudioFileWithIno(req.params.fileid)
if (!audioFile) {
2023-06-25 16:16:11 -05:00
Logger.error(`[LibraryItemController] Audio file not found with inode value ${req.params.fileid}`)
return res.sendStatus(404)
}
const ffprobeData = await AudioFileScanner.probeAudioFile(audioFile.metadata.path)
2023-06-25 16:16:11 -05:00
res.json(ffprobeData)
}
/**
* GET api/items/:id/file/:fileid
*
* @param {LibraryItemControllerRequestWithFile} req
* @param {Response} res
*/
async getLibraryFile(req, res) {
const libraryFile = req.libraryFile
if (global.XAccel) {
const encodedURI = encodeUriPath(global.XAccel + libraryFile.metadata.path)
Logger.debug(`Use X-Accel to serve static file ${encodedURI}`)
return res.status(204).header({ 'X-Accel-Redirect': encodedURI }).send()
}
// Express does not set the correct mimetype for m4b files so use our defined mimetypes if available
const audioMimeType = getAudioMimeTypeFromExtname(Path.extname(libraryFile.metadata.path))
if (audioMimeType) {
res.setHeader('Content-Type', audioMimeType)
}
res.sendFile(libraryFile.metadata.path)
}
/**
* DELETE api/items/:id/file/:fileid
*
* @param {LibraryItemControllerRequestWithFile} req
* @param {Response} res
*/
async deleteLibraryFile(req, res) {
const libraryFile = req.libraryFile
Logger.info(`[LibraryItemController] User "${req.user.username}" requested file delete at "${libraryFile.metadata.path}"`)
await fs.remove(libraryFile.metadata.path).catch((error) => {
Logger.error(`[LibraryItemController] Failed to delete library file at "${libraryFile.metadata.path}"`, error)
})
req.libraryItem.libraryFiles = req.libraryItem.libraryFiles.filter((lf) => lf.ino !== req.params.fileid)
req.libraryItem.changed('libraryFiles', true)
if (req.libraryItem.isBook) {
if (req.libraryItem.media.audioFiles.some((af) => af.ino === req.params.fileid)) {
req.libraryItem.media.audioFiles = req.libraryItem.media.audioFiles.filter((af) => af.ino !== req.params.fileid)
req.libraryItem.media.changed('audioFiles', true)
} else if (req.libraryItem.media.ebookFile?.ino === req.params.fileid) {
req.libraryItem.media.ebookFile = null
req.libraryItem.media.changed('ebookFile', true)
}
if (!req.libraryItem.media.hasMediaFiles) {
req.libraryItem.isMissing = true
}
} else if (req.libraryItem.media.podcastEpisodes.some((ep) => ep.audioFile.ino === req.params.fileid)) {
const episodeToRemove = req.libraryItem.media.podcastEpisodes.find((ep) => ep.audioFile.ino === req.params.fileid)
// Remove episode from all playlists
await Database.playlistModel.removeMediaItemsFromPlaylists([episodeToRemove.id])
// Remove episode media progress
const numProgressRemoved = await Database.mediaProgressModel.destroy({
where: {
mediaItemId: episodeToRemove.id
}
})
if (numProgressRemoved > 0) {
Logger.info(`[LibraryItemController] Removed media progress for episode ${episodeToRemove.id}`)
}
// Remove episode
await episodeToRemove.destroy()
req.libraryItem.media.podcastEpisodes = req.libraryItem.media.podcastEpisodes.filter((ep) => ep.audioFile.ino !== req.params.fileid)
}
if (req.libraryItem.media.changed()) {
await req.libraryItem.media.save()
}
await req.libraryItem.save()
SocketAuthority.libraryItemEmitter('item_updated', req.libraryItem)
res.sendStatus(200)
}
/**
* GET api/items/:id/file/:fileid/download
* Same as GET api/items/:id/file/:fileid but allows logging and restricting downloads
*
* @param {LibraryItemControllerRequestWithFile} req
* @param {Response} res
*/
async downloadLibraryFile(req, res) {
const libraryFile = req.libraryFile
const ua = uaParserJs(req.headers['user-agent'])
if (!req.user.canDownload) {
Logger.error(`[LibraryItemController] User "${req.user.username}" without download permission attempted to download file "${libraryFile.metadata.path}"`)
return res.sendStatus(403)
}
Logger.info(`[LibraryItemController] User "${req.user.username}" requested download for item "${req.libraryItem.media.title}" file at "${libraryFile.metadata.path}"`)
if (global.XAccel) {
const encodedURI = encodeUriPath(global.XAccel + libraryFile.metadata.path)
Logger.debug(`Use X-Accel to serve static file ${encodedURI}`)
return res.status(204).header({ 'X-Accel-Redirect': encodedURI }).send()
}
// Express does not set the correct mimetype for m4b files so use our defined mimetypes if available
let audioMimeType = getAudioMimeTypeFromExtname(Path.extname(libraryFile.metadata.path))
if (audioMimeType) {
// Work-around for Apple devices mishandling Content-Type on mobile browsers:
// https://github.com/advplyr/audiobookshelf/issues/3310
// We actually need to check for Webkit on Apple mobile devices because this issue impacts all browsers on iOS/iPadOS/etc, not just Safari.
const isAppleMobileBrowser = ua.device.vendor === 'Apple' && ua.device.type === 'mobile' && ua.engine.name === 'WebKit'
if (isAppleMobileBrowser && audioMimeType === AudioMimeType.M4B) {
audioMimeType = 'audio/m4b'
}
res.setHeader('Content-Type', audioMimeType)
}
try {
await new Promise((resolve, reject) => res.download(libraryFile.metadata.path, libraryFile.metadata.filename, (error) => (error ? reject(error) : resolve())))
Logger.info(`[LibraryItemController] Downloaded file "${libraryFile.metadata.path}"`)
} catch (error) {
Logger.error(`[LibraryItemController] Failed to download file "${libraryFile.metadata.path}"`, error)
LibraryItemController.handleDownloadError(error, res)
}
}
/**
* GET api/items/:id/ebook/:fileid?
* fileid is the inode value stored in LibraryFile.ino or EBookFile.ino
* fileid is only required when reading a supplementary ebook
* when no fileid is passed in the primary ebook will be returned
*
* @param {LibraryItemControllerRequest} req
* @param {Response} res
*/
2023-05-28 10:47:28 -05:00
async getEBookFile(req, res) {
let ebookFile = null
if (req.params.fileid) {
ebookFile = req.libraryItem.getLibraryFileWithIno(req.params.fileid)
if (!ebookFile?.isEBookFile) {
Logger.error(`[LibraryItemController] Invalid ebook file id "${req.params.fileid}"`)
return res.status(400).send('Invalid ebook file id')
}
} else {
ebookFile = req.libraryItem.media.ebookFile
}
2023-05-28 10:47:28 -05:00
if (!ebookFile) {
Logger.error(`[LibraryItemController] No ebookFile for library item "${req.libraryItem.media.title}"`)
2023-05-28 10:47:28 -05:00
return res.sendStatus(404)
}
const ebookFilePath = ebookFile.metadata.path
Logger.info(`[LibraryItemController] User "${req.user.username}" requested download for item "${req.libraryItem.media.title}" ebook at "${ebookFilePath}"`)
if (global.XAccel) {
const encodedURI = encodeUriPath(global.XAccel + ebookFilePath)
Logger.debug(`Use X-Accel to serve static file ${encodedURI}`)
return res.status(204).header({ 'X-Accel-Redirect': encodedURI }).send()
}
try {
await new Promise((resolve, reject) => res.sendFile(ebookFilePath, (error) => (error ? reject(error) : resolve())))
Logger.info(`[LibraryItemController] Downloaded ebook file "${ebookFilePath}"`)
} catch (error) {
Logger.error(`[LibraryItemController] Failed to download ebook file "${ebookFilePath}"`, error)
LibraryItemController.handleDownloadError(error, res)
}
2023-05-28 10:47:28 -05:00
}
/**
* PATCH api/items/:id/ebook/:fileid/status
* toggle the status of an ebook file.
* if an ebook file is the primary ebook, then it will be changed to supplementary
* if an ebook file is supplementary, then it will be changed to primary
*
* @param {LibraryItemControllerRequestWithFile} req
* @param {Response} res
*/
async updateEbookFileStatus(req, res) {
if (!req.libraryItem.isBook) {
Logger.error(`[LibraryItemController] Invalid media type for ebook file status update`)
return res.sendStatus(400)
}
if (!req.libraryFile?.isEBookFile) {
Logger.error(`[LibraryItemController] Invalid ebook file id "${req.params.fileid}"`)
return res.status(400).send('Invalid ebook file id')
}
const ebookLibraryFile = req.libraryFile
let primaryEbookFile = null
const ebookLibraryFileInos = req.libraryItem
.getLibraryFiles()
.filter((lf) => lf.isEBookFile)
.map((lf) => lf.ino)
if (ebookLibraryFile.isSupplementary) {
Logger.info(`[LibraryItemController] Updating ebook file "${ebookLibraryFile.metadata.filename}" to primary`)
primaryEbookFile = ebookLibraryFile.toJSON()
delete primaryEbookFile.isSupplementary
delete primaryEbookFile.fileType
primaryEbookFile.ebookFormat = ebookLibraryFile.metadata.format
} else {
Logger.info(`[LibraryItemController] Updating ebook file "${ebookLibraryFile.metadata.filename}" to supplementary`)
}
req.libraryItem.media.ebookFile = primaryEbookFile
req.libraryItem.media.changed('ebookFile', true)
await req.libraryItem.media.save()
req.libraryItem.libraryFiles = req.libraryItem.libraryFiles.map((lf) => {
if (ebookLibraryFileInos.includes(lf.ino)) {
lf.isSupplementary = lf.ino !== primaryEbookFile?.ino
}
return lf
})
req.libraryItem.changed('libraryFiles', true)
req.libraryItem.isMissing = !req.libraryItem.media.hasMediaFiles
await req.libraryItem.save()
SocketAuthority.libraryItemEmitter('item_updated', req.libraryItem)
res.sendStatus(200)
}
2026-02-06 14:51:54 +02:00
/**
* POST: /api/items/:id/move
* Move a library item to a different library
*
* @param {LibraryItemControllerRequest} req
* @param {Response} res
*/
async move(req, res) {
// Permission check - require delete permission (implies write access)
if (!req.user.canDelete) {
Logger.warn(`[LibraryItemController] User "${req.user.username}" attempted to move item without permission`)
return res.sendStatus(403)
}
const { targetLibraryId, targetFolderId } = req.body
if (!targetLibraryId) {
return res.status(400).send('Target library ID is required')
}
// Get target library with folders
const targetLibrary = await Database.libraryModel.findByIdWithFolders(targetLibraryId)
if (!targetLibrary) {
return res.status(404).send('Target library not found')
}
// Validate media type compatibility
const sourceLibrary = await Database.libraryModel.findByPk(req.libraryItem.libraryId)
if (!sourceLibrary) {
Logger.error(`[LibraryItemController] Source library not found for item ${req.libraryItem.id}`)
return res.status(500).send('Source library not found')
}
if (sourceLibrary.mediaType !== targetLibrary.mediaType) {
return res.status(400).send(`Cannot move ${sourceLibrary.mediaType} to ${targetLibrary.mediaType} library`)
}
// Don't allow moving to same library
if (sourceLibrary.id === targetLibrary.id) {
return res.status(400).send('Item is already in this library')
}
// Determine target folder
let targetFolder = null
if (targetFolderId) {
targetFolder = targetLibrary.libraryFolders.find((f) => f.id === targetFolderId)
if (!targetFolder) {
return res.status(400).send('Target folder not found in library')
}
} else {
// Use first folder if not specified
targetFolder = targetLibrary.libraryFolders[0]
}
2026-02-06 14:51:54 +02:00
if (!targetFolder) {
return res.status(400).send('Target library has no folders')
}
try {
2026-02-06 22:35:03 +02:00
await handleMoveLibraryItem(req.libraryItem, targetLibrary, targetFolder)
2026-02-06 22:25:44 +02:00
await Database.resetLibraryIssuesFilterData(sourceLibrary.id)
await Database.resetLibraryIssuesFilterData(targetLibrary.id)
if (Database.libraryFilterData[sourceLibrary.id]) delete Database.libraryFilterData[sourceLibrary.id]
if (Database.libraryFilterData[targetLibrary.id]) delete Database.libraryFilterData[targetLibrary.id]
if (req.libraryItem.isBook) {
libraryItemsBookFilters.clearCountCache('move_item')
} else if (req.libraryItem.isPodcast) {
libraryItemsPodcastFilters.clearCountCache('podcast', 'move_item')
}
2026-02-06 14:51:54 +02:00
res.json({
success: true,
libraryItem: req.libraryItem.toOldJSONExpanded()
})
} catch (error) {
return res.status(500).send(error.message || 'Failed to move item')
}
}
2026-02-13 14:15:18 +02:00
/**
* POST: /api/items/:id/consolidate
* Rename book folder to Author - Title and move to library root
*
* @param {LibraryItemControllerRequest} req
* @param {Response} res
*/
async consolidate(req, res) {
if (!req.libraryItem.isBook) {
return res.status(400).send('Consolidate only available for books')
}
if (req.libraryItem.isFile) {
return res.status(400).send('Consolidate only available for books in a folder')
}
const author = req.libraryItem.media.authors?.[0]?.name || 'Unknown Author'
const title = req.libraryItem.media.title || 'Unknown Title'
const sanitizedFolderName = Database.libraryItemModel.getConsolidatedFolderName(author, title)
2026-02-13 14:15:18 +02:00
const library = await Database.libraryModel.findByIdWithFolders(req.libraryItem.libraryId)
// Find the library folder that currently contains this item
const targetFolder = library.libraryFolders.find((f) => req.libraryItem.path.startsWith(f.path)) || library.libraryFolders[0]
try {
await handleMoveLibraryItem(req.libraryItem, library, targetFolder, sanitizedFolderName)
// Recursively remove empty parent directories
let parentDir = Path.dirname(req.libraryItem.path)
while (parentDir && parentDir !== targetFolder.path && parentDir !== Path.dirname(parentDir)) {
try {
const files = await fs.readdir(parentDir)
if (files.length === 0) {
await fs.remove(parentDir)
parentDir = Path.dirname(parentDir)
} else {
break
}
} catch (err) {
Logger.error(`[LibraryItemController] Failed to cleanup parent directory "${parentDir}"`, err)
break
}
}
res.json({
success: true,
libraryItem: req.libraryItem.toOldJSONExpanded()
})
} catch (error) {
Logger.error(`[LibraryItemController] Failed to consolidate item "${req.libraryItem.media.title}"`, error)
return res.status(500).send(error.message || 'Failed to consolidate item')
}
}
/**
* POST: /api/items/batch/consolidate
* Consolidate multiple library items
*
* @param {RequestWithUser} req
* @param {Response} res
*/
async batchConsolidate(req, res) {
const { libraryItemIds } = req.body
if (!Array.isArray(libraryItemIds) || !libraryItemIds.length) {
return res.status(400).send('Invalid request')
}
const libraryItems = await Database.libraryItemModel.findAllExpandedWhere({
id: libraryItemIds
})
const results = []
for (const libraryItem of libraryItems) {
if (libraryItem.mediaType !== 'book' || libraryItem.isFile) {
results.push({ id: libraryItem.id, success: false, error: 'Not a book in a folder' })
continue
}
try {
const author = libraryItem.media.authors?.[0]?.name || 'Unknown Author'
const title = libraryItem.media.title || 'Unknown Title'
const sanitizedFolderName = Database.libraryItemModel.getConsolidatedFolderName(author, title)
2026-02-13 14:15:18 +02:00
const library = await Database.libraryModel.findByIdWithFolders(libraryItem.libraryId)
const currentLibraryFolder = library.libraryFolders.find((lf) => libraryItem.path.startsWith(lf.path)) || library.libraryFolders[0]
const oldPath = libraryItem.path
await handleMoveLibraryItem(libraryItem, library, currentLibraryFolder, sanitizedFolderName)
// Recursively remove empty parent directories
let parentDir = Path.dirname(oldPath)
while (parentDir && parentDir !== currentLibraryFolder.path && parentDir !== Path.dirname(parentDir)) {
try {
const files = await fs.readdir(parentDir)
if (files.length === 0) {
await fs.remove(parentDir)
parentDir = Path.dirname(parentDir)
} else {
break
}
} catch (err) {
Logger.error(`[LibraryItemController] Failed to cleanup parent directory "${parentDir}"`, err)
break
}
}
results.push({ id: libraryItem.id, success: true })
} catch (error) {
Logger.error(`[LibraryItemController] Batch Consolidate: Failed to consolidate "${libraryItem.media?.title}"`, error)
results.push({ id: libraryItem.id, success: false, error: error.message })
}
}
res.json({ results })
}
2026-02-12 19:57:04 +02:00
/**
* POST: /api/items/batch/merge
* Merge multiple library items into one
*
* @this {import('../routers/ApiRouter')}
*
* @param {RequestWithUser} req
* @param {Response} res
*/
async batchMerge(req, res) {
if (!req.user.canDelete) {
Logger.warn(`[LibraryItemController] User "${req.user.username}" attempted to batch merge items without permission`)
return res.sendStatus(403)
}
const { libraryItemIds } = req.body
if (!libraryItemIds?.length || !Array.isArray(libraryItemIds) || libraryItemIds.length < 2) {
return res.status(400).send('Invalid request body. Must select at least 2 items.')
}
const libraryItems = await Database.libraryItemModel.findAllExpandedWhere({
id: libraryItemIds
})
if (libraryItems.length !== libraryItemIds.length) {
return res.status(404).send('Some library items not found')
}
const libraryId = libraryItems[0].libraryId
// Validate all items are in the same library and are books
const invalidItem = libraryItems.find((li) => li.libraryId !== libraryId || li.mediaType !== 'book')
if (invalidItem) {
return res.status(400).send('All items must be books in the same library')
}
// Sort items by ID to be deterministic, user selection order is lost in findAllExpandedWhere
// To preserve user selection order, we map libraryItemIds to objects
const orderedLibraryItems = libraryItemIds.map((id) => libraryItems.find((li) => li.id === id)).filter((li) => li)
const primaryItem = orderedLibraryItems[0]
const otherItems = orderedLibraryItems.slice(1)
const primaryItemPath = primaryItem.path
// If primary item is file, its dir is dirname. If folder, its dir is path.
const primaryItemDir = primaryItem.isFile ? Path.dirname(primaryItemPath) : primaryItemPath
const library = await Database.libraryModel.findByIdWithFolders(libraryId)
const libraryFolder = library.libraryFolders.find((lf) => primaryItemPath.startsWith(lf.path))
if (!libraryFolder) {
Logger.error(`[LibraryItemController] Library folder not found for primary item "${primaryItem.media.title}" path "${primaryItemPath}"`)
return res.status(500).send('Library folder not found for primary item')
}
let targetDirPath = primaryItemDir
// If primary item is a single file in the root of the library folder,
// create a new folder for the merged book.
// primaryItemDir check:
// If primaryItem.isFile is true, primaryItemDir is parent dir.
// If primaryItemDir == libraryFolder.path, it means it's in the root of library folder.
const isPrimaryInRoot = primaryItemDir === libraryFolder.path
if (isPrimaryInRoot) {
// Create a new folder for the merged book
const author = primaryItem.media.authors?.[0]?.name || 'Unknown Author'
const title = primaryItem.media.title || 'Unknown Title'
// Simple sanitization
const folderName = `${author} - ${title}`.replace(/[/\\?%*:|"<>]/g, '').trim()
targetDirPath = Path.join(libraryFolder.path, folderName)
if (await fs.pathExists(targetDirPath)) {
// Directory already exists, append timestamp to avoid conflict
targetDirPath += ` (${Date.now()})`
}
await fs.ensureDir(targetDirPath)
// Move primary item file to new folder
const newPrimaryPath = Path.join(targetDirPath, Path.basename(primaryItemPath))
await fs.move(primaryItemPath, newPrimaryPath)
// Update primary item path in memory (DB update will happen on scan)
primaryItem.path = newPrimaryPath
primaryItem.relPath = Path.relative(libraryFolder.path, newPrimaryPath)
}
Logger.info(`[LibraryItemController] Merging ${otherItems.length} items into "${primaryItem.media.title}" at "${targetDirPath}"`)
const successIds = []
const failIds = []
const failedItems = []
for (const item of otherItems) {
try {
const itemPath = item.path
if (item.isFile) {
const filename = Path.basename(itemPath)
let destPath = Path.join(targetDirPath, filename)
// Handle collision
if (await fs.pathExists(destPath)) {
const name = Path.parse(filename).name
const ext = Path.parse(filename).ext
destPath = Path.join(targetDirPath, `${name}_${Date.now()}${ext}`)
}
await fs.move(itemPath, destPath)
} else {
// It's a directory
// Move all files from this directory to target directory
const files = await fs.readdir(itemPath)
for (const file of files) {
const srcFile = Path.join(itemPath, file)
let destFile = Path.join(targetDirPath, file)
if (await fs.pathExists(destFile)) {
const name = Path.parse(file).name
const ext = Path.parse(file).ext
destFile = Path.join(targetDirPath, `${name}_${Date.now()}${ext}`)
}
// If it's a directory inside, move recursively?
// Users shouldn't have nested books usually. fs.move works for dirs too.
await fs.move(srcFile, destFile)
}
// Remove the now empty directory
await fs.remove(itemPath)
}
// Delete the library item from DB
// We pass empty array for mediaItemIds because we moved the files, so we don't want to delete them if they were linked.
// Actually handleDeleteLibraryItem deletes from DB handling relationships.
// But we already moved the files.
// If hard delete was called, it would try to delete files. But we didn't call delete with hard=1 logic here.
// We manually moved files.
// Now we just need to remove the DB entry.
// However, handleDeleteLibraryItem removes media progress, playlists, etc.
await this.handleDeleteLibraryItem(item.id, [item.media.id])
successIds.push(item.id)
} catch (error) {
Logger.error(`[LibraryItemController] Failed to merge item ${item.id}`, error)
failIds.push(item.id)
failedItems.push({ id: item.id, error: error.message })
}
}
// Rescan the target folder
2026-02-13 14:15:18 +02:00
// If moved to folder, tell scanner
2026-02-12 19:57:04 +02:00
if (isPrimaryInRoot) {
// We changed the structure of primary item
await LibraryItemScanner.scanLibraryItem(primaryItem.id, {
path: targetDirPath,
relPath: Path.relative(libraryFolder.path, targetDirPath),
isFile: false
})
} else {
// Just rescan content
await LibraryItemScanner.scanLibraryItem(primaryItem.id)
}
// Check remove empty authors/series for deleted items
// We can collect all author/series IDs from deleted items
const authorIdsToCheck = []
const seriesIdsToCheck = []
otherItems.forEach((item) => {
if (successIds.includes(item.id)) {
if (item.media.authors) authorIdsToCheck.push(...item.media.authors.map((a) => a.id))
if (item.media.series) seriesIdsToCheck.push(...item.media.series.map((s) => s.id))
}
})
if (authorIdsToCheck.length) await this.checkRemoveAuthorsWithNoBooks([...new Set(authorIdsToCheck)])
if (seriesIdsToCheck.length) await this.checkRemoveEmptySeries([...new Set(seriesIdsToCheck)])
res.json({
success: failIds.length === 0,
2026-02-13 14:15:18 +02:00
mergedItemId: primaryItem.id,
2026-02-12 19:57:04 +02:00
successIds,
failIds,
errors: failedItems
})
}
/**
* POST: /api/items/batch/reset-metadata
* Reset metadata for multiple library items
*
* @param {RequestWithUser} req
* @param {Response} res
*/
async batchResetMetadata(req, res) {
if (!req.user.canUpdate) {
Logger.warn(`[LibraryItemController] User "${req.user.username}" attempted to batch reset metadata without permission`)
return res.sendStatus(403)
}
const { libraryItemIds } = req.body
if (!Array.isArray(libraryItemIds) || !libraryItemIds.length) {
return res.status(400).send('Invalid request')
}
const libraryItems = await Database.libraryItemModel.findAllExpandedWhere({
id: libraryItemIds
})
const results = []
for (const libraryItem of libraryItems) {
try {
if (global.MetadataPath) {
const metadataPath = Path.join(global.MetadataPath, 'items', libraryItem.id, 'metadata.json')
if (await fs.pathExists(metadataPath)) {
Logger.info(`[LibraryItemController] Removing metadata file at "${metadataPath}"`)
await fs.remove(metadataPath)
}
}
if (libraryItem.path && !libraryItem.isFile) {
const localMetadataPath = Path.join(libraryItem.path, 'metadata.json')
if (await fs.pathExists(localMetadataPath)) {
Logger.info(`[LibraryItemController] Removing local metadata file at "${localMetadataPath}"`)
await fs.remove(localMetadataPath)
}
}
// Clear cover path to force re-scan of cover
if (libraryItem.media.coverPath) {
libraryItem.media.coverPath = null
await libraryItem.media.save()
}
// Trigger a scan ensuring we don't rely on cache/timestamps if possible
await LibraryItemScanner.scanLibraryItem(libraryItem.id)
results.push({ id: libraryItem.id, success: true })
} catch (error) {
Logger.error(`[LibraryItemController] Batch Reset Metadata: Failed to reset "${libraryItem.media?.title}"`, error)
results.push({ id: libraryItem.id, success: false, error: error.message })
}
}
res.json({ results })
}
/**
*
* @param {RequestWithUser} req
* @param {Response} res
* @param {NextFunction} next
*/
async middleware(req, res, next) {
req.libraryItem = await Database.libraryItemModel.getExpandedById(req.params.id)
if (!req.libraryItem?.media) return res.sendStatus(404)
2022-03-20 06:29:08 -05:00
// Check user can access this library item
if (!req.user.checkCanAccessLibraryItem(req.libraryItem)) {
2022-03-20 06:29:08 -05:00
return res.sendStatus(403)
}
// For library file routes, get the library file
if (req.params.fileid) {
req.libraryFile = req.libraryItem.getLibraryFileWithIno(req.params.fileid)
if (!req.libraryFile) {
Logger.error(`[LibraryItemController] Library file "${req.params.fileid}" does not exist for library item`)
return res.sendStatus(404)
}
}
if (req.path.includes('/play')) {
// allow POST requests using /play and /play/:episodeId
} else if (req.method == 'DELETE' && !req.user.canDelete) {
Logger.warn(`[LibraryItemController] User "${req.user.username}" attempted to delete without permission`)
return res.sendStatus(403)
} else if ((req.method == 'PATCH' || req.method == 'POST') && !req.user.canUpdate) {
Logger.warn(`[LibraryItemController] User "${req.user.username}" attempted to update without permission`)
return res.sendStatus(403)
}
next()
}
}
module.exports = new LibraryItemController()