Skip to content

Commit

Permalink
Merge pull request #3779 from advplyr/refactor-library-item
Browse files Browse the repository at this point in the history
Refactor LibraryItem to use new model
  • Loading branch information
advplyr authored Jan 2, 2025
2 parents de8b0ab + 5e8678f commit d205c6f
Show file tree
Hide file tree
Showing 14 changed files with 1,080 additions and 421 deletions.
372 changes: 245 additions & 127 deletions server/controllers/LibraryItemController.js

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion server/managers/AudioMetadataManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,13 @@ class AudioMetadataMangaer {
return this.tasksQueued.some((t) => t.data.libraryItemId === libraryItemId) || this.tasksRunning.some((t) => t.data.libraryItemId === libraryItemId)
}

/**
*
* @param {import('../models/LibraryItem')} libraryItem
* @returns
*/
getMetadataObjectForApi(libraryItem) {
return ffmpegHelpers.getFFMetadataObject(libraryItem, libraryItem.media.includedAudioFiles.length)
return ffmpegHelpers.getFFMetadataObject(libraryItem.toOldJSONExpanded(), libraryItem.media.includedAudioFiles.length)
}

/**
Expand Down
23 changes: 20 additions & 3 deletions server/managers/CoverManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ class CoverManager {
return imgType
}

/**
*
* @param {import('../models/LibraryItem')} libraryItem
* @param {*} coverFile - file object from req.files
* @returns {Promise<{error:string}|{cover:string}>}
*/
async uploadCover(libraryItem, coverFile) {
const extname = Path.extname(coverFile.name.toLowerCase())
if (!extname || !globals.SupportedImageTypes.includes(extname.slice(1))) {
Expand Down Expand Up @@ -110,14 +116,20 @@ class CoverManager {
await this.removeOldCovers(coverDirPath, extname)
await CacheManager.purgeCoverCache(libraryItem.id)

Logger.info(`[CoverManager] Uploaded libraryItem cover "${coverFullPath}" for "${libraryItem.media.metadata.title}"`)
Logger.info(`[CoverManager] Uploaded libraryItem cover "${coverFullPath}" for "${libraryItem.media.title}"`)

libraryItem.updateMediaCover(coverFullPath)
return {
cover: coverFullPath
}
}

/**
*
* @param {Object} libraryItem - old library item
* @param {string} url
* @param {boolean} [forceLibraryItemFolder=false]
* @returns {Promise<{error:string}|{cover:string}>}
*/
async downloadCoverFromUrl(libraryItem, url, forceLibraryItemFolder = false) {
try {
// Force save cover with library item is used for adding new podcasts
Expand Down Expand Up @@ -166,6 +178,12 @@ class CoverManager {
}
}

/**
*
* @param {string} coverPath
* @param {import('../models/LibraryItem')} libraryItem
* @returns {Promise<{error:string}|{cover:string,updated:boolean}>}
*/
async validateCoverPath(coverPath, libraryItem) {
// Invalid cover path
if (!coverPath || coverPath.startsWith('http:') || coverPath.startsWith('https:')) {
Expand Down Expand Up @@ -235,7 +253,6 @@ class CoverManager {

await CacheManager.purgeCoverCache(libraryItem.id)

libraryItem.updateMediaCover(coverPath)
return {
cover: coverPath,
updated: true
Expand Down
9 changes: 8 additions & 1 deletion server/managers/CronManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ class CronManager {
this.podcastCrons = this.podcastCrons.filter((pc) => pc.expression !== podcastCron.expression)
}

/**
*
* @param {import('../models/LibraryItem')} libraryItem - this can be the old model
*/
checkUpdatePodcastCron(libraryItem) {
// Remove from old cron by library item id
const existingCron = this.podcastCrons.find((pc) => pc.libraryItemIds.includes(libraryItem.id))
Expand All @@ -230,7 +234,10 @@ class CronManager {
const cronMatchingExpression = this.podcastCrons.find((pc) => pc.expression === libraryItem.media.autoDownloadSchedule)
if (cronMatchingExpression) {
cronMatchingExpression.libraryItemIds.push(libraryItem.id)
Logger.info(`[CronManager] Added podcast "${libraryItem.media.metadata.title}" to auto dl episode cron "${cronMatchingExpression.expression}"`)

// TODO: Update after old model removed
const podcastTitle = libraryItem.media.title || libraryItem.media.metadata?.title
Logger.info(`[CronManager] Added podcast "${podcastTitle}" to auto dl episode cron "${cronMatchingExpression.expression}"`)
} else {
this.startPodcastCron(libraryItem.media.autoDownloadSchedule, [libraryItem.id])
}
Expand Down
6 changes: 3 additions & 3 deletions server/managers/PlaybackSessionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class PlaybackSessionManager {

/**
*
* @param {import('../controllers/SessionController').RequestWithUser} req
* @param {import('../controllers/LibraryItemController').LibraryItemControllerRequest} req
* @param {Object} [clientDeviceInfo]
* @returns {Promise<DeviceInfo>}
*/
Expand Down Expand Up @@ -67,14 +67,14 @@ class PlaybackSessionManager {

/**
*
* @param {import('../controllers/SessionController').RequestWithUser} req
* @param {import('../controllers/LibraryItemController').LibraryItemControllerRequest} req
* @param {import('express').Response} res
* @param {string} [episodeId]
*/
async startSessionRequest(req, res, episodeId) {
const deviceInfo = await this.getDeviceInfo(req, req.body?.deviceInfo)
Logger.debug(`[PlaybackSessionManager] startSessionRequest for device ${deviceInfo.deviceDescription}`)
const { libraryItem, body: options } = req
const { oldLibraryItem: libraryItem, body: options } = req
const session = await this.startSession(req.user, deviceInfo, libraryItem, episodeId, options)
res.json(session.toJSONForClient(libraryItem))
}
Expand Down
Loading

0 comments on commit d205c6f

Please sign in to comment.