- Added people manager - Auto update non fully fetched people - People media downloader - Updated media controller to serve peoples profiles - Config people media folders auto creation
137 lines
4.6 KiB
JavaScript
137 lines
4.6 KiB
JavaScript
const path = require('path');
|
|
const fs = require('fs');
|
|
const { default: axios } = require('axios');
|
|
const { v4: uuidv4 } = require('uuid');
|
|
|
|
const { logToFile } = require("./logService");
|
|
const configService = require('./configService');
|
|
|
|
const _configService = configService;
|
|
|
|
class MediaDownloaderService {
|
|
constructor() {
|
|
if (!MediaDownloaderService.instance) {
|
|
MediaDownloaderService.instance = this;
|
|
}
|
|
return MediaDownloaderService.instance;
|
|
}
|
|
|
|
async downloadCollectionMedia(collection) {
|
|
if (!collection) return null;
|
|
|
|
// Local variables for this function invocation
|
|
let downloadTasks = [];
|
|
let downloadedImages = [];
|
|
|
|
_configService.createMediaFolders(); // Ensuring directories exist
|
|
|
|
const dataPath = _configService.getSettings().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')
|
|
this.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')
|
|
this.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;
|
|
}
|
|
|
|
async downloadPersonMedia(person) {
|
|
if (!person) return null;
|
|
|
|
// Local variables for this function invocation
|
|
let downloadTasks = [];
|
|
let downloadedImages = [];
|
|
|
|
_configService.createMediaFolders(); // Ensuring directories exist
|
|
|
|
const dataPath = _configService.getSettings().userDataPath;
|
|
|
|
// Check if person already has a profile image
|
|
const hasProfile = person.medias && person.medias.filter((m) => m.type === 'profile').length !== 0;
|
|
|
|
if (person.profile_path && !hasProfile) {
|
|
const profilesDir = path.join(dataPath, 'images', 'peoples', 'profiles');
|
|
this.scheduleDownloads({ file_path: person.profile_path }, 'profile', profilesDir, 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)
|
|
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];
|
|
}
|
|
|
|
// Adjusted scheduleDownloads to accept local arrays
|
|
scheduleDownloads(media, mediaType, mediaDir, downloadTasks, downloadedImages) {
|
|
const uniqueFilename = uuidv4();
|
|
const extension = media.file_path.split('.').pop();
|
|
|
|
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(
|
|
this.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)
|
|
})
|
|
);
|
|
}
|
|
|
|
async downloadImage(url, filePath) {
|
|
const response = await axios({
|
|
url: url,
|
|
method: 'GET',
|
|
responseType: 'stream'
|
|
});
|
|
|
|
const writer = fs.createWriteStream(filePath);
|
|
response.data.pipe(writer);
|
|
|
|
return new Promise((resolve, reject) => {
|
|
writer.on('finish', resolve);
|
|
writer.on('error', reject);
|
|
});
|
|
}
|
|
|
|
async downloadFile(url) {
|
|
const response = await axios({
|
|
url: url,
|
|
method: 'GET',
|
|
responseType: 'stream'
|
|
});
|
|
|
|
return response;
|
|
}
|
|
}
|
|
|
|
module.exports = new MediaDownloaderService(); |