add media page + backend + minor changes to media related stuff & sidebar

This commit is contained in:
X4Lo committed 2024-09-10 00:43:39 +01:00
1 parent c46309e458
commit eba9721a3c
20 files changed
+597 -73

No files matched your search

+6 -4
View File
@@ -5,18 +5,20 @@ const Media = sequelize.define("media",
{
id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
entry_id: {
entryId: {
type: DataTypes.INTEGER,
references: {
model: 'Entries',
key: 'id'
}
},
field: 'entry_id'
},
type: { type: DataTypes.STRING(50) },
path: { type: DataTypes.STRING(200) },
is_local: { type: DataTypes.BOOLEAN },
is_selected: { type: DataTypes.BOOLEAN },
isLocal: { type: DataTypes.BOOLEAN, field: 'is_local' },
isSelected: { type: DataTypes.BOOLEAN, field: 'is_selected', defaultValue: false },
isCustom: { type: DataTypes.BOOLEAN, field: 'is_custom', defaultValue: false },
}
);
+24
View File
@@ -0,0 +1,24 @@
const express = require('express');
const EntrySeen = require('../models/EntrySeen');
const Entry = require('../models/Entry');
const Media = require('../models/Media');
const customMediaRouter = express.Router();
// ! /custom-media
// * GET - ALL CUSTOM MEDIA
customMediaRouter.get('/', async (req, res) => {
const allCustomMedia = await Media.findAll({
where: {
isCustom: true
},
include: [
{ model: Entry, as: 'entry' }
]
})
res.status(200).json(allCustomMedia);
});
module.exports = customMediaRouter;
+77
View File
@@ -0,0 +1,77 @@
const express = require('express');
const path = require('path');
const fs = require('fs');
const { getSettings } = require('../services/settingsService');
const { generatePreview } = require('../services/mediaService');
const mediaRouter = express.Router();
// ! /media
// ? Routes : media access routes
mediaRouter.get('/:category/:type/:size/:fileName', (req, res, next) => {
const { category, type, size, fileName } = req.params;
if (!['entries', 'collections', 'franchises'].includes(category)) {
return res.status(404).send('Category Not Found');
}
if (!['backdrops', 'posters', 'logos', 'others'].includes(type)) {
return res.status(404).send('Type Not Found');
}
const settings = getSettings();
const dataPath = settings.userDataPath;
const mediaPath = path.join(dataPath, 'images', category, type);
// const mediaPath = path.join(__dirname, '..', `data/images/${category}/${type}`);
// * preparing to serve the preview
// ? basicaly im checking if the file requested is a preview file
// ? if so I make sure to change the extention to '_preview.jpg' (bcs all previews are jpg)
let filePath = path.join(mediaPath, fileName);
if (size == 'preview') {
const previewFileName = fileName.split('.')[0] + '_preview.jpg'
filePath = path.join(mediaPath, previewFileName);
}
fs.access(filePath, fs.constants.F_OK, (err) => {
// * generating a preview if it doesn't exist
// ? then if the preview doesn't exist I generate a new one
// ? by taking the file name and removing '_preview' from it to get
// ? the original file name, and starting from that I join it to the media path
// ? and generate a preview file, but still return 202 http code since the file
// ? is still generating
if (err && size == 'preview') {
const originalPath = path.join(mediaPath, fileName);
const previewFileName = fileName.split('.')[0] + '_preview.jpg'
const previewPath = path.join(mediaPath, previewFileName);
let previewWidth = 342;
if (type == 'backdrops') previewWidth = 780;
if (type == 'logos') previewWidth = 300;
generatePreview(originalPath, previewPath, previewWidth).then((result) => {
console.log(`generated a preview: `, previewFileName);
const stream = fs.createReadStream(filePath);
stream.pipe(res);
}).catch((err) => {
});;
// const stream = fs.createReadStream(filePath);
// stream.pipe(res);
// return res.status(202).send('Preview is still generating. Please try again later.');
} else {
const options = {
fallthrough: false, // ! Important: don't call next middleware if file isn't found
};
// express.static(filePath, options)(req, res, next);
// Serve the file using fs.createReadStream
const stream = fs.createReadStream(filePath);
stream.pipe(res);
}
});
});
module.exports = mediaRouter;
+5 -1
View File
@@ -9,9 +9,11 @@ const episodeSeenRouter = require('./episode-seen.routes');
const settingsRouter = require('./settings.routes');
const maintenanceRouter = require('./maintenance.routes');
const statisticsRouter = require('./statistics.routes');
const customMediaRouter = require('./custom-media.routes');
const mediaRouter = require('./media.routes');
const router = express.Router();
router.use('/media', mediaRouter);
router.use('/entries', entryRouter);
router.use('/user-collections', userCollectionRoutes);
router.use('/entry-seen', entrySeenRouter);
@@ -20,6 +22,8 @@ router.use('/entry-ratings', entryRatingsRouter);
router.use('/seasons', seasonRouter);
router.use('/episode-seen', episodeSeenRouter);
router.use('/custom-media', customMediaRouter);
router.use('/statistics', statisticsRouter);
router.use('/settings', settingsRouter);
+10 -1
View File
@@ -5,6 +5,7 @@ const path = require('path');
const { v4: uuidv4 } = require('uuid');
const { fetchEntryMedia } = require('./tmdbService');
const { getSettings } = require('./settingsService');
const sharp = require('sharp');
async function downloadImage(url, filePath) {
const response = await axios({
@@ -67,7 +68,7 @@ async function getEntryMedia(tmdb_id, is_tvserie, includes = ['posters', 'logos'
// Utility to find the preferred media (English if available, otherwise the first one)
function findPreferredMedia(mediaArray, preferredLanguage = 'en') {
return mediaArray.find(media => media.iso_639_1 === preferredLanguage) || mediaArray[0];
return mediaArray.find(media => media.iso_639_1 === preferredLanguage && !media.file_path.includes('.svg')) || mediaArray.find(media => !media.file_path.includes('.svg')) || mediaArray[0];
}
// Adjusted scheduleDownloads to accept local arrays
@@ -97,6 +98,13 @@ function scheduleDownloads(media, mediaType, mediaDir, downloadTasks, downloaded
);
}
function generatePreview(originalPath, previewPath, previewWidth, previewQuality = 80) {
return sharp(originalPath)
.resize({ width: previewWidth })
.jpeg({ quality: previewQuality })
.toFile(previewPath);
}
function getMediaFolder() {
return getSettings().userDataPath;
}
@@ -122,4 +130,5 @@ module.exports = {
getEntryMedia,
createMediaFolders,
getMediaFolder,
generatePreview
};