implemented images maintenance, services + component
This commit is contained in:
1 parent
ba96f540ca
commit
34766ba8e9
13 files changed
+448
-4
No files matched your search
@@ -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
|
||||
};
|
||||
Reference in new issue
Block a user