File size: 2,299 Bytes
a0b358c
 
62393c4
 
a0b358c
 
 
2619711
 
 
 
 
 
 
 
 
 
 
 
 
 
a0b358c
 
2619711
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d7e1d6b
2619711
 
 
 
 
 
5c4343f
2619711
5c4343f
2619711
 
 
 
 
 
 
 
 
a0b358c
 
 
5c4343f
 
2619711
a0b358c
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
const express = require('express');
const request = require('request');
require('dotenv').config();

const app = express();
const eachfilesize = {};

function getFileSize(links) {
    let fileSize = 0;
    for (const link of links) {
        request.head(link, (err, res) => {
            if (err) {
                console.error(err);
                return;
            }
            const contentLength = parseInt(res.headers['content-length'] || 0.1);
            fileSize += contentLength;
            eachfilesize[link] = contentLength;
        });
    }
    return fileSize;
}

app.get('/', (req, res) => {
    const shared = req.query.shared;
    const filename = req.query.filename;

    if (shared) {
        const originalUrl = `https://huggingface.co/tranquan24/video/resolve/main/${shared}`;
        const token = process.env.TOKEN;
        const headers = {
            'Authorization': `Bearer ${token}`,
            'Range': req.headers['range'] || ''
        };
        
        request.get({
            url: originalUrl,
            headers: headers
        }).on('response', (originalResponse) => {
            const links = [originalUrl];
            const fileSize = getFileSize(links);

            let headers_dict = originalResponse.headers;
            headers_dict['X-Speed'] = 'Fast';
            headers_dict['X-Bandwidth'] = 'High';
            headers_dict['Access-Control-Allow-Origin'] = 'https://embed.giize.com';

            if (filename) {
                headers_dict['Content-Disposition'] = `attachment; filename="${filename}"`;
            } else {
                const filenameFromUrl = shared.split('/').pop();
                headers_dict['Content-Disposition'] = `attachment; filename="${filenameFromUrl}"`;
            }

            delete headers_dict['set-cookie'];

            const { statusCode, headers } = originalResponse;
            res.writeHead(statusCode, headers);
            originalResponse.pipe(res);
        }).on('error', (err) => {
            console.error(err);
            res.status(500).send('Internal Server Error');
        });
    } else {
        res.status(400).send('Missing shared parameter');
    }
});

const port = 7860;
app.listen(port, '0.0.0.0', () => {
    console.log(`Server is running on port ${port}`);
});