Major rework of media handelling, and collections
This commit is contained in:
1 parent
b42e055691
commit
f7cb2bed53
37 files changed
+1120
-411
No files matched your search
@@ -6,6 +6,7 @@ const { v4: uuidv4 } = require('uuid');
|
||||
const { fetchEntryMedia } = require('./tmdbService');
|
||||
const { getSettings } = require('./settingsService');
|
||||
const sharp = require('sharp');
|
||||
const { logToFile } = require('./logService');
|
||||
|
||||
async function downloadImage(url, filePath) {
|
||||
const response = await axios({
|
||||
@@ -66,6 +67,41 @@ async function getEntryMedia(tmdb_id, is_tvserie, includes = ['posters', 'logos'
|
||||
return downloadedImages;
|
||||
}
|
||||
|
||||
async function downloadCollectionMedia(collection) {
|
||||
if (!collection) return null;
|
||||
|
||||
// Local variables for this function invocation
|
||||
let downloadTasks = [];
|
||||
let downloadedImages = [];
|
||||
|
||||
createMediaFolders(); // Ensuring directories exist
|
||||
|
||||
// Getting the userDataPath to store the images
|
||||
const settings = getSettings();
|
||||
const dataPath = settings.userDataPath;
|
||||
|
||||
const hasPoster = collection.medias && collection.medias.filter((m) => m.type == 'poster').length != 0;
|
||||
if (collection.posterPath && !hasPoster) {
|
||||
const postersDir = path.join(dataPath, 'images', 'collections', 'posters')
|
||||
scheduleDownloads({ file_path: collection.posterPath }, 'poster', postersDir, downloadTasks, downloadedImages);
|
||||
}
|
||||
|
||||
const hasBackdrop = collection.medias && collection.medias.filter((m) => m.type == 'backdrop').length != 0;
|
||||
if (collection.backdropPath && !hasBackdrop) {
|
||||
const backdropsDir = path.join(dataPath, 'images', 'collections', 'backdrops')
|
||||
scheduleDownloads({ file_path: collection.backdropPath }, 'backdrop', backdropsDir, downloadTasks, downloadedImages);
|
||||
}
|
||||
|
||||
// Wait for all downloads to complete
|
||||
await Promise.all(downloadTasks).then(() => {
|
||||
console.log('Downloaded images:', downloadedImages);
|
||||
}).catch(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 && !media.file_path.includes('.svg')) || mediaArray.find(media => !media.file_path.includes('.svg')) || mediaArray[0];
|
||||
@@ -76,25 +112,22 @@ function scheduleDownloads(media, mediaType, mediaDir, downloadTasks, downloaded
|
||||
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);
|
||||
const url = `https://image.tmdb.org/t/p/original${media.file_path}`;
|
||||
const filename = `${uniqueFilename}.${extension}`;
|
||||
const downloadPath = path.join(mediaDir, filename);
|
||||
|
||||
// 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))
|
||||
downloadImage(url, downloadPath)
|
||||
.then(() => {
|
||||
logToFile(`Media Service | downloaded: ${mediaType}, path:${downloadPath} url:${url}`)
|
||||
downloadedImages.push({ type: mediaType, path: filename })
|
||||
})
|
||||
.catch(err => {
|
||||
logToFile(`Media Service | failed to download: ${mediaType}, path:${downloadPath} url:${url}`)
|
||||
logToFile(err)
|
||||
console.error(`Error downloading ${mediaType}:`, err)
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
@@ -105,6 +138,28 @@ function generatePreview(originalPath, previewPath, previewWidth, previewQuality
|
||||
.toFile(previewPath);
|
||||
}
|
||||
|
||||
function doGeneratePreview(fileCategory, fileType, originalFileName, previewWidth = 0, previewQuality = 80) {
|
||||
const dataPath = getSettings().userDataPath;
|
||||
const mediaPath = path.join(dataPath, 'images', fileCategory, fileType);
|
||||
|
||||
const originalPath = path.join(mediaPath, originalFileName);
|
||||
|
||||
const fileNameWithoutExt = originalFileName.split('.')[0];
|
||||
const previewFileName = fileNameWithoutExt + '_preview.jpg'
|
||||
const previewPath = path.join(mediaPath, previewFileName);
|
||||
|
||||
if (previewWidth == 0) {
|
||||
previewWidth = 342; // ? poster
|
||||
if (fileType == 'backdrops') previewWidth = 780;
|
||||
if (fileType == 'logos') previewWidth = 300;
|
||||
}
|
||||
|
||||
return sharp(originalPath)
|
||||
.resize({ width: previewWidth })
|
||||
.jpeg({ quality: previewQuality })
|
||||
.toFile(previewPath);
|
||||
}
|
||||
|
||||
function getMediaFolder() {
|
||||
return getSettings().userDataPath;
|
||||
}
|
||||
@@ -117,11 +172,13 @@ function createMediaFolders() {
|
||||
const backdropDir = path.join(dataPath, 'images', 'entries', 'backdrops');
|
||||
const logoDir = path.join(dataPath, 'images', 'entries', 'logos');
|
||||
const collectionsPostersDir = path.join(dataPath, 'images', 'collections', 'posters');
|
||||
const collectionsBackdropsDir = path.join(dataPath, 'images', 'collections', 'backdrops');
|
||||
|
||||
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 });
|
||||
|
||||
// console.log(`Created media folders.`);
|
||||
}
|
||||
@@ -130,5 +187,7 @@ module.exports = {
|
||||
getEntryMedia,
|
||||
createMediaFolders,
|
||||
getMediaFolder,
|
||||
generatePreview
|
||||
generatePreview,
|
||||
doGeneratePreview,
|
||||
downloadCollectionMedia
|
||||
};
|
||||
Reference in new issue
Block a user