diff --git a/backend/controllers/mediaController.js b/backend/controllers/mediaController.js index 9114495..2a53190 100644 --- a/backend/controllers/mediaController.js +++ b/backend/controllers/mediaController.js @@ -46,7 +46,7 @@ class MediaController { getMedia(req, res, next) { const { category, type, size, fileName } = req.params; - if (!['entries', 'collections', 'franchises'].includes(category)) { + if (!['entries', 'collections', 'franchises', 'seasons', 'episodes'].includes(category)) { return res.status(404).send('Category Not Found'); } diff --git a/backend/services/entryManager.js b/backend/services/entryManager.js index 2752270..411dc0e 100644 --- a/backend/services/entryManager.js +++ b/backend/services/entryManager.js @@ -164,7 +164,10 @@ class EntryManager { let existingSeason = await Season.findOne({ where: { tmdb_id: season.id - } + }, + include: [ + { model: Media, as: 'medias' } + ] }); // If the season exists, update it; otherwise, create a new season @@ -198,6 +201,9 @@ class EntryManager { }); } + if (existingSeason.medias.length = 0) + _mediaManager.getSeasonMedia(existingSeason); + // * Update or insert episodes for the season for (const episode of seasonData.episodes) { // Fetch existing episode data if it exists diff --git a/backend/services/entryService.js b/backend/services/entryService.js index 9db4727..72b7059 100644 --- a/backend/services/entryService.js +++ b/backend/services/entryService.js @@ -364,7 +364,8 @@ class EntryService { entry_id: id }, include: [ - { model: Episode, as: 'episodes' } + { model: Episode, as: 'episodes' }, + { model: Media, as: 'medias' } ], order: [ ['season_number', 'ASC'], diff --git a/backend/services/maintenanceService.js b/backend/services/maintenanceService.js index 7786d4d..172f565 100644 --- a/backend/services/maintenanceService.js +++ b/backend/services/maintenanceService.js @@ -18,12 +18,12 @@ class MaintenanceService { return MaintenanceService.instance; } - /** - * Delete orphan media records from database, - * orphan media records are media records that exist in the database - * but do not have a file in the `folder data` - * @param {boolean} [dry=false] - if true only return the count of the files that are getting deleted, default=`false` - */ + /** + * Delete orphan media records from database, + * orphan media records are media records that exist in the database + * but do not have a file in the `folder data` + * @param {boolean} [dry=false] - if true only return the count of the files that are getting deleted, default=`false` + */ async deleteOrphanMediaRecords(dry = false) { const allMedia = await Media.findAll(); @@ -32,19 +32,21 @@ class MaintenanceService { // Handling different media types const mediaTypeFolder = { - 'entryposter': path.join(dataPath, 'images', 'entries', 'posters'), - 'entrylogo': path.join(dataPath, 'images', 'entries', 'logos'), - 'entrybackdrop': path.join(dataPath, 'images', 'entries', 'backdrops'), + 'entry;poster': path.join(dataPath, 'images', 'entries', 'posters'), + 'entry;logo': path.join(dataPath, 'images', 'entries', 'logos'), + 'entry;backdrop': path.join(dataPath, 'images', 'entries', 'backdrops'), - 'collectionposter': path.join(dataPath, 'images', 'collections', 'posters'), - 'collectionlogo': path.join(dataPath, 'images', 'collections', 'logos'), - 'collectionbackdrop': path.join(dataPath, 'images', 'collections', 'backdrops') + 'collection;poster': path.join(dataPath, 'images', 'collections', 'posters'), + 'collection;backdrop': path.join(dataPath, 'images', 'collections', 'backdrops'), + + 'season;poster': path.join(dataPath, 'images', 'seasons', 'posters') }; let missingFiles = []; for (let i = 0; i < allMedia.length; i++) { const folderType = `${allMedia[i].mediaable}${allMedia[i].type.replace("_preview", "")}`; + if (!mediaTypeFolder[folderType]) continue const filePath = path.join(mediaTypeFolder[folderType], allMedia[i].path) if (!fs.existsSync(filePath)) { missingFiles.push(allMedia[i]); @@ -103,7 +105,9 @@ class MaintenanceService { 'entry;backdrop': path.join(dataPath, 'images', 'entries', 'backdrops'), 'collection;poster': path.join(dataPath, 'images', 'collections', 'posters'), - 'collection;backdrop': path.join(dataPath, 'images', 'collections', 'backdrops') + 'collection;backdrop': path.join(dataPath, 'images', 'collections', 'backdrops'), + + 'season;poster': path.join(dataPath, 'images', 'seasons', 'posters') }; // * counter diff --git a/backend/services/mediaDownloaderService.js b/backend/services/mediaDownloaderService.js index df4ef53..5e99ae9 100644 --- a/backend/services/mediaDownloaderService.js +++ b/backend/services/mediaDownloaderService.js @@ -93,6 +93,18 @@ class MediaDownloaderService { writer.on('error', reject); }); } + + + async downloadFile(url) { + const response = await axios({ + url: url, + method: 'GET', + responseType: 'stream' + }); + + return response; + } + } module.exports = new MediaDownloaderService(); \ No newline at end of file diff --git a/backend/services/mediaManager.js b/backend/services/mediaManager.js index 688f4bf..21ec445 100644 --- a/backend/services/mediaManager.js +++ b/backend/services/mediaManager.js @@ -1,4 +1,5 @@ const path = require('path'); +const { v4: uuidv4 } = require('uuid'); const Entry = require("../models/Entry"); @@ -10,12 +11,16 @@ const tmdbService = require("./tmdbService"); const mediaService = require("./mediaService"); const mediaDownloaderService = require("./mediaDownloaderService"); const mediaOptimizerService = require("./mediaOptimizerService"); +const Season = require('../models/Season'); +const storageService = require('./storageService'); +const Media = require('../models/Media'); const _configService = configService; const _mediaService = mediaService; const _tmdbService = tmdbService; const _mediaOptimizerService = mediaOptimizerService; const _mediaDownloaderService = mediaDownloaderService; +const _storageService = storageService; class MediaManager { @@ -113,6 +118,38 @@ class MediaManager { return downloadedImages; } + + async getSeasonMedia(season) { + // const season = await Season.findOne(seasonId); + + // if (!season) return NotFoundError(`Season with id ${seasonId} not found.`); + + const url = `https://image.tmdb.org/t/p/original${season.poster_path}`; + const file = await _mediaDownloaderService.downloadFile(url); + + const uniqueFilename = uuidv4(); + const extension = season.poster_path.split('.').pop(); + const filename = `${uniqueFilename}.${extension}`; + + const dataPath = _configService.getSettings().userDataPath; + 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, + type: 'poster', + path: filename, + isLocal: true, + isSelected: true + }); + + // optimine file + + }).catch(); + } + } module.exports = new MediaManager();