Spaces:
Sleeping
Sleeping
Update index.js
Browse files
index.js
CHANGED
@@ -2,62 +2,71 @@ const express = require('express');
|
|
2 |
const request = require('request');
|
3 |
require('dotenv').config();
|
4 |
|
5 |
-
|
6 |
const app = express();
|
7 |
-
const PORT = 7860;
|
8 |
-
|
9 |
const eachfilesize = {};
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
});
|
26 |
}
|
27 |
|
28 |
-
app.get('
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
'Range': req.headers.range || ''
|
40 |
-
};
|
41 |
-
|
42 |
-
try {
|
43 |
-
request.get({ url: originalUrl, headers: headers }, (err, response, body) => {
|
44 |
-
if (err) {
|
45 |
-
console.error(err);
|
46 |
-
return res.status(500).send('500');
|
47 |
-
}
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
}
|
56 |
-
} else {
|
57 |
-
return res.status(400).send('400');
|
58 |
-
}
|
59 |
});
|
60 |
|
61 |
-
|
62 |
-
|
|
|
63 |
});
|
|
|
2 |
const request = require('request');
|
3 |
require('dotenv').config();
|
4 |
|
|
|
5 |
const app = express();
|
|
|
|
|
6 |
const eachfilesize = {};
|
7 |
|
8 |
+
function getFileSize(links) {
|
9 |
+
let fileSize = 0;
|
10 |
+
for (const link of links) {
|
11 |
+
request.head(link, (err, res) => {
|
12 |
+
if (err) {
|
13 |
+
console.error(err);
|
14 |
+
return;
|
15 |
+
}
|
16 |
+
const contentLength = parseInt(res.headers['content-length'] || 0.1);
|
17 |
+
fileSize += contentLength;
|
18 |
+
eachfilesize[link] = contentLength;
|
19 |
+
});
|
20 |
+
}
|
21 |
+
return fileSize;
|
|
|
22 |
}
|
23 |
|
24 |
+
app.get('/', (req, res) => {
|
25 |
+
const shared = req.query.shared;
|
26 |
+
const filename = req.query.filename;
|
27 |
+
|
28 |
+
if (shared) {
|
29 |
+
const originalUrl = `https://huggingface.co/tranquan24/video/resolve/main/${shared}`;
|
30 |
+
const token = process.env.TOKEN;
|
31 |
+
const headers = {
|
32 |
+
'Authorization': `Bearer ${token}`,
|
33 |
+
'Range': req.headers['range'] || ''
|
34 |
+
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
+
request.get({
|
37 |
+
url: originalUrl,
|
38 |
+
headers: headers
|
39 |
+
}).on('response', (originalResponse) => {
|
40 |
+
const links = [originalUrl];
|
41 |
+
const fileSize = getFileSize(links);
|
42 |
+
|
43 |
+
let headers_dict = originalResponse.headers;
|
44 |
+
headers_dict['X-Speed'] = 'Fast';
|
45 |
+
headers_dict['X-Bandwidth'] = 'High';
|
46 |
+
headers_dict['Access-Control-Allow-Origin'] = '*';
|
47 |
+
|
48 |
+
if (filename) {
|
49 |
+
headers_dict['Content-Disposition'] = `attachment; filename="${filename}"`;
|
50 |
+
} else {
|
51 |
+
const filenameFromUrl = shared.split('/').pop();
|
52 |
+
headers_dict['Content-Disposition'] = `attachment; filename="${filenameFromUrl}"`;
|
53 |
+
}
|
54 |
+
|
55 |
+
delete headers_dict['set-cookie'];
|
56 |
+
|
57 |
+
const { statusCode, headers } = originalResponse;
|
58 |
+
res.writeHead(statusCode, headers);
|
59 |
+
originalResponse.pipe(res);
|
60 |
+
}).on('error', (err) => {
|
61 |
+
console.error(err);
|
62 |
+
res.status(500).send('Internal Server Error');
|
63 |
+
});
|
64 |
+
} else {
|
65 |
+
res.status(400).send('Missing shared parameter');
|
66 |
}
|
|
|
|
|
|
|
67 |
});
|
68 |
|
69 |
+
const port = 7860;
|
70 |
+
app.listen(port, '0.0.0.0', () => {
|
71 |
+
console.log(`Server is running on port ${port}`);
|
72 |
});
|