pedroslopez / whatsapp-web.js

A WhatsApp client library for NodeJS that connects through the WhatsApp Web browser app
https://wwebjs.dev
Apache License 2.0
14.96k stars 3.55k forks source link

BOT Freeze Before Downloading Video Above 10MB to File System #651

Closed jodhykardo closed 3 years ago

jodhykardo commented 3 years ago

Hi,

Right now i'm developing chat system through REST API. For the media in chat, BOT must download the file and then serves it as link to PHP server. The chat system work perfectly fine until someone sent a video with size 17MB. The BOT don't downloading the video and it just freeze without any error log. The BOT don't even reply any messages automatically after freeze. But, if video size is below 10MB, it can download the video and work perfectly fine.

Here is my code:

`const chatId = msg.id.id; const chatAck = msg.ack; const chatMessage = msg.body; const chatTime = msg.timestamp; const chatFrom = msg.from; const chatTo = msg.to; const chatAuthor = msg.author; const chatType = msg.type; const chatMediaKey = msg.mediaKey; const chatName = chat.name; const username = chatName.replace(" ", "-"); console.log("Before IF");

if(msg.hasMedia){
    console.log("After IF");
    const downloadMedia = await msg.downloadMedia();
    console.log(downloadMedia.mimetype);
    let subExt = downloadMedia.mimetype
    const ext = subExt.substring(subExt.lastIndexOf('/') + 1);
    const buffer = Buffer.from(downloadMedia.data, "base64");
    const file = username + "-" + Date.now() + "." + ext;
    console.log(downloadMedia);
    fs.writeFile("./media/" + file, buffer, function (err) {
        if (err) {
            console.error(err);
        }else{
            //console.log(downloadMedia.mimetype);
            sendchat(5, chatId, chatAck, chatMessage, chatTime, chatFrom, chatTo, chatAuthor,chatType, chatMediaKey, chatName, file);
        }
    });
}

sendchat(5, chatId, chatAck, chatMessage, chatTime, chatFrom, chatTo, chatAuthor,chatType, chatMediaKey, chatName);`

WhatsApp

Library

Additional context

Add any other context about the problem here.

fdciabdul commented 3 years ago

Use wget or aria2c instead of Buffer which has more resources for your device

for example you can use this code


function os_func() {
    this.execCommand = function (cmd) {
        return new Promise((resolve, reject)=> {
           exec(cmd, (error, stdout, stderr) => {
             if (error) {
                reject(error);
                return;
            }
            resolve(stdout)
           });
       })
   }
}
var os = new os_func();

os.execCommand('wget "https://site.com/vid.mp4"   -o videofolder/result.mp4').then(res=> {
    var media = MessageMedia.fromFilePath('videofolder/result.mp4');
chat.sendMessage(media);
}).catch(err=> {
    console.log("os >>>", err);
})
jodhykardo commented 3 years ago

Use wget or aria2c instead of Buffer which has more resources for your device

for example you can use this code

function os_func() {
    this.execCommand = function (cmd) {
        return new Promise((resolve, reject)=> {
           exec(cmd, (error, stdout, stderr) => {
             if (error) {
                reject(error);
                return;
            }
            resolve(stdout)
           });
       })
   }
}
var os = new os_func();

os.execCommand('wget "https://site.com/vid.mp4"   -o videofolder/result.mp4').then(res=> {
    var media = MessageMedia.fromFilePath('videofolder/result.mp4');
chat.sendMessage(media);
}).catch(err=> {
    console.log("os >>>", err);
})

The condition is the BOT receiving media message, the data is in base64, so i need to buffer it to save the media in the BOT server.

sostenesapollo commented 3 years ago

That's also happenning with me

sostenesapollo commented 3 years ago

but in upload file... if you try to send this file works ?

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.