Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add support for sending large files (up to 2GB) #2368

Open
wants to merge 63 commits into
base: main
Choose a base branch
from

Conversation

opssemnik
Copy link

@opssemnik opssemnik commented Jul 28, 2023

PR Details

Use an additional evaluate function to setup a browser variable,
Break the file down into 50MB chunks and append to the browser with a loop of puppeteer evaluates

During sendMessage event, inside the browser, override the attachment's data with the variable previously created

work is based out of:
main...matricce:whatsapp-web.js:fix/sendBigFiles

Updated patch is from @shirser121:
https://github.com/pedroslopez/whatsapp-web.js/pull/2368/files/c452fe86bb0c5ff3c16863ae77e9e76ac8b66010

Description

Changes sendMessage in Client.js
it does not affect messages without media, or messages whose media is below 50 Megabytes

Create client objects (chunks) of 50MB parts of the original file contents.
Create metadata chunk to save the amount of data chunks

Afterwards, initiate sendMessage on the client, using a dummy MessageMedia object, which is overwritten inside the browser with the chunks sent previously

Related Issues

Fixes #2117
Fixes #1771
Fixes #1951
Fixes #1925

Motivation and Context

Currently the lib only allows <80MB files, which was fine, however, whatsapp web added support for 2GB files (2097152000 bytes)

How Has This Been Tested

npm install git+https://github.com/opssemnik/whatsapp-web.js#patch-1
Sending / Replying messages without attachments has been tested
Sending / replying messages with media (sticker, non sticker, voice and documents) < 50MB
Sending / replying messages with media (sticker, non sticker, voice and documents) > 50MB < 1GB

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist

  • My code follows the code style of this project.
  • I have updated the documentation accordingly (index.d.ts).

Break the file down into 50MB chunks and send via batches, creating a string object for each chunk

Afterwards decode each chunk into one bing string

Todo:
I am overriding the entire attachment into a new MessageMedia object. Overriding just the data field was not yielding results (it stayed the original big string)

work is based out of:
pedroslopez/whatsapp-web.js@main...matricce:whatsapp-web.js:fix/sendBigFiles
Correctly assign mimetype and filesize values
@Santosl2
Copy link

Santosl2 commented Jul 29, 2023

In JS we don't use var anymore (only for very specific cases). Instead of var, try use const for unmodified variables or let for variables that will have modifications.

@opssemnik
Copy link
Author

In JS we don't use var anymore (only for very specific cases). Instead of var, try use const for unmodified variables or let for variables that will have modifications.

I've commited the changes, thanks!

Copy link
Collaborator

@shirser121 shirser121 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to improve the code a little

src/Client.js Show resolved Hide resolved
src/Client.js Show resolved Hide resolved
Change from multiple variables of 50MBs each into one variable that gets appended by 50MB chunks

See:
https://github.com/pedroslopez/whatsapp-web.js/pull/2368/files/c452fe86bb0c5ff3c16863ae77e9e76ac8b66010
@shirser121
Copy link
Collaborator

Please fix ESLint

@opssemnik
Copy link
Author

Please fix ESLint

Fix commited!

src/Client.js Outdated Show resolved Hide resolved
src/Client.js Outdated Show resolved Hide resolved
@shirser121
Copy link
Collaborator

Please fix ESLint

@opssemnik
Copy link
Author

Please fix ESLint

Fix commited (2)

@opssemnik opssemnik changed the title Add support for sending large files (up to 1GB) fix: Add support for sending large files (up to 1GB) Aug 8, 2023
Copy link
Collaborator

@tuyuribr tuyuribr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@amiruldev20
Copy link

does it work?

@opssemnik
Copy link
Author

opssemnik commented Aug 15, 2023

I don't know how to properly make staggered PRs (a PR that depends on another), but i added a small commit to fix audio waveform on downloaded voice files, this is unrelated to the main PR per say but its a very small change enabled by the new filesize field and getUploadLimits (added by this PR)

as long as the media file is audio, opus codec, and its size is less than getUploadLimits (audio), waweb will send it as a voice memo, even though mediaData.type is not ptt

goes from this:
image

to this:
image

This is useful so developers dont need to add media checks (and different message send/reply codes for each type of media)
I.e. if you downloaded the voice memo from a received message

tagging @alechkos as he is the creator of the waveform feature

@alechkos
Copy link
Collaborator

@opssemnik

tagging @alechkos as he is the creator of the waveform feature

it's not me, i just brought it from another lib

@opssemnik opssemnik changed the title fix: Add support for sending large files (up to 1GB) fix: Add support for sending large files (up to 2GB) Aug 15, 2023
@aminobot22
Copy link

aminobot22 commented Aug 28, 2023

npm install git+https://github.com/opssemnik/whatsapp-web.js#patch-1
@opssemnik

i used this one but its not working for me

not showing any error

@PurpShell
Copy link
Sponsor Collaborator

@opssemnik

tagging @alechkos as he is the creator of the waveform feature

it's not me, i just brought it from another lib

I brought waveforms to Baileys too from wppconnect, it is a very clean implementation, what is there to solve? I don't get the issue.

@shirser121
Copy link
Collaborator

Error: Cannot create a string longer than 0x1fffffe8 characters
We can't read files bigger than 512 MB, so this is pretty useless.
We should change the MessageMedia options, to allow the creation of a pipeline stream instead of creating a base64 string, so we can read the file in chunks.

@PurpShell @alechkos can you help here, please?

@opssemnik
Copy link
Author

opssemnik commented Sep 27, 2023

Error: Cannot create a string longer than 0x1fffffe8 characters We can't read files bigger than 512 MB, so this is pretty useless. We should change the MessageMedia options, to allow the creation of a pipeline stream instead of creating a base64 string, so we can read the file in chunks.

@PurpShell @alechkos can you help here, please?

Currently the library breaks at +-50mb because of puppeteer's evaluate function, we hit node's string limit at 10x the current limit

Locally, for files larger than that, i changed MessageMedia's data to be an array (and the underlying code to support that), however, since node's string limitation has changed in newer versions, i didnt include it in here

@shirser121
Copy link
Collaborator

I tried to send media about 380MB and its not work.
Sometimes crash and sometimes just not sent.
Its seem like there is Missing parameter.
Also, i found out that after 3-4 tries i got out of memory error

@RC047
Copy link
Contributor

RC047 commented Dec 1, 2023

Maybe we have to look for another way to send large files

@pierrecorsini
Copy link
Contributor

The fact that we are unsure of: what is exactly the limit of size, the conditions of failure, and the failing behaviour; is for me a real issue of stability.

IMHO this would be great to identify a safe limit of size where the upload is stable; and trigger a (bypassable) exception if the file is bigger.

@alechkos alechkos added the waiting for testers Waiting for other people test this PR in other envs label Jan 14, 2024
@tofers
Copy link
Contributor

tofers commented Feb 11, 2024

I tried this code. It won't send.

You need to update the code to the latest version and check it again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting for testers Waiting for other people test this PR in other envs
Projects
None yet