refactor: changed /info router from POST to GET

This commit is contained in:
MasterAcnolo
2026-08-07 21:13:12 +02:00
committed by MasterAcnolo
parent c67a1511a5
commit df0952c231
3 changed files with 23 additions and 10 deletions

View File

@@ -30,15 +30,15 @@ function formatSize(bytes) {
*/
async function fetchVideoInfo(url) {
try {
const res = await fetch("/info", {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: new URLSearchParams({ url }),
const res = await fetch(`/info?url=${encodeURIComponent(url)}`, {
method: "GET",
headers: {"Accept": "application/json"}
});
const data = await res.json();
if (!res.ok) return {error: `An Error occured when fetching info`};
const data = await res.json();
if (!data) return { error: "Data is Missing" };
return data;

View File

@@ -18,7 +18,9 @@ const { isValidUrl } = require("../helpers/validation.helpers");
*/
async function infoController(req, res) {
const url = req.body.url || req.query.url; // Supports POST and GET requests
// Previously, i support both POST and GET element, but it's kinda weird. So now it only support GET.
const url = req.query.url;
const encodedUrl = encodeURIComponent(url)
/**
* Basic validation:
@@ -30,9 +32,10 @@ async function infoController(req, res) {
return res.status(400).send("Invalid URL Or Missing");
}
logger.info(`Info request received. URL: ${url}`);
logger.info(`Info request received. RAW URL: ${url}`);
logger.info(`Info request received. ENCODED: ${encodedUrl}`);
// Lightweight heuristic to detect playlist URLs
// Lightweight heuristic to detect playlist URLs. It works for Youtube.
const isPlaylistUrl = url.includes("&list") || url.includes("?list");
logger.info(
@@ -86,6 +89,16 @@ async function infoController(req, res) {
*/
if (data._type === "playlist") {
// If the playlist got only one video, show it as a video.
if (data.entries && data.entries.length === 1) {
const singleVideo = parseVideo(data.entries[0]);
logger.info(`Playlist with 1 item converted to video: ${singleVideo.title}`);
return res.json(singleVideo);
}
const playlist = parsePlaylist(data);
logger.info(

View File

@@ -2,6 +2,6 @@ const express = require("express");
const router = express.Router();
const { infoController } = require("../controller/info.controller");
router.post("/", infoController);
router.get("/", infoController);
module.exports = router;