From f1cd3f916790f4e9495eff102047b0f209343a83 Mon Sep 17 00:00:00 2001 From: X4Lo Date: Fri, 11 Jul 2025 23:38:00 +0100 Subject: [PATCH] Backend: minor fixes + Added people media folder auto creating & mutler upload function --- backend/routes/entry.routes.js | 4 +++- backend/services/configService.js | 3 ++- backend/services/maintenanceService.js | 19 +++++++------------ backend/services/mutlerStorageLocations.js | 17 +++++++++++++++++ 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/backend/routes/entry.routes.js b/backend/routes/entry.routes.js index 6626c65..5ae2f09 100644 --- a/backend/routes/entry.routes.js +++ b/backend/routes/entry.routes.js @@ -92,10 +92,13 @@ entryRouter.put('/:entryId/media/:mediaId', upload, async (req, res) => { return res.status(404).json({ message: 'Media not found' }); } + // ? setting all other media to not selected + // ? to not have multiple selected media of the same type await Media.update( { isSelected: false }, { where: { + mediaable: 'entry', mediaableId: entryId, type: media.type } @@ -103,7 +106,6 @@ entryRouter.put('/:entryId/media/:mediaId', upload, async (req, res) => { ); const updatedMedia = await media.update({ isSelected: true }); - return res.status(200).json(updatedMedia); }); diff --git a/backend/services/configService.js b/backend/services/configService.js index 760baba..64b8e02 100644 --- a/backend/services/configService.js +++ b/backend/services/configService.js @@ -73,7 +73,6 @@ class ConfigService { getMediaFolder() { return this.getSettings().userDataPath; } - createMediaFolders() { const settings = this.getSettings(); const dataPath = settings.userDataPath; @@ -85,6 +84,7 @@ class ConfigService { const collectionsBackdropsDir = path.join(dataPath, 'images', 'collections', 'backdrops'); const seasonsPostersDir = path.join(dataPath, 'images', 'seasons', 'posters'); const episodesStillsDir = path.join(dataPath, 'images', 'episodes', 'stills'); + const peopleProfilesDir = path.join(dataPath, 'images', 'people', 'profiles'); fs.mkdirSync(posterDir, { recursive: true }); fs.mkdirSync(backdropDir, { recursive: true }); @@ -93,6 +93,7 @@ class ConfigService { fs.mkdirSync(collectionsBackdropsDir, { recursive: true }); fs.mkdirSync(seasonsPostersDir, { recursive: true }); fs.mkdirSync(episodesStillsDir, { recursive: true }); + fs.mkdirSync(peopleProfilesDir, { recursive: true }); } getTmdbApiUrl() { diff --git a/backend/services/maintenanceService.js b/backend/services/maintenanceService.js index 69fea36..1423669 100644 --- a/backend/services/maintenanceService.js +++ b/backend/services/maintenanceService.js @@ -20,17 +20,15 @@ class MaintenanceService { /** * Delete orphan media records from database, - * orphan media records are media records that exist in the database - * but do not have a file in the `folder data` + * these are media records that exist in the database + * but do not have a file in the `user data folder`. * @param {boolean} [dry=false] - if true only return the count of the files that are getting deleted, default=`false` */ async deleteOrphanMediaRecords(dry = false) { const allMedia = await Media.findAll(); - // Getting the userDataPath to store the images const dataPath = await _configService.getMediaFolder(); - // Handling different media types const mediaTypeFolder = { 'entry;poster': path.join(dataPath, 'images', 'entries', 'posters'), 'entry;logo': path.join(dataPath, 'images', 'entries', 'logos'), @@ -45,9 +43,10 @@ class MaintenanceService { let missingFiles = []; for (let i = 0; i < allMedia.length; i++) { - const folderType = `${allMedia[i].mediaable}${allMedia[i].type.replace("_preview", "")}`; + const folderType = `${allMedia[i].mediaable};${allMedia[i].type.replace("_preview", "")}`; + + if (!mediaTypeFolder[folderType]) continue; - if (!mediaTypeFolder[folderType]) continue const filePath = path.join(mediaTypeFolder[folderType], allMedia[i].path) if (!fs.existsSync(filePath)) { missingFiles.push(allMedia[i]); @@ -61,9 +60,6 @@ class MaintenanceService { 'stills': 0, }; - // if (missingFiles.length == 0) - // return removed; - for (let i = 0; i < missingFiles.length; i++) { removed[missingFiles[i].type + 's'] += 1; if (!dry) missingFiles[i].destroy(); @@ -82,7 +78,6 @@ class MaintenanceService { * @param {boolean} [dry=false] - if true only return the count of the files that are getting deleted, default=`false` */ async deleteOrphanFiles(dry = false) { - // Getting the userDataPath to store the images const dataPath = _configService.getMediaFolder(); // ? Getting the name (without extention) of all media in database @@ -118,7 +113,7 @@ class MaintenanceService { try { // ? getting all files inside that folder const files = await fsPromises.readdir(directory).then(); - // console.log(`${directory}, files count: ${files.length}`); + // console.log(`found ${files.length} files in ${directory}`); // Check each file in the directory for (let file of files) { @@ -131,7 +126,7 @@ class MaintenanceService { if (!isInDatabase) { removed[folderType.split(';')[1] + 's']++; // Increment the count of removed files if (!dry) { - const b = path.join(directory, file); + const filePath = path.join(directory, file); await fsPromises.unlink(filePath); } } diff --git a/backend/services/mutlerStorageLocations.js b/backend/services/mutlerStorageLocations.js index 0a985b1..431bb45 100644 --- a/backend/services/mutlerStorageLocations.js +++ b/backend/services/mutlerStorageLocations.js @@ -44,7 +44,24 @@ const collectionPicturesUpload = multer({ }) }); +const peoplePicturesUpload = multer({ + storage: + multer.diskStorage({ + destination: function (req, file, cb) { + let folder = 'profiles'; + const dataPath = configService.getSettings().userDataPath; + cb(null, path.join(dataPath, 'images', 'people', folder)); + }, + filename: function (req, file, cb) { + const uniqueFilename = uuidv4(); + const extension = file.originalname.split('.').pop(); + cb(null, `${uniqueFilename}.${extension}`); + } + }) +}); + module.exports = { entryPicturesUpload, collectionPicturesUpload, + peoplePicturesUpload, } \ No newline at end of file