refactored the media service
This commit is contained in:
1 parent
63515a9a39
commit
ba96f540ca
1 file changed
+75
-116
@@ -1,11 +1,10 @@
|
||||
const express = require('express');
|
||||
const axios = require('axios');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const multer = require('multer');
|
||||
|
||||
const { v4: uuidv4 } = require('uuid');
|
||||
const { fetchEntryMedia } = require('./tmdbService');
|
||||
const { getSettings } = require('./settingsService');
|
||||
|
||||
async function downloadImage(url, filePath) {
|
||||
const response = await axios({
|
||||
@@ -23,137 +22,97 @@ async function downloadImage(url, filePath) {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
async function getEntryMedia(tmdb_id, is_tvserie) {
|
||||
console.log(`hello`);
|
||||
async function getEntryMedia(tmdb_id, is_tvserie, includes = ['posters', 'logos', 'backdrops']) {
|
||||
// Fetching entry media
|
||||
let mediaData = await fetchEntryMedia(tmdb_id, is_tvserie);
|
||||
if (mediaData == null) return null;
|
||||
|
||||
// Local variables for this function invocation
|
||||
let downloadTasks = [];
|
||||
let downloadedImages = [];
|
||||
|
||||
// Ensuring directories exist
|
||||
const posterDir = path.join(__dirname, '../../', 'uploads', 'entries', 'posters');
|
||||
const backdropDir = path.join(__dirname, '../../', 'uploads', 'entries', 'backdrops');
|
||||
const logoDir = path.join(__dirname, '../../', 'uploads', 'entries', 'logos');
|
||||
createMediaFolders(); // Ensuring directories exist
|
||||
|
||||
fs.mkdirSync(posterDir, { recursive: true });
|
||||
fs.mkdirSync(backdropDir, { recursive: true });
|
||||
fs.mkdirSync(logoDir, { recursive: true });
|
||||
// Getting the userDataPath to store the images
|
||||
const settings = getSettings();
|
||||
const dataPath = settings.userDataPath;
|
||||
|
||||
const downloadTasks = [];
|
||||
// * downloading poster original + w300 (for cards)
|
||||
if (mediaData.posters && mediaData.posters.length) {
|
||||
let selectedMedia = null;
|
||||
for (let media of mediaData.posters) {
|
||||
if (media.iso_639_1 == 'en') {
|
||||
selectedMedia = media;
|
||||
break;
|
||||
// Handling different media types
|
||||
const mediaTypes = [
|
||||
{ type: 'posters', dir: path.join(dataPath, 'images', 'entries', 'posters') },
|
||||
{ type: 'logos', dir: path.join(dataPath, 'images', 'entries', 'logos') },
|
||||
{ type: 'backdrops', dir: path.join(dataPath, 'images', 'entries', 'backdrops') }
|
||||
];
|
||||
|
||||
mediaTypes.forEach(({ type, dir }) => {
|
||||
if (!includes.includes(type)) return; // ? the media type is not requested
|
||||
|
||||
if (mediaData[type] && mediaData[type].length) {
|
||||
const selectedMedia = findPreferredMedia(mediaData[type]);
|
||||
if (selectedMedia) {
|
||||
scheduleDownloads(selectedMedia, type.slice(0, -1), dir, downloadTasks, downloadedImages);
|
||||
}
|
||||
}
|
||||
if (selectedMedia == null) selectedMedia = mediaData.posters[0]
|
||||
|
||||
const uniqueFilename = uuidv4();
|
||||
const extension = selectedMedia.file_path.split('.').pop();
|
||||
|
||||
let originalPosterUrl = `https://image.tmdb.org/t/p/original${selectedMedia.file_path}`;
|
||||
let originalPosterFileName = `${uniqueFilename}.${extension}`;
|
||||
let originalPosterFilePath = path.join(posterDir, originalPosterFileName);
|
||||
|
||||
downloadTasks.push(
|
||||
downloadImage(originalPosterUrl, originalPosterFilePath)
|
||||
.then(() => downloadedImages.push({ type: 'poster', path: `${originalPosterFileName}` }))
|
||||
.catch(err => console.error('Error downloading poster:', err))
|
||||
);
|
||||
|
||||
let cardPosterUrl = `https://image.tmdb.org/t/p/w300${selectedMedia.file_path}`;
|
||||
let cardPosterFileName = `${uniqueFilename}_preview.${extension}`;
|
||||
let cardPosterFilePath = path.join(posterDir, cardPosterFileName);
|
||||
|
||||
downloadTasks.push(
|
||||
downloadImage(cardPosterUrl, cardPosterFilePath)
|
||||
.then(() => { })
|
||||
.catch(err => console.error('Error downloading preview poster:', err))
|
||||
);
|
||||
}
|
||||
|
||||
if (mediaData.logos && mediaData.logos.length) {
|
||||
let selectedMedia = null;
|
||||
for (let media of mediaData.logos) {
|
||||
if (media.iso_639_1 == 'en') {
|
||||
selectedMedia = media;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (selectedMedia == null) selectedMedia = mediaData.logos[0]
|
||||
|
||||
const uniqueFilename = uuidv4();
|
||||
const extension = selectedMedia.file_path.split('.').pop();
|
||||
|
||||
let originalLogoUrl = `https://image.tmdb.org/t/p/original${selectedMedia.file_path}`;
|
||||
let originalLogoFileName = `${uniqueFilename}.${extension}`;
|
||||
let originalLogoFilePath = path.join(logoDir, originalLogoFileName);
|
||||
|
||||
downloadTasks.push(
|
||||
downloadImage(originalLogoUrl, originalLogoFilePath)
|
||||
.then(() => downloadedImages.push({ type: 'logo', path: `${originalLogoFileName}` }))
|
||||
.catch(err => console.error('Error downloading logo:', err))
|
||||
);
|
||||
|
||||
let cardLogoUrl = `https://image.tmdb.org/t/p/w300${selectedMedia.file_path}`;
|
||||
let cardLogoFileName = `${uniqueFilename}_preview.${extension}`;
|
||||
let cardLogoFilePath = path.join(logoDir, cardLogoFileName);
|
||||
|
||||
downloadTasks.push(
|
||||
downloadImage(cardLogoUrl, cardLogoFilePath)
|
||||
.then(() => { })
|
||||
.catch(err => console.error('Error downloading preview logo:', err))
|
||||
);
|
||||
}
|
||||
|
||||
if (mediaData.backdrops && mediaData.backdrops.length) {
|
||||
let selectedMedia = null;
|
||||
for (let media of mediaData.backdrops) {
|
||||
if (media.iso_639_1 == 'en') {
|
||||
selectedMedia = media;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (selectedMedia == null) selectedMedia = mediaData.backdrops[0]
|
||||
|
||||
const uniqueFilename = uuidv4();
|
||||
const extension = selectedMedia.file_path.split('.').pop();
|
||||
|
||||
let originalBackdropUrl = `https://image.tmdb.org/t/p/original${selectedMedia.file_path}`;
|
||||
let originalBackdropFileName = `${uniqueFilename}.${extension}`;
|
||||
let originalBackdropFilePath = path.join(backdropDir, originalBackdropFileName);
|
||||
|
||||
downloadTasks.push(
|
||||
downloadImage(originalBackdropUrl, originalBackdropFilePath)
|
||||
.then(() => downloadedImages.push({ type: 'backdrop', path: `${originalBackdropFileName}` }))
|
||||
.catch(err => console.error('Error downloading backdrop:', err))
|
||||
);
|
||||
|
||||
let cardBackdropUrl = `https://image.tmdb.org/t/p/w300${selectedMedia.file_path}`;
|
||||
let cardBackdropFileName = `${uniqueFilename}_preview.${extension}`;
|
||||
let cardBackdropFilePath = path.join(backdropDir, cardBackdropFileName);
|
||||
|
||||
downloadTasks.push(
|
||||
downloadImage(cardBackdropUrl, cardBackdropFilePath)
|
||||
.then(() => { })
|
||||
.catch(err => console.error('Error downloading preview backdrop:', err))
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
// Wait for all downloads to complete
|
||||
await Promise.all(downloadTasks).then(downloadedImages => {
|
||||
// console.log('Downloaded images:', downloadedImages);
|
||||
await Promise.all(downloadTasks).then(() => {
|
||||
console.log('Downloaded images:', downloadedImages);
|
||||
}).catch(err => {
|
||||
console.error('Error downloading entry images: mediaService>promise all:', err);
|
||||
console.error('Error downloading entry images:', err);
|
||||
});
|
||||
|
||||
return downloadedImages;
|
||||
}
|
||||
|
||||
// Utility to find the preferred media (English if available, otherwise the first one)
|
||||
function findPreferredMedia(mediaArray, preferredLanguage = 'en') {
|
||||
return mediaArray.find(media => media.iso_639_1 === preferredLanguage) || mediaArray[0];
|
||||
}
|
||||
|
||||
// Adjusted scheduleDownloads to accept local arrays
|
||||
function scheduleDownloads(media, mediaType, mediaDir, downloadTasks, downloadedImages) {
|
||||
const uniqueFilename = uuidv4();
|
||||
const extension = media.file_path.split('.').pop();
|
||||
|
||||
const originalUrl = `https://image.tmdb.org/t/p/original${media.file_path}`;
|
||||
const originalFileName = `${uniqueFilename}.${extension}`;
|
||||
const originalFilePath = path.join(mediaDir, originalFileName);
|
||||
|
||||
// Schedule download of the original image
|
||||
downloadTasks.push(
|
||||
downloadImage(originalUrl, originalFilePath)
|
||||
.then(() => downloadedImages.push({ type: mediaType, path: originalFileName }))
|
||||
.catch(err => console.error(`Error downloading ${mediaType}:`, err))
|
||||
);
|
||||
|
||||
const previewUrl = `https://image.tmdb.org/t/p/w300${media.file_path}`;
|
||||
const previewFileName = `${uniqueFilename}_preview.${extension}`;
|
||||
const previewFilePath = path.join(mediaDir, previewFileName);
|
||||
|
||||
// Schedule download of the preview image
|
||||
downloadTasks.push(
|
||||
downloadImage(previewUrl, previewFilePath)
|
||||
.catch(err => console.error(`Error downloading preview ${mediaType}:`, err))
|
||||
);
|
||||
}
|
||||
|
||||
function createMediaFolders() {
|
||||
const settings = getSettings();
|
||||
const dataPath = settings.userDataPath;
|
||||
|
||||
const posterDir = path.join(dataPath, 'images', 'entries', 'posters');
|
||||
const backdropDir = path.join(dataPath, 'images', 'entries', 'backdrops');
|
||||
const logoDir = path.join(dataPath, 'images', 'entries', 'logos');
|
||||
|
||||
fs.mkdirSync(posterDir, { recursive: true });
|
||||
fs.mkdirSync(backdropDir, { recursive: true });
|
||||
fs.mkdirSync(logoDir, { recursive: true });
|
||||
|
||||
// console.log(`Created media folders.`);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getEntryMedia,
|
||||
createMediaFolders,
|
||||
};
|
||||
Reference in new issue
Block a user