-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew.js
103 lines (86 loc) · 2.93 KB
/
new.js
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
const express = require('express');
const { MongoClient, MongoRuntimeError } = require('mongodb');
const mongodb = require('mongodb');
const fs = require('fs');
const app = express();
const path = require('path');
const multer = require('multer');
const port = 3000;
const url = 'mongodb://localhost:27017';
let db;
// Connect to MongoDB at the start of the application
MongoClient.connect(url)
.then((client) => {
console.log('Connected successfully to MongoDB');
db = client.db('videos');
})
.catch((error) => {
console.error('Error connecting to MongoDB:', error);
process.exit(1); // Exit the application if there's an error connecting to the database
});
app.get('/', (req, res) => {
const indexPath = path.join(__dirname, './index.html');
res.sendFile(indexPath);
});
const upload = multer({ dest: 'uploads/' });
app.post('/init-video', upload.single('video'), (req, res) => {
const originalFilename = req.file.originalname;
const bucket = new mongodb.GridFSBucket(db);
const videoUploadStream = bucket.openUploadStream(originalFilename);
const videoReadStream = fs.createReadStream(req.file.path);
videoReadStream.pipe(videoUploadStream);
videoUploadStream.on('error', (error) => {
console.error('Error during video upload:', error);
res.sendStatus(500);
});
videoUploadStream.on('finish', () => {
console.log('Upload done');
fs.unlinkSync(req.file.path);
res.sendStatus(200);
});
});
app.get('/video/:filename', (req, res) => {
const filename = req.params.filename;
const bucket = new mongodb.GridFSBucket(db);
const downloadStream = bucket.openDownloadStreamByName(filename);
downloadStream.on('error', (error) => {
if (error instanceof MongoRuntimeError && error.code === 'FileNotFound') {
console.error(`File not found in GridFS: ${filename}`, error);
res.sendStatus(404);
} else {
console.error(`Error during video streaming for ${filename}:`, error);
res.sendStatus(500);
}
});
res.set('Content-Type', 'video/mp4');
res.set('Content-Disposition', `inline; filename=${filename}`);
downloadStream.pipe(res);
});
app.get('/video', async (req, res) => {
try {
const bucket = new mongodb.GridFSBucket(db);
const files = await db.collection('fs.files').find().toArray();
res.send(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Available Videos</title>
</head>
<body>
<h1>Available Videos</h1>
<ul>
${files.map(file => `<li><a href="/video/${file.filename}">${file.filename}</a></li>`).join('')}
</ul>
</body>
</html>
`);
} catch (error) {
console.error('Error retrieving available videos:', error);
res.sendStatus(500);
}
});
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});