From 4e20c5320bf030cd627f6cc98ee0620a6ee240ec Mon Sep 17 00:00:00 2001 From: X4Lo Date: Tue, 1 Apr 2025 23:53:25 +0000 Subject: [PATCH] Backend: - add download of episode media function - serving of episodes/seasons files - pulling seasons/episodes media on entry create - maintenance for seasons/episodes media --- backend/controllers/mediaController.js | 2 +- backend/services/configService.js | 4 ++++ backend/services/entryManager.js | 10 ++++++-- backend/services/entryService.js | 9 ++++++- backend/services/maintenanceService.js | 33 ++++++++++---------------- backend/services/mediaManager.js | 31 +++++++++++++++++++----- 6 files changed, 58 insertions(+), 31 deletions(-) diff --git a/backend/controllers/mediaController.js b/backend/controllers/mediaController.js index 2a53190..9338fec 100644 --- a/backend/controllers/mediaController.js +++ b/backend/controllers/mediaController.js @@ -50,7 +50,7 @@ class MediaController { return res.status(404).send('Category Not Found'); } - if (!['backdrops', 'posters', 'logos', 'others'].includes(type)) { + if (!['backdrops', 'posters', 'logos', 'stills', 'others'].includes(type)) { return res.status(404).send('Type Not Found'); } diff --git a/backend/services/configService.js b/backend/services/configService.js index caf50f0..760baba 100644 --- a/backend/services/configService.js +++ b/backend/services/configService.js @@ -83,12 +83,16 @@ class ConfigService { const logoDir = path.join(dataPath, 'images', 'entries', 'logos'); const collectionsPostersDir = path.join(dataPath, 'images', 'collections', 'posters'); const collectionsBackdropsDir = path.join(dataPath, 'images', 'collections', 'backdrops'); + const seasonsPostersDir = path.join(dataPath, 'images', 'seasons', 'posters'); + const episodesStillsDir = path.join(dataPath, 'images', 'episodes', 'stills'); fs.mkdirSync(posterDir, { recursive: true }); fs.mkdirSync(backdropDir, { recursive: true }); fs.mkdirSync(logoDir, { recursive: true }); fs.mkdirSync(collectionsPostersDir, { recursive: true }); fs.mkdirSync(collectionsBackdropsDir, { recursive: true }); + fs.mkdirSync(seasonsPostersDir, { recursive: true }); + fs.mkdirSync(episodesStillsDir, { recursive: true }); } getTmdbApiUrl() { diff --git a/backend/services/entryManager.js b/backend/services/entryManager.js index 411dc0e..b1c205c 100644 --- a/backend/services/entryManager.js +++ b/backend/services/entryManager.js @@ -210,7 +210,10 @@ class EntryManager { let existingEpisode = await Episode.findOne({ where: { tmdb_id: episode.id, - } + }, + include: [ + { model: Media, as: 'medias' } + ] }); // If the episode exists, update it; otherwise, create a new episode @@ -229,7 +232,7 @@ class EntryManager { await existingEpisode.save(); } else { // Create a new episode - await Episode.create({ + existingEpisode = await Episode.create({ tmdb_id: episode.id, season_id: existingSeason.id, name: episode.name, @@ -242,6 +245,9 @@ class EntryManager { still_path: episode.still_path, }); } + + if (existingEpisode.medias.length == 0) + _mediaManager.getEpisodenMedia(existingEpisode); } } } diff --git a/backend/services/entryService.js b/backend/services/entryService.js index 72b7059..a097fa3 100644 --- a/backend/services/entryService.js +++ b/backend/services/entryService.js @@ -198,6 +198,8 @@ class EntryService { banner_path: null, }) + _mediaManager.getSeasonMedia(savedSeason); + // * episodes let episodes = []; for (const episode of seasonData.episodes) { @@ -219,6 +221,7 @@ class EntryService { }); episodes.push(savedEpisode); + _mediaManager.getEpisodenMedia(savedEpisode); } // TODO fetch cast @@ -364,7 +367,11 @@ class EntryService { entry_id: id }, include: [ - { model: Episode, as: 'episodes' }, + { + model: Episode, as: 'episodes', include: [ + { model: Media, as: 'medias' } + ] + }, { model: Media, as: 'medias' } ], order: [ diff --git a/backend/services/maintenanceService.js b/backend/services/maintenanceService.js index 172f565..69fea36 100644 --- a/backend/services/maintenanceService.js +++ b/backend/services/maintenanceService.js @@ -39,7 +39,8 @@ class MaintenanceService { 'collection;poster': path.join(dataPath, 'images', 'collections', 'posters'), 'collection;backdrop': path.join(dataPath, 'images', 'collections', 'backdrops'), - 'season;poster': path.join(dataPath, 'images', 'seasons', 'posters') + 'season;poster': path.join(dataPath, 'images', 'seasons', 'posters'), + 'episode;still': path.join(dataPath, 'images', 'episodes', 'stills') }; let missingFiles = []; @@ -56,7 +57,8 @@ class MaintenanceService { let removed = { 'posters': 0, 'logos': 0, - 'backdrops': 0 + 'backdrops': 0, + 'stills': 0, }; // if (missingFiles.length == 0) @@ -83,20 +85,11 @@ class MaintenanceService { // Getting the userDataPath to store the images const dataPath = _configService.getMediaFolder(); - // ? Getting the path of all media files and putting them in an array - // const allMedia = (await Media.findAll()).map(m => { - // if (m.mediaable == 'entry') - // return path.join(dataPath, 'images', 'entries', m.type + 's', m.path) - // else if (m.mediaable == 'collection') - // return path.join(dataPath, 'images', 'collections', m.type + 's', m.path) - // }); - // ? Getting the name (without extention) of all media in database const allMedia = (await Media.findAll()).map(m => { return m.path.split('.')[0] }); - // ? Loop trough the files of the images folder and check // ? if a file doesnt exist in the allMedia array and mark it to delete const mediaTypeFolder = { @@ -107,14 +100,16 @@ class MaintenanceService { 'collection;poster': path.join(dataPath, 'images', 'collections', 'posters'), 'collection;backdrop': path.join(dataPath, 'images', 'collections', 'backdrops'), - 'season;poster': path.join(dataPath, 'images', 'seasons', 'posters') + 'season;poster': path.join(dataPath, 'images', 'seasons', 'posters'), + 'episode;still': path.join(dataPath, 'images', 'episodes', 'stills') }; // * counter let removed = { 'posters': 0, 'logos': 0, - 'backdrops': 0 + 'backdrops': 0, + 'stills': 0, }; // Iterate over each media type directory and check for orphan files @@ -127,13 +122,6 @@ class MaintenanceService { // Check each file in the directory for (let file of files) { - const filePath = path.join(directory, file); - // // Check if the constructed file path exists in the allMedia array - // // ? this step also takes into account preview files, by removing '_preview' - // // ? and verifying if the file path is in database - // const hasJpgFormat = allMedia.includes(filePath.replace("_preview.jpg", ".jpg")); - // const hasPngFormat = allMedia.includes(filePath.replace("_preview.jpg", ".png")); - // const hasGifFormat = allMedia.includes(filePath.replace("_preview.jpg", ".gif")); // preparing the file name const fileName = file.split('.')[0].replace("_preview", ""); @@ -142,7 +130,10 @@ class MaintenanceService { // if the file isn't in database even on other extentions then delete it if (!isInDatabase) { removed[folderType.split(';')[1] + 's']++; // Increment the count of removed files - if (!dry) await fsPromises.unlink(filePath); + if (!dry) { + const b = path.join(directory, file); + await fsPromises.unlink(filePath); + } } } } catch (err) { diff --git a/backend/services/mediaManager.js b/backend/services/mediaManager.js index 21ec445..e02458b 100644 --- a/backend/services/mediaManager.js +++ b/backend/services/mediaManager.js @@ -120,10 +120,7 @@ class MediaManager { } async getSeasonMedia(season) { - // const season = await Season.findOne(seasonId); - - // if (!season) return NotFoundError(`Season with id ${seasonId} not found.`); - + if (!season.poster_path) return; const url = `https://image.tmdb.org/t/p/original${season.poster_path}`; const file = await _mediaDownloaderService.downloadFile(url); @@ -135,7 +132,6 @@ class MediaManager { const fullFilePath = path.join(dataPath, 'images', 'seasons', 'posters', filename) _storageService.storeFile(file, fullFilePath).then(async () => { - // create media object await Media.create({ mediaable: 'season', mediaableId: season.id, @@ -144,9 +140,32 @@ class MediaManager { isLocal: true, isSelected: true }); + console.log(`File created for season: ${filename}`); + }).catch(); + } - // optimine file + async getEpisodenMedia(episode) { + if (!episode.still_path) return; + const url = `https://image.tmdb.org/t/p/original${episode.still_path}`; + const file = await _mediaDownloaderService.downloadFile(url); + const uniqueFilename = uuidv4(); + const extension = episode.still_path.split('.').pop(); + const filename = `${uniqueFilename}.${extension}`; + + const dataPath = _configService.getSettings().userDataPath; + const fullFilePath = path.join(dataPath, 'images', 'episodes', 'stills', filename) + + _storageService.storeFile(file, fullFilePath).then(async () => { + await Media.create({ + mediaable: 'episode', + mediaableId: episode.id, + type: 'still', + path: filename, + isLocal: true, + isSelected: true + }); + console.log(`File created for episode: ${filename}`); }).catch(); }