File size: 2,955 Bytes
92a83a5
 
 
 
 
 
 
 
 
 
 
 
 
 
1ce7a42
92a83a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d73de17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const { fetchTmdbData } = require('../handlers/mainHandle');
const { processVideo } = require('../utils/ts');
const { getCachedLink } = require('../utils/cache');

/**
 * Mapa de rutas: Local -> TMDB Endpoint
 * Nota: No incluir '/' al inicio de los valores para evitar URLs malformadas.
 */
const tmdbEndpointMap = {
    '/movie/popular': 'movie/popular',
    '/tv/popular': 'tv/popular',
    '/trending/movie/day': 'trending/movie/day',
    '/trending/tv/day': 'trending/tv/day',
    '/trending/all/day': 'trending/all/day',
    '/trending/all/week': 'trending/all/week',
    '/trending/movie/week': 'trending/movie/week',
    '/trending/tv/week': 'trending/tv/week',
    '/genre/movie/list': 'genre/movie/list',
    '/discover/movie': 'discover/movie',
    '/discover/tv': 'discover/tv',
    '/movie/upcoming': 'movie/upcoming',
    '/movie/now_playing': 'movie/now_playing',
    '/tv': 'tv',
    '/movie': 'movie',
    '/search/multi': 'search/multi',
};

// --- CONTROLADORES ---

async function getTmdbContent(req, res) {
    try {
        // 1. Extraer la ruta base (ej: /search/multi)
        const basePath = req.route.path.replace(/\/:tmdbid.*$/, '');
        let endpoint = tmdbEndpointMap[basePath];

        if (!endpoint) {
            return res.status(404).json({ error: "Ruta no mapeada en el servidor." });
        }

        // 2. Construir el endpoint con IDs si existen (Evita añadir undefined)
        const { tmdbid, seasonid } = req.params;
        
        if (tmdbid) {
            endpoint += `/${tmdbid}`;
            if (seasonid) {
                endpoint += `/season/${seasonid}`;
            }
        }

        // 3. Consultar a TMDB (req.query ya trae 'query', 'page', 'language', etc.)
        const data = await fetchTmdbData(endpoint, req.query);

        if (!data) {
            return res.status(404).json({ error: "No se encontraron resultados en TMDB." });
        }

        res.setHeader('Access-Control-Allow-Origin', '*');
        return res.json(data);

    } catch (error) {
        console.error(`[TMDB Controller Error]: ${error.message}`);
        return res.status(500).json({ 
            error: "Error al procesar la solicitud con TMDB.",
            details: error.message 
        });
    }
}

async function getCacheStream(req, res) {
    const { locker, id } = req.params;
    const cacheKey = `${locker}-get${id}`;
    
    const cachedItem = getCachedLink(cacheKey);

    if (!cachedItem || !cachedItem.result) {
        return res.status(404).json({ error: `Contenido no encontrado o expirado para el ID: ${id}` });
    }

    try {
        const processedLink = await processVideo(cachedItem.result);
        return res.json({ link: processedLink });
    } catch (error) {
        console.error(`[Stream Error]: ${error.message}`);
        return res.status(500).json({ error: 'Error al procesar el enlace de video.' });
    }
}

module.exports = {
    getCacheStream,
    getTmdbContent
};