You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The client.downloadMedia command works fine and downloads files with a bandwidth of about 50 Mbps.
My problem is with files larger than 2 GB, which after downloading give this error:
Error: The value of "length" is out of range.It must be >= 0 && <= 2147483647. Received 2197627698
and creates a file with zero size.
This is my code:
try{constfilepath='file';constfileInfo=filesToDownload[0];letstartTime=Date.now();letlastProgress=0;// Download file with progress trackingconstbuffer=awaitclient.downloadMedia(fileInfo.media,{progressCallback: (receivedBytes,totalBytes)=>{if(totalBytes&&totalBytes>0){constpercent=Math.round((receivedBytes/totalBytes)*100);constelapsed=(Date.now()-startTime)/1000;constspeed=elapsed>0 ? (receivedBytes/elapsed) : 0;constspeedStr=formatSpeed(speed);// Fixed width formatting to prevent jumpingconstpercentStr=percent.toString().padStart(3,' ');constreceivedStr=formatBytes(receivedBytes).padStart(10,' ');consttotalStr=formatBytes(totalBytes).padStart(10,' ');constspeedFormatted=speedStr.padStart(12,' ');// Update progress in same line with fixed widthconstprogressLine=` Progress: ${percentStr}% | ${receivedStr} / ${totalStr} | ${speedFormatted}`;process.stdout.write(`\r${progressLine.padEnd(80,' ')}`);lastProgress=percent;}},workers: 8,// Increased workers for better speedchunkSize: 1048576,// 1MB chunks for better speedrequestDelay: 0// No delay for better speed});// Write buffer to filefs.writeFileSync(filepath,buffer);}catch(error){console.error(`Error: ${error.message}`);}// Helper functions for formattingfunctionformatBytes(bytes){if(!bytes||bytes===0)return'0.00 B';constk=1024;constsizes=['B','KB','MB','GB'];consti=Math.floor(Math.log(bytes)/Math.log(k));return(bytes/Math.pow(k,i)).toFixed(2)+' '+sizes[i];}functionformatSpeed(bytesPerSecond){if(!bytesPerSecond||bytesPerSecond===0)return'0.00 bps';constbitsPerSecond=bytesPerSecond*8;// Convert bytes to bitsconstk=1000;// Use 1000 for network speeds (not 1024)constsizes=['bps','Kbps','Mbps','Gbps'];consti=Math.floor(Math.log(bitsPerSecond)/Math.log(k));return(bitsPerSecond/Math.pow(k,i)).toFixed(2)+' '+sizes[i];}
(Actually the complexity of the code is to implement the progress bar and it is actually a simple client.downloadMedia code.)
I asked AI about this problem and he said that this is due to nodejs limitations and suggested a solution to write a stream and changed my code to the following:
if(fileInfo.fileSize&&fileInfo.fileSize>2000000000){// Real-time progress tracking for large files onlyconstprogressInterval=setInterval(()=>{if(fs.existsSync(filepath)){conststats=fs.statSync(filepath);constcurrentSize=stats.size;if(currentSize>0){constelapsed=(Date.now()-startTime)/1000;constpercent=Math.round((currentSize/fileInfo.fileSize)*100);constspeed=elapsed>0 ? (currentSize/elapsed) : 0;constspeedStr=formatSpeed(speed);constpercentStr=percent.toString().padStart(3,' ');constreceivedStr=formatBytes(currentSize).padStart(10,' ');consttotalStr=formatBytes(fileInfo.fileSize).padStart(10,' ');constspeedFormatted=speedStr.padStart(12,' ');constprogressLine=` Progress: ${percentStr}% | ${receivedStr} / ${totalStr} | ${speedFormatted}`;process.stdout.write(`\r${progressLine.padEnd(80,' ')}`);}}else{// Show waiting message for large filesconstelapsed=(Date.now()-startTime)/1000;if(elapsed>5){process.stdout.write(`\r⏳ Preparing large file download... (${elapsed.toFixed(0)}s)`.padEnd(80,' '));}}},100);// Update every 0.1 seconds for ultra-smooth progress// For large files, use downloadFile with streamingawaitclient.downloadFile(fileInfo.media,{outputFile: filepath,workers: 8,chunkSize: 1048576,requestDelay: 0});clearInterval(progressInterval);}
This code also works for files larger than 2 GB.
But the problem here is that my download speed is around 15 Mbps, which is much lower than the previous method.
What is a better solution that is as integrated as possible and of course gives me the highest possible speed?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
The
client.downloadMedia
command works fine and downloads files with a bandwidth of about 50 Mbps.My problem is with files larger than 2 GB, which after downloading give this error:
and creates a file with zero size.
This is my code:
(Actually the complexity of the code is to implement the progress bar and it is actually a simple client.downloadMedia code.)
I asked AI about this problem and he said that this is due to nodejs limitations and suggested a solution to write a stream and changed my code to the following:
This code also works for files larger than 2 GB.
But the problem here is that my download speed is around 15 Mbps, which is much lower than the previous method.
What is a better solution that is as integrated as possible and of course gives me the highest possible speed?
Beta Was this translation helpful? Give feedback.
All reactions