NodeJS get audio file

Viewed 256

I have stored some audio files in GridFS, and I am able to do a find() query to fetch them.

But how do I get them so I can stream the audio from these files?

import clientPromise from "../../lib/mongodb";
import { MongoClient, GridFSBucket } from 'mongodb'
var CryptoJS = require("crypto-js");

//import { hash } from 'bcryptjs';
export default async function audio(req, res) {
  
        const uri = process.env.MONGODB_URI
        let client
        let clientPromise
        const options = {}
        client = new MongoClient(uri, options)
        clientPromise = client.connect()


        const clients = await clientPromise


        const database = clients.db('AUDIODB');
        const bucket = new GridFSBucket(database,{bucketName:"uploads"});

        const { id } = req.query;
        const cursor = bucket.find({});

        cursor.forEach(doc => console.log(doc)); //THIS SHOWS ME THE FILES UPLOADED

       
}


export async function getServerSideProps(context) {
    try {
        // client.db() will be the default database passed in the MONGODB_URI
        // You can change the database by calling the client.db() function and specifying a database like:
        // const db = client.db("myDatabase");
        // Then you can execute queries against your database like so:
        // db.find({}) or any of the MongoDB Node Driver commands
        await clientPromise
        return {
          props: { isConnected: true },
        }
      } catch (e) {
        console.error(e)
        return {
          props: { isConnected: false },
        }
      }
}

as you can see from above, I have it showing all the files that I have uploaded

        cursor.forEach(doc => console.log(doc)); //THIS SHOWS ME THE FILES UPLOADED

However this just gets me the following

{
  _id: new ObjectId("6267ba6dd2512c3257f30980"),
  filename: '/Volumes/Studio/AUDIOFILES/2022/AUDIOPATH/AUDIO.mp3',
  length: 168906,
  chunkSize: 261120,
  uploadDate: 2022-04-26T09:25:05.589Z
}

But clearly I can't put

<audio src="FILENAME"/> 

as my server does not host the uploaded file.

0 Answers
Related