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
@@ -13,15 +13,21 @@ async function deleteMissingFilesEntries(dry = false) {
|
||||
const dataPath = await getMediaFolder();
|
||||
|
||||
// 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')
|
||||
const mediaTypeFolder = {
|
||||
'entryposter': path.join(dataPath, 'images', 'entries', 'posters'),
|
||||
'entrylogo': path.join(dataPath, 'images', 'entries', 'logos'),
|
||||
'entrybackdrop': path.join(dataPath, 'images', 'entries', 'backdrops'),
|
||||
|
||||
'collectionposter': path.join(dataPath, 'images', 'collections', 'posters'),
|
||||
'collectionlogo': path.join(dataPath, 'images', 'collections', 'logos'),
|
||||
'collectionbackdrop': path.join(dataPath, 'images', 'collections', 'backdrops')
|
||||
};
|
||||
|
||||
let missingFiles = [];
|
||||
for (let i = 0; i < allMedia.length; i++) {
|
||||
let type = `${allMedia[i].type}`.replace("_preview", "");
|
||||
const filePath = path.join(mediaTypes[type], allMedia[i].path)
|
||||
const folderType = `${allMedia[i].mediaable}${allMedia[i].type.replace("_preview", "")}`;
|
||||
|
||||
const filePath = path.join(mediaTypeFolder[folderType], allMedia[i].path)
|
||||
if (!fs.existsSync(filePath)) {
|
||||
missingFiles.push(allMedia[i]);
|
||||
}
|
||||
@@ -33,40 +39,55 @@ async function deleteMissingFilesEntries(dry = false) {
|
||||
'backdrops': 0
|
||||
};
|
||||
|
||||
if (missingFiles.length == 0)
|
||||
return removed;
|
||||
// if (missingFiles.length == 0)
|
||||
// return removed;
|
||||
|
||||
for (let i = 0; i < missingFiles.length; i++) {
|
||||
removed[missingFiles[i].type] += 1;
|
||||
removed[missingFiles[i].type + 's'] += 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete orphan files from media folder data,
|
||||
* orphan files are files that exist in the `folder data`
|
||||
* but do not have an instance in the database
|
||||
* @param {boolean} [dry=false] - if true only return the count of the files that are getting deleted, default=`false`
|
||||
*/
|
||||
async function deleteOrphanFiles(dry = false) {
|
||||
// Getting the userDataPath to store the images
|
||||
const dataPath = getMediaFolder();
|
||||
|
||||
// ? 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)
|
||||
);
|
||||
// const allMedia = (await Media.findAll()).map(m => {
|
||||
// if (m.mediaable == 'entry')
|
||||
// return path.join(dataPath, 'images', 'entries', m.type + 's', m.path)
|
||||
// else if (m.mediaable == 'collection')
|
||||
// return path.join(dataPath, 'images', 'collections', m.type + 's', m.path)
|
||||
// });
|
||||
|
||||
// ? Getting the name (without extention) of all media in database
|
||||
const allMedia = (await Media.findAll()).map(m => {
|
||||
return m.path.split('.')[0]
|
||||
});
|
||||
|
||||
|
||||
// ? Loop trough the files of the images folder and check
|
||||
// ? if a file doesnt exist in the allMedia array and mark it to delete
|
||||
const mediaTypeFolder = {
|
||||
'entry;poster': path.join(dataPath, 'images', 'entries', 'posters'),
|
||||
'entry;logo': path.join(dataPath, 'images', 'entries', 'logos'),
|
||||
'entry;backdrop': path.join(dataPath, 'images', 'entries', 'backdrops'),
|
||||
|
||||
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')
|
||||
'collection;poster': path.join(dataPath, 'images', 'collections', 'posters'),
|
||||
'collection;backdrop': path.join(dataPath, 'images', 'collections', 'backdrops')
|
||||
};
|
||||
|
||||
// * counter
|
||||
let removed = {
|
||||
'posters': 0,
|
||||
'logos': 0,
|
||||
@@ -74,19 +95,31 @@ async function deleteOrphanFiles(dry = false) {
|
||||
};
|
||||
|
||||
// Iterate over each media type directory and check for orphan files
|
||||
for (let type in mediaTypes) {
|
||||
const directory = mediaTypes[type];
|
||||
for (let folderType in mediaTypeFolder) {
|
||||
const directory = mediaTypeFolder[folderType];
|
||||
try {
|
||||
// ? getting all files inside that folder
|
||||
const files = await fsPromises.readdir(directory).then();
|
||||
// console.log(`${directory}, files count: ${files.length}`);
|
||||
|
||||
// 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
|
||||
// const filePath = path.join(directory, file);
|
||||
// // Check if the constructed file path exists in the allMedia array
|
||||
// // ? this step also takes into account preview files, by removing '_preview'
|
||||
// // ? and verifying if the file path is in database
|
||||
// const hasJpgFormat = allMedia.includes(filePath.replace("_preview.jpg", ".jpg"));
|
||||
// const hasPngFormat = allMedia.includes(filePath.replace("_preview.jpg", ".png"));
|
||||
// const hasGifFormat = allMedia.includes(filePath.replace("_preview.jpg", ".gif"));
|
||||
|
||||
// preparing the file name
|
||||
const fileName = file.split('.')[0].replace("_preview", "");
|
||||
const isInDatabase = allMedia.includes(fileName);
|
||||
|
||||
// if the file isn't in database even on other extentions then delete it
|
||||
if (!isInDatabase) {
|
||||
removed[folderType.split(';')[1] + 's']++; // Increment the count of removed files
|
||||
if (!dry) await fsPromises.unlink(filePath);
|
||||
removed[type + 's']++; // Increment the count of removed files
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
|
||||
Reference in new issue
Block a user