0

I have a difficulty here showing video using react-native-video. The server is using Express, and using GridFSBucket to retrieve the video from mongodb.

The problem is that: The video from GridFSBucket won't show. But when I tried to place the video in public folder, it will show.

So, my guess is that there is something wrong with how I serve the video. Here is my code from the server:

const bucket = new GridFSBucket(db.original(), { bucketName: "fs" });
const fileDetail = await db.original().collection("fs.files").findOne({_id: idObj});
if (isNullOrUndefined(fileDetail)) {
    throw new NoContentException(`asset not found for id ${id}`);
}

const range = req.headers["range"]
if (range && typeof range === "string") {
    const parts = range.replace(/bytes=/, "").split("-");
    const partialstart = parts[0];
    const partialend = parts[1];

    const start = parseInt(partialstart, 10);
    const end = partialend ? parseInt(partialend, 10) : fileDetail.length - 1;
    const chunksize = (end - start) + 1;

    res.writeHead(206, {
        'Accept-Ranges': 'bytes',
        'Content-Length': chunksize,
        'Content-Range': 'bytes ' + start + '-' + end + '/' + fileDetail.length,
        'Content-Type': fileDetail.contentType
    });

    bucket.openDownloadStream(idObj, {start, end}).pipe(res);
} else {
    res.header('Content-Length', fileDetail.length);
    res.header('Content-Type', fileDetail.contentType);

    bucket.openDownloadStream(idObj).pipe(res);
}

Thanks in advance for your answer ^_^

Rizki Sunaryo
  • 468
  • 1
  • 5
  • 14

1 Answers1

0

I found the solution.

The line:

bucket.openDownloadStream(idObj, {start, end}).pipe(res);

should be:

bucket.openDownloadStream(idObj, {start, end: end - 1}).pipe(res);
Rizki Sunaryo
  • 468
  • 1
  • 5
  • 14