File size: 2,905 Bytes
a0b358c
 
62393c4
 
a0b358c
 
 
d7e1d6b
 
 
 
 
 
 
 
 
 
a0b358c
 
d7e1d6b
 
 
 
 
 
 
 
 
 
 
 
 
 
5c4343f
d7e1d6b
 
 
 
5c4343f
d7e1d6b
 
7766270
d7e1d6b
 
 
 
 
 
5c4343f
d7e1d6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7766270
5c4343f
d7e1d6b
 
 
 
 
a0b358c
d7e1d6b
 
 
a0b358c
 
5c4343f
 
d7e1d6b
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
74
75
76
77
78
79
80
81
82
83
84
85
86
const express = require('express');
const request = require('request');
require('dotenv').config();

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

async function getFileSize(link) {
  try {
    const res = await request.head(link);
    const contentLength = parseInt(res.headers['content-length'] || 0.1);
    eachfilesize[link] = contentLength;
    return contentLength;
  } catch (err) {
    console.error(err);
    return 0;
  }
}

app.get('/', async (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'] || ''
    };
    
    try {
      const originalResponse = await request.get({ url: originalUrl, headers });

      let headers_dict = {};
      for (const [key, value] of Object.entries(originalResponse.headers)) {
        headers_dict[key] = value;
      }

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

      const newResponse = new Response(originalResponse.body, { headers: headers_dict });
      newResponse.headers.delete('Date');
      newResponse.headers.delete('Etag');
      newResponse.headers.delete('Last-Modified');
      newResponse.headers.delete('Cf-Cache-Status');
      newResponse.headers.delete('Content-Disposition');
      newResponse.headers.delete('Nel');
      newResponse.headers.delete('Cf-Ray');
      newResponse.headers.delete('Report-To');
      newResponse.headers.delete('X-Amz-Cf-Id');
      newResponse.headers.delete('X-Amz-Cf-Pop');
      newResponse.headers.delete('X-Amz-Server-Side-Encryption');
      newResponse.headers.delete('X-Amz-Storage-Class');
      newResponse.headers.delete('X-Cache');
      newResponse.headers.set('Cache-Control', 'private, max-age=21188');
      newResponse.headers.set('Connection', 'close');
      newResponse.headers.set('Server', 'gvs 1.0');
      newResponse.headers.set('Vary', 'Origin');
      newResponse.headers.set('X-Content-Type-Options', 'nosniff');
      newResponse.headers.set('Access-Control-Allow-Origin', '*');

      res.writeHead(newResponse.status, newResponse.headers);
      newResponse.body.pipe(res);
    } catch (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}`);
});