B: add download of season media + maintenance

This commit is contained in:
X4Lo committed 2025-02-20 01:34:42 +01:00
1 parent d799ef2368
commit 534572775f
6 files changed
+76 -16

No files matched your search

+1 -1
View File
@@ -46,7 +46,7 @@ class MediaController {
getMedia(req, res, next) { getMedia(req, res, next) {
const { category, type, size, fileName } = req.params; 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'); return res.status(404).send('Category Not Found');
} }
+7 -1
View File
@@ -164,7 +164,10 @@ class EntryManager {
let existingSeason = await Season.findOne({ let existingSeason = await Season.findOne({
where: { where: {
tmdb_id: season.id tmdb_id: season.id
} },
include: [
{ model: Media, as: 'medias' }
]
}); });
// If the season exists, update it; otherwise, create a new season // 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 // * Update or insert episodes for the season
for (const episode of seasonData.episodes) { for (const episode of seasonData.episodes) {
// Fetch existing episode data if it exists // Fetch existing episode data if it exists
+2 -1
View File
@@ -364,7 +364,8 @@ class EntryService {
entry_id: id entry_id: id
}, },
include: [ include: [
{ model: Episode, as: 'episodes' } { model: Episode, as: 'episodes' },
{ model: Media, as: 'medias' }
], ],
order: [ order: [
['season_number', 'ASC'], ['season_number', 'ASC'],
+17 -13
View File
@@ -18,12 +18,12 @@ class MaintenanceService {
return MaintenanceService.instance; return MaintenanceService.instance;
} }
/** /**
* Delete orphan media records from database, * Delete orphan media records from database,
* orphan media records are media records that exist in the database * orphan media records are media records that exist in the database
* but do not have a file in the `folder data` * 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` * @param {boolean} [dry=false] - if true only return the count of the files that are getting deleted, default=`false`
*/ */
async deleteOrphanMediaRecords(dry = false) { async deleteOrphanMediaRecords(dry = false) {
const allMedia = await Media.findAll(); const allMedia = await Media.findAll();
@@ -32,19 +32,21 @@ class MaintenanceService {
// Handling different media types // Handling different media types
const mediaTypeFolder = { const mediaTypeFolder = {
'entryposter': path.join(dataPath, 'images', 'entries', 'posters'), 'entry;poster': path.join(dataPath, 'images', 'entries', 'posters'),
'entrylogo': path.join(dataPath, 'images', 'entries', 'logos'), 'entry;logo': path.join(dataPath, 'images', 'entries', 'logos'),
'entrybackdrop': path.join(dataPath, 'images', 'entries', 'backdrops'), 'entry;backdrop': path.join(dataPath, 'images', 'entries', 'backdrops'),
'collectionposter': path.join(dataPath, 'images', 'collections', 'posters'), 'collection;poster': path.join(dataPath, 'images', 'collections', 'posters'),
'collectionlogo': path.join(dataPath, 'images', 'collections', 'logos'), 'collection;backdrop': path.join(dataPath, 'images', 'collections', 'backdrops'),
'collectionbackdrop': path.join(dataPath, 'images', 'collections', 'backdrops')
'season;poster': path.join(dataPath, 'images', 'seasons', 'posters')
}; };
let missingFiles = []; let missingFiles = [];
for (let i = 0; i < allMedia.length; i++) { for (let i = 0; i < allMedia.length; i++) {
const folderType = `${allMedia[i].mediaable}${allMedia[i].type.replace("_preview", "")}`; const folderType = `${allMedia[i].mediaable}${allMedia[i].type.replace("_preview", "")}`;
if (!mediaTypeFolder[folderType]) continue
const filePath = path.join(mediaTypeFolder[folderType], allMedia[i].path) const filePath = path.join(mediaTypeFolder[folderType], allMedia[i].path)
if (!fs.existsSync(filePath)) { if (!fs.existsSync(filePath)) {
missingFiles.push(allMedia[i]); missingFiles.push(allMedia[i]);
@@ -103,7 +105,9 @@ class MaintenanceService {
'entry;backdrop': path.join(dataPath, 'images', 'entries', 'backdrops'), 'entry;backdrop': path.join(dataPath, 'images', 'entries', 'backdrops'),
'collection;poster': path.join(dataPath, 'images', 'collections', 'posters'), '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 // * counter
@@ -93,6 +93,18 @@ class MediaDownloaderService {
writer.on('error', reject); writer.on('error', reject);
}); });
} }
async downloadFile(url) {
const response = await axios({
url: url,
method: 'GET',
responseType: 'stream'
});
return response;
}
} }
module.exports = new MediaDownloaderService(); module.exports = new MediaDownloaderService();
+37
View File
@@ -1,4 +1,5 @@
const path = require('path'); const path = require('path');
const { v4: uuidv4 } = require('uuid');
const Entry = require("../models/Entry"); const Entry = require("../models/Entry");
@@ -10,12 +11,16 @@ const tmdbService = require("./tmdbService");
const mediaService = require("./mediaService"); const mediaService = require("./mediaService");
const mediaDownloaderService = require("./mediaDownloaderService"); const mediaDownloaderService = require("./mediaDownloaderService");
const mediaOptimizerService = require("./mediaOptimizerService"); const mediaOptimizerService = require("./mediaOptimizerService");
const Season = require('../models/Season');
const storageService = require('./storageService');
const Media = require('../models/Media');
const _configService = configService; const _configService = configService;
const _mediaService = mediaService; const _mediaService = mediaService;
const _tmdbService = tmdbService; const _tmdbService = tmdbService;
const _mediaOptimizerService = mediaOptimizerService; const _mediaOptimizerService = mediaOptimizerService;
const _mediaDownloaderService = mediaDownloaderService; const _mediaDownloaderService = mediaDownloaderService;
const _storageService = storageService;
class MediaManager { class MediaManager {
@@ -113,6 +118,38 @@ class MediaManager {
return downloadedImages; 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(); module.exports = new MediaManager();