Backend: minor fixes + Added people media folder auto creating & mutler upload function

This commit is contained in:
X4Lo committed 2025-07-11 23:38:00 +01:00
1 parent d3dbd7c3e3
commit f1cd3f9167
4 files changed
+29 -14

No files matched your search

+3 -1
View File
@@ -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);
});
+2 -1
View File
@@ -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() {
+7 -12
View File
@@ -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);
}
}
@@ -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,
}