Skip to content

Commit

Permalink
Move cover path func to LibraryItem model
Browse files Browse the repository at this point in the history
  • Loading branch information
advplyr committed Nov 2, 2024
1 parent c25acb4 commit 7a1623e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 25 deletions.
2 changes: 1 addition & 1 deletion server/Auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Auth {

/**
* Checks if the request should not be authenticated.
* @param {import('express').Request} req
* @param {Request} req
* @returns {boolean}
* @private
*/
Expand Down
22 changes: 0 additions & 22 deletions server/Database.js
Original file line number Diff line number Diff line change
Expand Up @@ -808,28 +808,6 @@ class Database {
return `${normalizedColumn} LIKE ${pattern}`
}
}

async getLibraryItemCoverPath(libraryItemId) {
const libraryItem = await this.libraryItemModel.findByPk(libraryItemId, {
attributes: ['id', 'mediaType', 'mediaId', 'libraryId'],
include: [
{
model: this.bookModel,
attributes: ['id', 'coverPath']
},
{
model: this.podcastModel,
attributes: ['id', 'coverPath']
}
]
})
if (!libraryItem) {
Logger.warn(`[Database] getCover: Library item "${libraryItemId}" does not exist`)
return null
}

return libraryItem.media.coverPath
}
}

module.exports = new Database()
2 changes: 1 addition & 1 deletion server/controllers/LibraryItemController.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ class LibraryItemController {
}

if (raw) {
const coverPath = await Database.getLibraryItemCoverPath(libraryItemId)
const coverPath = await Database.libraryItemModel.getCoverPath(libraryItemId)
if (!coverPath || !(await fs.pathExists(coverPath))) {
return res.sendStatus(404)
}
Expand Down
2 changes: 1 addition & 1 deletion server/managers/CacheManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class CacheManager {
}

// Cached cover does not exist, generate it
const coverPath = await Database.getLibraryItemCoverPath(libraryItemId)
const coverPath = await Database.libraryItemModel.getCoverPath(libraryItemId)
if (!coverPath || !(await fs.pathExists(coverPath))) {
return res.sendStatus(404)
}
Expand Down
27 changes: 27 additions & 0 deletions server/models/LibraryItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,33 @@ class LibraryItem extends Model {
return this.getOldLibraryItem(libraryItem)
}

/**
*
* @param {string} libraryItemId
* @returns {Promise<string>}
*/
static async getCoverPath(libraryItemId) {
const libraryItem = await this.findByPk(libraryItemId, {
attributes: ['id', 'mediaType', 'mediaId', 'libraryId'],
include: [
{
model: this.bookModel,
attributes: ['id', 'coverPath']
},
{
model: this.podcastModel,
attributes: ['id', 'coverPath']
}
]
})
if (!libraryItem) {
Logger.warn(`[LibraryItem] getCoverPath: Library item "${libraryItemId}" does not exist`)
return null
}

return libraryItem.media.coverPath
}

/**
*
* @param {import('sequelize').FindOptions} options
Expand Down

0 comments on commit 7a1623e

Please sign in to comment.