const express = require("express"); const entryRouter = express.Router(); const { entryPicturesUpload } = require("../services/mutlerStorageLocations"); const Entry = require("../models/Entry"); const Media = require("../models/Media"); const { logToFile } = require("../services/logService"); const entryController = require("../controllers/entryController"); const mediaOptimizerService = require("../services/mediaOptimizerService"); // ! /entries // ! TMDB PROXY ROUTES (must be before /:id to avoid matching) entryRouter.get("/tmdb/trending/:type", entryController.getTmdbTrending); entryRouter.get("/tmdb/popular/:type", entryController.getTmdbPopular); entryRouter.get("/tmdb/top-rated/:type", entryController.getTmdbTopRated); entryRouter.get("/tmdb/upcoming/movie", entryController.getTmdbUpcoming); entryRouter.get("/tmdb/search", entryController.getTmdbSearch); entryRouter.get("/tmdb/preview/:type/:tmdbId", entryController.getTmdbPreview); entryRouter.get("/sync-series", entryController.syncSeries); entryRouter.get("/", entryController.getAllEntries); entryRouter.get("/:id", entryController.getEntryById); entryRouter.post("/", entryController.fetchAndSaveNewEntry); entryRouter.put("/:id", entryController.updateEntry); entryRouter.put("/:id/ratings", entryController.updateEntryRatings); entryRouter.put("/:id/ids", entryController.updateEntryIds); entryRouter.delete("/:id", entryController.deleteEntry); entryRouter.get("/:id/seasons", entryController.getEntrySeasons); entryRouter.get("/:id/credits", entryController.getEntryCredits); entryRouter.get( "/:id/recommendations", entryController.getEntryRecommendations, ); entryRouter.put("/:id/refresh", entryController.refrechEntry); entryRouter.put("/:id/fetch-collection", entryController.fetchEntryCollection); // ! ENTRY MEDIA // * POST - ADD NEW ENTRY MEDIA (poster/backdrop/logo) const upload = entryPicturesUpload.fields([ { name: "poster", maxCount: 1 }, { name: "backdrop", maxCount: 1 }, { name: "logo", maxCount: 1 }, ]); entryRouter.post("/:id/media", upload, async (req, res) => { const { id } = req.params; if (!req.files) { return res.status(400).json({ message: "No files uploaded" }); } const entry = await Entry.findByPk(id); if (!entry) { return res.status(404).json({ message: "Entry not found" }); } // Function to handle media creation based on uploaded file type const fileType = ["poster", "backdrop", "logo"].find( (type) => req.files[type], ); if (!fileType) { return res.status(400).json({ message: "Invalid media type" }); } const file = req.files[fileType][0]; const createdMedia = await Media.create({ mediaable: "entry", mediaableId: id, type: fileType, path: file.filename, isLocal: true, isSelected: false, isCustom: true, }); // ? generating low resolution preview mediaOptimizerService .doGeneratePreview("entries", fileType + "s", file.filename) .then((result) => { const stream = fs.createReadStream(filePath); stream.pipe(res); const msg = `Media Route | Generated Entry Preview`; logToFile(msg); }) .catch((err) => { const msg = `Media Route | Error Generating Entry Preview`; logToFile(msg); }); return res.status(200).json(createdMedia); }); // * PUT - SET ENTRY MEDIA AS SELECTED entryRouter.put("/:entryId/media/:mediaId", upload, async (req, res) => { const entryId = req.params.entryId; const mediaId = req.params.mediaId; const entry = await Entry.findByPk(entryId); if (!entry) { return res.status(404).json({ message: "Entry not found" }); } const media = await Media.findByPk(mediaId); if (!media) { 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, }, }, ); const updatedMedia = await media.update({ isSelected: true }); return res.status(200).json(updatedMedia); }); // * DELETE - DELETE AND ENTRY MEDIA entryRouter.delete("/:entryId/media/:mediaId", async (req, res) => { const mediaId = req.params.mediaId; // ? just before deleting we need to check if the deleted media was the one selected // ? if so select a new one if there is, then delete const media = await Media.findByPk(mediaId); if (media && media.isSelected) { let otherMedia = await Media.findOne({ where: { mediaable: "entry", mediaableId: media.mediaableId, type: media.type, isSelected: false, }, }); // ? if found select the first one if (otherMedia) { otherMedia.update({ isSelected: true }); } } await Media.destroy({ where: { id: mediaId } }); res.status(200).json(); }); module.exports = entryRouter;