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 axios = require('axios');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const multer = require('multer');
|
|
||||||
|
|
||||||
const { v4: uuidv4 } = require('uuid');
|
const { v4: uuidv4 } = require('uuid');
|
||||||
const { fetchEntryMedia } = require('./tmdbService');
|
const { fetchEntryMedia } = require('./tmdbService');
|
||||||
|
const { getSettings } = require('./settingsService');
|
||||||
|
|
||||||
async function downloadImage(url, filePath) {
|
async function downloadImage(url, filePath) {
|
||||||
const response = await axios({
|
const response = await axios({
|
||||||
@@ -23,137 +22,97 @@ async function downloadImage(url, filePath) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function getEntryMedia(tmdb_id, is_tvserie, includes = ['posters', 'logos', 'backdrops']) {
|
||||||
async function getEntryMedia(tmdb_id, is_tvserie) {
|
// Fetching entry media
|
||||||
console.log(`hello`);
|
|
||||||
let mediaData = await fetchEntryMedia(tmdb_id, is_tvserie);
|
let mediaData = await fetchEntryMedia(tmdb_id, is_tvserie);
|
||||||
if (mediaData == null) return null;
|
if (mediaData == null) return null;
|
||||||
|
|
||||||
|
// Local variables for this function invocation
|
||||||
|
let downloadTasks = [];
|
||||||
let downloadedImages = [];
|
let downloadedImages = [];
|
||||||
|
|
||||||
// Ensuring directories exist
|
createMediaFolders(); // 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');
|
|
||||||
|
|
||||||
fs.mkdirSync(posterDir, { recursive: true });
|
// Getting the userDataPath to store the images
|
||||||
fs.mkdirSync(backdropDir, { recursive: true });
|
const settings = getSettings();
|
||||||
fs.mkdirSync(logoDir, { recursive: true });
|
const dataPath = settings.userDataPath;
|
||||||
|
|
||||||
const downloadTasks = [];
|
// Handling different media types
|
||||||
// * downloading poster original + w300 (for cards)
|
const mediaTypes = [
|
||||||
if (mediaData.posters && mediaData.posters.length) {
|
{ type: 'posters', dir: path.join(dataPath, 'images', 'entries', 'posters') },
|
||||||
let selectedMedia = null;
|
{ type: 'logos', dir: path.join(dataPath, 'images', 'entries', 'logos') },
|
||||||
for (let media of mediaData.posters) {
|
{ type: 'backdrops', dir: path.join(dataPath, 'images', 'entries', 'backdrops') }
|
||||||
if (media.iso_639_1 == 'en') {
|
];
|
||||||
selectedMedia = media;
|
|
||||||
break;
|
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
|
// Wait for all downloads to complete
|
||||||
await Promise.all(downloadTasks).then(downloadedImages => {
|
await Promise.all(downloadTasks).then(() => {
|
||||||
// console.log('Downloaded images:', downloadedImages);
|
console.log('Downloaded images:', downloadedImages);
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
console.error('Error downloading entry images: mediaService>promise all:', err);
|
console.error('Error downloading entry images:', err);
|
||||||
});
|
});
|
||||||
|
|
||||||
return downloadedImages;
|
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 = {
|
module.exports = {
|
||||||
getEntryMedia,
|
getEntryMedia,
|
||||||
|
createMediaFolders,
|
||||||
};
|
};
|
||||||
Reference in new issue
Block a user