Stream the Video Downloaded from MongoDB's GridFS on react-native-video

57 views Asked by At

I've created a video-saving functionality using MongoDB's GridFS Storage, Now I want to retrieve that video file and stream it using react-native-video library, I've coded the Endpoint for retrieving the video file & its also working on Browser & HTML's Video Tag as source but it is not working on react-native-video & Throwing this error,

{"extra": -2147483648, "what": 1}}

Here is my API Endpoint for retrieving the file Stored in GridFS

const getFile = async (req, res) => {
  try {
    const filename = req.params.filename;

    const conn = await mongoose.connection;
    const gfs = new mongodb.GridFSBucket(conn.db, {
      bucketName: 'video',
    });

    const file = await gfs.find({filename}).toArray();

    const videoSize = file[0].length;
    const start = 0;
    const end = videoSize - 1;
    const contentLength = end - start + 1;
    
    res.writeHead(206, {
      'Content-Range': `bytes ${start}-${end}/${videoSize}`,
      'Accept-Ranges': 'bytes',
      'Content-Length': contentLength,
      'Content-Type': file[0].contentType,
      'Content-Disposition': `inline; filename="${filename}"`,
    });

    
    const downloadStream = gfs.openDownloadStreamByName(filename, {
      start,
    });
    downloadStream.pipe(res);

   
  } catch (error) {
    console.log(error);
    return res.status(500).json({
      message: 'Error While Fetching the File',
    });
  }
};

Here I'm Passing that URL as a Source on react-native-video component on React Native Front End

<Video
        ref={ref}
        source={{
          uri: "http://localhost:5000/api/reel/file/2ded8f3e3050bf5f2d58297d79a35611",
        }}
        type="mp4"
        resizeMode="cover"
        onError={err => console.log(err)}
        style={styles.container}
      />

FYI I've also tried with Using Exo Player but got another issues.

Any Suggestions related to this approach would be appreciated,is it good practice to use GridFS for video streaming applications or I should switch CloudFront, Thanks!

0

There are 0 answers