Major Backend SI Update part 2: added controllers

This commit is contained in:
X4Lo committed 2025-02-15 22:42:57 +01:00
1 parent a51e207707
commit dbb00fdb61
38 files changed
+2129 -1231

No files matched your search

+4 -72
View File
@@ -1,80 +1,12 @@
const express = require('express');
const mediaRouter = express.Router();
const path = require('path');
const fs = require('fs');
const configService = require('../services/configService.js');
const { logToFile } = require('../services/logService');
const mediaOptimizerService = require('../services/mediaOptimizerService.js');
const _mediaOptimizerService = mediaOptimizerService;
const mediaController = require('../controllers/mediaController.js');
// ! /media
mediaRouter.get('/', mediaController.getMedias);
// ? 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 = configService.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] + (type == 'logos' ? '_preview.png' : '_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;
_mediaOptimizerService.doGeneratePreview(category, type, fileName).then((result) => {
const stream = fs.createReadStream(filePath);
stream.pipe(res);
logToFile(`Media Route | Generated Preview`);
}).catch((err) => {
logToFile(`Media Route | Error Generating Preview`);
logToFile(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);
}
});
});
mediaRouter.get('/:category/:type/:size/:fileName', mediaController.getMedia);
module.exports = mediaRouter;