'nodejs - How to check for multiple files in Telegram API

I need to encrypt and decrypt files sent from the user to my telegram bot. I have this code at the moment, I've used it in a project where I was using Vue.js for the front-end. Since I'm converting it to a full node-js scrypt I need a simply way to check if the user send a single file or multiple files then proceed to encrypt. At the moment I've noticed that each file will be send in a single message also if they are grouped. I've tried to use the Array.map() function but it's not working because each file is a single object.

Any help please?

This is how telegram api manage files into message object

{
  message_id: 48,
  from: {
    id: 184262,
    is_bot: false,
    first_name: '',
    username: '',
    language_code: 'it'
  },
  chat: {
    id: 184262,
    first_name: '',
    username: '',
    type: 'private'
  },
  date: 1649587649,
  media_group_id: '13196701196719156',
  document: {
    file_name: 'AvvisoDiPagamento001700017484336.pdf',
    mime_type: 'application/pdf',
    thumb: {
      file_id: 'AAMCBAADGQEAAzBiUrXBVDTGvxTdt9Ykkf92byUJKgACmwwAAvDomFLPQ5F8ZA3ZOgEAB20AAyME',
      file_unique_id: 'AQADmwwAAvDomFJy',
      file_size: 11635,
      width: 226,
      height: 320
    },
    file_id: 'BQACAgQAAxkBAAMwYlK1wVQ8U3bfWJJH_dm8lCSoAApsMAALw6JhSz0ORfGQN2TojBA',
    file_unique_id: 'AgADmwwAAvDomFI',
    file_size: 19674
  }
}
{
  message_id: 49,
  from: {
    id: 184262,
    is_bot: false,
    first_name: '',
    username: '',
    language_code: 'it'
  },
  chat: {
    id: 184262,
    first_name: '',
    username: '',
    type: 'private'
  },
  date: 1649587649,
  media_group_id: '13196701196719156',
  document: {
    file_name: 'test_0.pdf',
    mime_type: 'application/pdf',
    thumb: {
      file_id: 'AAMCBAADGQFiUrXB00MQ0X-9lOdda8htSdj12QACnAwAAvDomFIx8-KFV_OzDAEAB20AAyME'
    ...

This is the code I have at the moment

bot.on('document', async (file) => {
  const stream = bot.getFileStream(file.document.file_id);
  const downloadedFile = stream.pipe(fs.createWriteStream(file.document.file_name));
  // this will not work 
  const data = {
    filePath: `${process.cwd()}/${downloadedFile.path}`,
    password: password,
    fileType: file.document.mime_type,
    fileName: file.document.file_name
  }
  const eData = await encryptData(data)
})

const encryptData = async (data) => {
  let output = [];
  let salt = crypto.randomBytes(32);
  let key = crypto.scryptSync(data.password, salt, 32);
  
   data.files.map( (file) => {
     let buffer = fs.readFileSync(path.resolve(file.path));
     let base64 = dataURI.getBase64DataURI(buffer, file.type);
     let iv =  crypto.randomBytes(16);
     let cipher =  crypto.createCipheriv('aes-256-gcm', key, iv);
     let encryptedData = Buffer.concat([cipher.update(base64, 'utf8'), cipher.final()]);
     let payload = `${iv.toString('hex')}:${encryptedData.toString('hex')}`;
     output.push(payload);
   });

    let result = output.join(' '); 
    return `${salt.toString('hex')}+${result}`;
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source