Reputation: 361
Let me start off with apologizing for my title. I'm having this weird MongoDB bug, and I truly have no idea what to name this post.
I'm trying to make a simple video streaming platform using GridFS-Stream. Streaming the actual video isn't a problem. Once I go to the video's desired link, it plays the video from the database. But the problem is that even though the video is found in MongoDB, node.js still thinks it didn't find anything.
I'm truly at a loss for words. I've been searching everywhere around the internet, and I've been stuck on this bug for the past few days.
If someone could help guide me to the right direction on solving this bug, it would be greatly appreciated.
Console Logs (excuse the warnings)
Clipit Webserver | v1.0.0
----------------------------------------------
> Server is up and running
(node:16292) [MONGODB DRIVER] Warning: Top-level use of w, wtimeout, j, and fsync is deprecated. Use writeConcern instead.
(node:16292) [MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true }
to the MongoClient constructor.
> Succesfully connected to the database
127.0.0.1 has connected to: 60df1dbe017d5035d834b5ae
Video not found
(node:16292) DeprecationWarning: GridStore is deprecated, and will be removed in a future version. Please use GridFSBucket instead
> 127.0.0.1 has connected to: /
Notice how it first finds the video and even sends the video id? I have no idea why the second it finds the video, it says the video is not found. I have completely no idea what's this bug and how to fix it.
Video route & pipestream
//@ROUTE: Video route
//@DESCRIPTION: using the stream route, finds the video in the database and streams the video to the client
app.get('/v/:id', (req, res)=> {
const videoId = req.params.id
gfs.files.findOne({'test': videoId}, (err, file)=> {
if(err) {
throw err
}
if(file) {
console.log(`${req.ip} has connected to: ${file._id}`)
res.render('Video.ejs', {file: file, title: "Video :: Clipit", curSession: req.session})
} else {
console.log('Video not found')
res.redirect('/')
}
})
})
//@ROUTE: Video stream route
//@DESCRIPTION: Finds the video in the database, and creates a pipe stream, used in the video route
app.get('/api/stream/:filename', (req, res)=> {
gfs.files.findOne({filename: req.params.filename}, (err, file)=> {
if(err) {
throw err
} else {
const readstream = gfs.createReadStream(file.filename)
readstream.pipe(res)
}
})
})
Video's HTML Page
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">
<link rel="stylesheet" href="styles.css">
<title><%= title %></title>
</head>
<body>
<header>
<div class="clipit-title">
<h3>Clipit</h3>
</div>
<div class="clipit-routes">
<ul class="nav-links">
<li><a href="/" class="nav-link">Upload</a></li>
<% if(curSession.loggedIn == true) { %>
<li><a href="#" class="nav-link"><%= curSession.username %></a></li>
<li><a href="/api/logout" class="nav-link">Log out</a></li>
<% } else { %>
<li><a href="/login" class="nav-link">Log in</a></li>
<li><a href="/signup" class="nav-link">Sign up</a></li>
<% } %>
</ul>
</div>
</header>
<main>
<div>
<video id="videoPlayer" width="650" controls autoplay>
<source src="/api/stream/<%= file.filename %>" type="<%= file.contentType %>"/>
</video>
</div>
</main>
</body>
</html>
GridFS Storage and Upload
//Sets up the GridFS stream
conn.once('open', ()=> {
gfs = Grid(conn.db, mongoose.mongo)
gfs.collection(settings.BUCKET)
})
//Creates the storage for the videos (used for storing video data when uploading a video)
const videoStorage = new GridFsStorage({
url: settings.DBURI,
file: (req, file)=> {
return new Promise((resolve, reject)=> {
crypto.randomBytes(16, (err, buf)=> {
if(err) {
return reject(err)
}
const fileName = buf.toString('hex') + path.extname(file.originalname)
const fileInfo = {
filename: fileName,
bucketName: settings.BUCKET
}
resolve(fileInfo)
})
})
}
})
//Creates the upload for the video storage
const videoUpload = multer({storage: videoStorage})
Thanks for giving me your time, I hope one of you could help me. Have a good day!
Upvotes: 0
Views: 112