Major Backend Single Responsibility Update part 1
This commit is contained in:
1 parent
65177c7553
commit
cbdffb21e3
41 files changed
+1723
-888
No files matched your search
@@ -0,0 +1,98 @@
|
||||
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;
|
||||
}
|
||||
|
||||
// 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);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new MediaDownloaderService();
|
||||
Reference in new issue
Block a user