router_function_getMedia.js

const axios = require('axios');
const konfig = require('../../config.js');
const { KeiLog } = require('../../lib/Logger');

/**
 * Module for retrieving media content from a Facebook Graph API endpoint.
 *@module getmedia
 * @param {object} app - The Express application object.
 */
module.exports = async function (app) {
  app.get("/getmedia", async (req, res) => {
    try {
      const { id } = req.query;

      const result = await axios({
        method: 'get',
        url: `https://graph.facebook.com/v16.0/${id}`,
        headers: {
          'Authorization': `Bearer ${konfig.token}`
        }
      });

      const imageType = result.data.mime_type === 'image/jpeg' ? 'image/jpeg' : 'application/octet-stream';

      const results = await axios({
        method: 'get',
        url: `${result.data.url}`,
        responseType: 'arraybuffer',
        headers: {
          'Authorization': `Bearer ${konfig.token}`
        }
      });

      const base64Image = Buffer.from(results.data, 'binary').toString('base64');

      if (imageType === 'image/jpeg') {
        const img = Buffer.from(base64Image, 'base64');
        res.writeHead(200, {
          'Content-Type': 'image/png',
          'Content-Length': img.length
        });
        res.end(img);
      } else {
        res.json({ base64: base64Image })
      }

    } catch (error) {
     // console.log(error);
      res.status(500).json(error);
    }
  });
};