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
@@ -15,6 +15,7 @@ const Media = require('../models/Media');
|
||||
const Episode = require('../models/Episode');
|
||||
|
||||
// ! /entries
|
||||
|
||||
// * GET - ALL ENTRIES
|
||||
entryRouter.get('/', async (req, res) => {
|
||||
const { page, pageSize } = req.body;
|
||||
@@ -129,7 +130,6 @@ entryRouter.get('/:id/seasons', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// ! POST
|
||||
// * POST - CREATE ENTRY
|
||||
entryRouter.post('/', async (req, res) => {
|
||||
const { tmdb_id, is_tvserie } = req.body;
|
||||
@@ -181,6 +181,7 @@ entryRouter.post('/bulk-import-episodes-seen', async (req, res) => {
|
||||
res.status(201).json(createdRows);
|
||||
});
|
||||
// + import routes end
|
||||
|
||||
// * PUT - ENTRY UPDATE
|
||||
entryRouter.put('/:id', async (req, res) => {
|
||||
const { id } = req.params;
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
const express = require('express');
|
||||
const { deleteMissingFilesEntries, deleteOrphanFiles } = require('../services/maintenanceService');
|
||||
const maintenanceRouter = express.Router();
|
||||
|
||||
// ! /maintenance
|
||||
// * GET - Orphan files
|
||||
maintenanceRouter.get('/orphan-files', async (req, res) => {
|
||||
const deleted = await deleteOrphanFiles(true);
|
||||
|
||||
return res.status(200).json(deleted);
|
||||
});
|
||||
|
||||
// * GET - Missing files
|
||||
maintenanceRouter.get('/missing-files', async (req, res) => {
|
||||
const deleted = await deleteMissingFilesEntries(true);
|
||||
|
||||
return res.status(200).json(deleted);
|
||||
});
|
||||
|
||||
// * DELETE - Orphan files
|
||||
maintenanceRouter.delete('/orphan-files', async (req, res) => {
|
||||
const deleted = await deleteOrphanFiles();
|
||||
|
||||
return res.status(200).json(deleted);
|
||||
});
|
||||
|
||||
// * DELETE - Missing files
|
||||
maintenanceRouter.delete('/missing-files', async (req, res) => {
|
||||
const deleted = await deleteMissingFilesEntries();
|
||||
|
||||
return res.status(200).json(deleted);
|
||||
});
|
||||
|
||||
module.exports = maintenanceRouter;
|
||||
@@ -7,6 +7,7 @@ const entryRatingsRouter = require('./entry-ratings.routes');
|
||||
const seasonRouter = require('./season.routes');
|
||||
const episodeSeenRouter = require('./episode-seen.routes');
|
||||
const settingsRouter = require('./settings.routes');
|
||||
const maintenanceRouter = require('./maintenance.routes');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
@@ -20,5 +21,6 @@ router.use('/seasons', seasonRouter);
|
||||
router.use('/episode-seen', episodeSeenRouter);
|
||||
|
||||
router.use('/settings', settingsRouter);
|
||||
router.use('/maintenance', maintenanceRouter)
|
||||
|
||||
module.exports = router;
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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