From 7a1623e6a11307842060360bac97ee10256548f9 Mon Sep 17 00:00:00 2001 From: advplyr Date: Sat, 2 Nov 2024 12:56:40 -0500 Subject: [PATCH] Move cover path func to LibraryItem model --- server/Auth.js | 2 +- server/Database.js | 22 ----------------- server/controllers/LibraryItemController.js | 2 +- server/managers/CacheManager.js | 2 +- server/models/LibraryItem.js | 27 +++++++++++++++++++++ 5 files changed, 30 insertions(+), 25 deletions(-) diff --git a/server/Auth.js b/server/Auth.js index 6e5a4621..da124b72 100644 --- a/server/Auth.js +++ b/server/Auth.js @@ -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 */ diff --git a/server/Database.js b/server/Database.js index 9b9f9cf9..9bce2605 100644 --- a/server/Database.js +++ b/server/Database.js @@ -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() diff --git a/server/controllers/LibraryItemController.js b/server/controllers/LibraryItemController.js index f0f5c86d..1976c34d 100644 --- a/server/controllers/LibraryItemController.js +++ b/server/controllers/LibraryItemController.js @@ -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) } diff --git a/server/managers/CacheManager.js b/server/managers/CacheManager.js index d1b27423..83efae90 100644 --- a/server/managers/CacheManager.js +++ b/server/managers/CacheManager.js @@ -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) } diff --git a/server/models/LibraryItem.js b/server/models/LibraryItem.js index dd07747a..9815b216 100644 --- a/server/models/LibraryItem.js +++ b/server/models/LibraryItem.js @@ -863,6 +863,33 @@ class LibraryItem extends Model { return this.getOldLibraryItem(libraryItem) } + /** + * + * @param {string} libraryItemId + * @returns {Promise} + */ + 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