File size: 8,081 Bytes
bdd1186
 
 
 
 
 
 
 
 
 
 
ab5ee14
d91f103
ab5ee14
 
 
 
 
 
d91f103
bdd1186
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d91f103
 
 
 
bdd1186
 
 
 
 
 
 
 
 
 
 
d91f103
 
bdd1186
 
 
 
 
 
 
 
 
ab5ee14
 
 
 
 
 
 
 
bdd1186
 
 
 
 
 
d91f103
 
ab5ee14
d91f103
bdd1186
 
 
 
 
 
 
 
 
d91f103
bdd1186
 
d91f103
bdd1186
 
 
 
 
 
 
 
 
ab5ee14
 
 
 
 
 
 
 
bdd1186
 
 
 
 
 
 
 
 
 
d91f103
bdd1186
 
 
 
d91f103
 
 
 
 
 
 
 
bdd1186
ab5ee14
bdd1186
 
d91f103
56e7b36
 
bdd1186
 
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const express = require('express');
const axios = require('axios');
const app = express();

const TOKEN = process.env.HF_TOKEN; 
const REPO_ID = "amri12310/hf-vault-99281-xk9-p02-mlq8-r4x-v9928-zb-v2-alpha-sync";

app.get(['/stream/:u_id', '/stream/:u_id/:filename'], async (req, res) => {
    const { u_id } = req.params;
    const auth = TOKEN ? { "Authorization": `Bearer ${TOKEN}` } : {};

    // ⚡ CRITICAL: The instant kill-switch
    let clientConnected = true;
    const controller = new AbortController(); 

    req.on('close', () => { 
        clientConnected = false; 
        controller.abort(); // Instantly sever the Hugging Face download
    });

    try {
        let meta = null;
        let isLegacy = false;
        let allLegacyChunks = [];
        let totalSize = 0;

        // 1. Try New Format (meta.json)
        try {
            const r_meta = await axios.get(`https://huggingface.co/datasets/${REPO_ID}/resolve/main/${u_id}/meta.json`, { headers: auth, timeout: 5000 });
            meta = r_meta.data;
            totalSize = meta.size;
        } catch (e) {
            // 2. Fallback to Legacy Format (meta_part1.json)
            for (let p of [1, 2, 3]) {
                try {
                    const r_leg = await axios.get(`https://huggingface.co/datasets/${REPO_ID}/resolve/main/${u_id}/meta_part${p}.json`, { headers: auth, timeout: 5000 });
                    if (r_leg.data) {
                        totalSize = Math.max(totalSize, r_leg.data.total_size || 0);
                        allLegacyChunks.push(...(r_leg.data.chunks || []));
                    }
                } catch (err) {}
            }
            if (allLegacyChunks.length > 0) {
                isLegacy = true;
                allLegacyChunks.sort((a, b) => a.start - b.start);
            } else {
                return res.status(404).send("Metadata not found.");
            }
        }

        if (req.method === 'HEAD') {
            return res.status(200).set({ "Content-Length": totalSize, "Content-Type": "video/x-matroska", "Accept-Ranges": "bytes" }).end();
        }

        const rangeHeader = req.headers.range;
        let reqStart = 0;
        let reqEnd = totalSize - 1;

        if (rangeHeader) {
            const parts = rangeHeader.replace(/bytes=/, "").split("-");
            reqStart = parseInt(parts[0], 10);
            reqEnd = parts[1] ? parseInt(parts[1], 10) : totalSize - 1;
        }

        const bytesToSend = (reqEnd - reqStart) + 1;
        const responseHeaders = {
            "Content-Range": `bytes ${reqStart}-${reqEnd}/${totalSize}`,
            "Accept-Ranges": "bytes",
            "Content-Length": bytesToSend,
            "Content-Type": "video/x-matroska",
            "Connection": "keep-alive"
        };

        if (req.query.dl === '1') responseHeaders["Content-Disposition"] = `attachment; filename="${u_id}.mkv"`;
        res.writeHead(rangeHeader ? 206 : 200, responseHeaders);

        let bytesSent = 0;
        const mbToSend = (bytesToSend / 1024 / 1024).toFixed(2);
        const shortId = u_id.substring(0, 15);
        
        console.log(`▶️ [STREAM START] ${shortId}... | Range: ${reqStart}-${reqEnd} (${mbToSend} MB)`);

        // ==========================================
        // ROUTE A: NEW TURBO FORMAT
        // ==========================================
        if (!isLegacy) {
            const numChunks = meta.chunks;
            const chunkSizeInBytes = (meta.chunk_size_mb || 500) * 1024 * 1024;
            const startChunkIdx = Math.floor(reqStart / chunkSizeInBytes);
            const firstOffset = reqStart % chunkSizeInBytes;

            for (let i = startChunkIdx; i < numChunks; i++) {
                if (!clientConnected || bytesSent >= bytesToSend) break;
                
                const chunkUrl = `https://huggingface.co/datasets/${REPO_ID}/resolve/main/${u_id}/${i}.bin`;
                const currentOffset = (i === startChunkIdx) ? firstOffset : 0;
                
                const remainingBytesNeeded = bytesToSend - bytesSent;
                const bytesAvailableInChunk = chunkSizeInBytes - currentOffset;
                const bytesToFetchFromChunk = Math.min(remainingBytesNeeded, bytesAvailableInChunk);
                const chunkReqEnd = currentOffset + bytesToFetchFromChunk - 1;

                try {
                    const chunkStream = await axios({ 
                        method: 'get', 
                        url: chunkUrl, 
                        headers: { ...auth, "Range": `bytes=${currentOffset}-${chunkReqEnd}` }, 
                        responseType: 'stream',
                        signal: controller.signal // ⚡ Kill switch attached
                    });
                    
                    await new Promise((resolve, reject) => {
                        chunkStream.data.pipe(res, { end: false });
                        chunkStream.data.on('end', () => { bytesSent += bytesToFetchFromChunk; resolve(); });
                        chunkStream.data.on('error', reject);
                    });
                } catch (err) {
                    if (clientConnected) {
                        console.log(`⚠️ [NETWORK] Cloudflare dropped chunk ${i}. Retrying...`);
                        i--; 
                    }
                }
            }
        } 
        // ==========================================
        // ROUTE B: LEGACY SHARDED FORMAT
        // ==========================================
        else {
            let currentStart = reqStart;
            for (let chunk of allLegacyChunks) {
                if (!clientConnected || bytesSent >= bytesToSend) break;
                if (chunk.end <= currentStart) continue;

                while (currentStart < chunk.end && bytesSent < bytesToSend && clientConnected) {
                    const url = `https://huggingface.co/datasets/${REPO_ID}/resolve/main/${u_id}/${chunk.name}`;
                    const offsetInChunk = currentStart - chunk.start;
                    
                    const remainingBytesNeeded = bytesToSend - bytesSent;
                    const bytesAvailableInChunk = chunk.end - currentStart;
                    const bytesToFetch = Math.min(remainingBytesNeeded, bytesAvailableInChunk);
                    const chunkReqEnd = offsetInChunk + bytesToFetch - 1;

                    try {
                        const chunkStream = await axios({ 
                            method: 'get', 
                            url: url, 
                            headers: { ...auth, "Range": `bytes=${offsetInChunk}-${chunkReqEnd}` }, 
                            responseType: 'stream',
                            signal: controller.signal // ⚡ Kill switch attached
                        });
                        
                        await new Promise((resolve, reject) => {
                            chunkStream.data.pipe(res, { end: false });
                            chunkStream.data.on('end', () => { 
                                bytesSent += bytesToFetch; 
                                currentStart += bytesToFetch;
                                resolve(); 
                            });
                            chunkStream.data.on('error', reject);
                        });
                    } catch (err) {
                        if (clientConnected) console.log(`⚠️ [NETWORK] Legacy chunk dropped. Retrying...`);
                    }
                }
            }
        }
        
        if (clientConnected) {
            console.log(`✅ [STREAM FINISHED] ${shortId}...`);
            res.end();
        } else {
            console.log(`⏹️ [STREAM HALTED] TV closed connection early.`);
        }
        
    } catch (err) {
        if (!res.headersSent && clientConnected) res.status(500).send("Engine Malfunction");
    }
});

// ⚡ The Heartbeat Route
app.get('/', (req, res) => res.status(200).send("Engine E is awake!"));

app.listen(7860, () => console.log('🚀 Engine E (Node.js Egress) running on port 7860'));