-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
92 lines (75 loc) · 2.71 KB
/
Copy pathserver.js
File metadata and controls
92 lines (75 loc) · 2.71 KB
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
const path = require('path');
const express = require('express');
const resolveHandler = require('./api/resolve');
const mp4Handler = require('./api/mp4/[fileId]');
const m3uHandler = require('./api/m3u/[fileId]');
const { createStreamingProxy } = require('./lib/streaming-proxy');
const { extractDriveParams } = require('./lib/drive');
const app = express();
const port = process.env.PORT || 3000;
// Create a shared streaming proxy instance
const streamProxy = createStreamingProxy();
app.use(express.json());
app.use(express.static(__dirname));
app.get('/api/resolve', (req, res) => {
resolveHandler(req, res);
});
app.get('/api/mp4/:fileId', (req, res) => {
req.query = { ...req.query, fileId: req.params.fileId };
mp4Handler(req, res);
});
// M3U8 playlist for video players (HLS)
app.get('/api/m3u/:fileId', (req, res) => {
req.query = { ...req.query, fileId: req.params.fileId };
m3uHandler(req, res);
});
// New streaming endpoint with HTTP proxy for smoother playback
app.get('/api/stream/:fileId', async (req, res) => {
try {
const rawId = req.query.fileId || req.query.id || req.query.url || req.params.fileId;
const { fileId, resourceKey } = extractDriveParams(rawId);
if (!fileId) {
res.status(400).json({ error: 'Missing or invalid Google Drive file ID.' });
return;
}
// Use the streaming proxy for direct streaming with optimized headers
await streamProxy.streamDirect(fileId, resourceKey, req, res);
} catch (error) {
if (!res.headersSent) {
res.status(502).json({
error: 'Failed to stream video',
detail: error.message
});
}
}
});
// Alias for streaming (shorter URL)
app.get('/stream/:fileId', async (req, res) => {
req.query = { ...req.query, fileId: req.params.fileId };
// Forward to the stream handler via query
const rawId = req.query.fileId || req.query.id || req.query.url;
const { fileId, resourceKey } = extractDriveParams(rawId);
if (!fileId) {
res.status(400).json({ error: 'Missing or invalid Google Drive file ID.' });
return;
}
await streamProxy.streamDirect(fileId, resourceKey, req, res);
});
app.get('/mp4/:fileId.mp4', (req, res) => {
req.query = { ...req.query, fileId: req.params.fileId, ext: 'mp4' };
mp4Handler(req, res);
});
app.get('/mp4/:fileId.mov', (req, res) => {
req.query = { ...req.query, fileId: req.params.fileId, ext: 'mov' };
mp4Handler(req, res);
});
app.get('/mp4/:fileId.mkv', (req, res) => {
req.query = { ...req.query, fileId: req.params.fileId, ext: 'mkv' };
mp4Handler(req, res);
});
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
app.listen(port, () => {
console.log(`gdrive-mp4-extractor running at http://localhost:${port}`);
});