implemented images maintenance, services + component

This commit is contained in:
X4Lo committed 2024-06-06 03:41:55 +01:00
1 parent ba96f540ca
commit 34766ba8e9
13 files changed
+448 -4

No files matched your search

+41 -2
View File
@@ -125,6 +125,8 @@ async function saveEntry(data) {
// + end of: seasons & episodes for tv series
// ? cast
// ? images
const downloadedImages = await getEntryMedia(data.id, is_tvseries);
console.log(`entry service, downloaded images: `, downloadedImages)
@@ -490,9 +492,46 @@ async function refrechEntry(entryId) {
// if (!oldEntry.casts || oldEntry.casts.length == 0 || oldEntry.casts.length < oldEntry.max_cast) {
// }
// * Media
// * Only fetch if a Poster, Backdrop or a Logo is missing
// * Updating Media : Only fetch if a Poster, Backdrop or a Logo is missing
let downloadedImages;
if (oldEntry.medias.length == 0) {
// ? if no media is available, directly get all
downloadedImages = await getEntryMedia(data.id, oldEntry.is_tvseries);
} else {
// ? only get missing media types
let availableMedia = {
'posters': 0,
'logos': 0,
'backdrops': 0
};
// ? count media by type
for (let i = 0; i < oldEntry.medias.length; i++) {
availableMedia[oldEntry.medias[i].type] += 1;
}
// ? if the count of a media type == 0, the add the type to mediaToGet
let mediaToGet = [];
availableMedia.forEach(({ type, count }) => {
if (count == 0) mediaToGet.push(type);
});
if (mediaToGet.length != 0) {
downloadedImages = await getEntryMedia(data.id, oldEntry.is_tvseries, mediaToGet);
}
}
if (downloadedImages) {
for (const image of downloadedImages) {
await Media.create({
entry_id: oldEntry.id,
type: image.type,
path: image.path,
is_local: true,
is_selected: true
})
}
}
// + end of: refrech logic
+106
View File
@@ -0,0 +1,106 @@
const path = require('path');
const fsPromises = require('fs').promises;
const fs = require('fs');
const Media = require("../models/Media");
const { getSettings } = require("./settingsService");
const { logToFile } = require('./logService');
async function deleteMissingFilesEntries(dry = false) {
const allMedia = await Media.findAll();
// Getting the userDataPath to store the images
const dataPath = getSettings().userDataPath;
// Handling different media types
const mediaTypes = {
'poster': path.join(dataPath, 'images', 'entries', 'posters'),
'logo': path.join(dataPath, 'images', 'entries', 'logos'),
'backdrop': path.join(dataPath, 'images', 'entries', 'backdrops')
};
let missingFiles = [];
for (let i = 0; i < allMedia.length; i++) {
let type = `${allMedia[i].type}`;
const filePath = path.join(mediaTypes[type], allMedia[i].path)
if (!fs.existsSync(filePath)) {
missingFiles.push(allMedia[i]);
}
}
let removed = {
'posters': 0,
'logos': 0,
'backdrops': 0
};
if (missingFiles.length == 0)
return removed;
for (let i = 0; i < missingFiles.length; i++) {
removed[missingFiles[i].type] += 1;
if (!dry) missingFiles[i].destroy();
}
if (dry) logToFile(`Run a dry missing files check, found: {posters: ${removed.posters}, backdrops: ${removed.backdrops}, logos: ${removed.logos}}`)
else logToFile(`Run an missing files cleanup, deleted: {posters: ${removed.posters}, backdrops: ${removed.backdrops}, logos: ${removed.logos}}`)
return removed;
}
async function deleteOrphanFiles(dry = false) {
// Getting the userDataPath to store the images
const dataPath = getSettings().userDataPath;
// ? Getting the path of all media files and putting them in an array
const allMedia = (await Media.findAll()).map(m =>
path.join(dataPath, 'images', 'entries', m.type + 's', m.path)
);
// ? Loop trough the files of the images folder and check
// ? if a file doesnt exist in the allMedia array and mark it to delete
let orphanFiles = [];
const mediaTypes = {
'poster': path.join(dataPath, 'images', 'entries', 'posters'),
'logo': path.join(dataPath, 'images', 'entries', 'logos'),
'backdrop': path.join(dataPath, 'images', 'entries', 'backdrops')
};
let removed = {
'posters': 0,
'logos': 0,
'backdrops': 0
};
// Iterate over each media type directory and check for orphan files
for (let type in mediaTypes) {
const directory = mediaTypes[type];
try {
const files = await fsPromises.readdir(directory).then();
// Check each file in the directory
for (let file of files) {
const filePath = path.join(directory, file);
// Check if the constructed file path exists in the allMedia array
if (!allMedia.includes(filePath.replace("_preview", ""))) {
// If not, delete the file
if (!dry) await fsPromises.unlink(filePath);
removed[type + 's']++; // Increment the count of removed files
}
}
} catch (err) {
console.error(`Error handling files in ${directory}:`, err);
}
}
if (dry) logToFile(`Run a dry orphan files check, found: {posters: ${removed.posters}, backdrops: ${removed.backdrops}, logos: ${removed.logos}}`)
else logToFile(`Run an orphan files cleanup, deleted: {posters: ${removed.posters}, backdrops: ${removed.backdrops}, logos: ${removed.logos}}`)
return removed;
}
module.exports = {
deleteMissingFilesEntries,
deleteOrphanFiles
};