fix(WhatsappDirectLine): improve large file upload handling and error messages
Some checks are pending
GBCI / build (push) Waiting to run

This commit is contained in:
Rodrigo Rodriguez (Pragmatismo) 2025-06-10 20:27:02 -03:00
parent f232bdbaac
commit 25e688aed9

View file

@ -1345,8 +1345,7 @@ private async sendButtonList(to: string, buttons: string[]) {
GBLog.error(`Error on Whatsapp callback: ${GBUtil.toYAML(error)}`); GBLog.error(`Error on Whatsapp callback: ${GBUtil.toYAML(error)}`);
} }
} }
public async uploadLargeFile(min, filePath) {
public async uploadLargeFile(min, filePath) {
const CHUNK_SIZE = 4 * 1024 * 1024; // 4MB chunks const CHUNK_SIZE = 4 * 1024 * 1024; // 4MB chunks
let uploadSessionId; let uploadSessionId;
const fileSize = (await fs.stat(filePath)).size; const fileSize = (await fs.stat(filePath)).size;
@ -1380,17 +1379,26 @@ private async sendButtonList(to: string, buttons: string[]) {
while (startOffset < fileSize) { while (startOffset < fileSize) {
const endOffset = Math.min(startOffset + CHUNK_SIZE, fileSize); const endOffset = Math.min(startOffset + CHUNK_SIZE, fileSize);
const fileStream = createReadStream(filePath, { start: startOffset, end: endOffset - 1 });
const chunkSize = endOffset - startOffset; const chunkSize = endOffset - startOffset;
// Read the chunk into a buffer to get accurate size
const buffer = new Uint8Array(chunkSize);
const fd = await fs.open(filePath, 'r');
const { bytesRead } = await fd.read(buffer, 0, chunkSize, startOffset);
await fd.close();
// Trim buffer to actual bytes read
const chunk = buffer.subarray(0, bytesRead);
const uploadResponse = await fetch(`https://graph.facebook.com/v20.0/upload:${uploadSessionId}`, { const uploadResponse = await fetch(`https://graph.facebook.com/v20.0/upload:${uploadSessionId}`, {
method: 'POST', method: 'POST',
headers: { headers: {
Authorization: `OAuth ${userAccessToken}`, 'Authorization': `OAuth ${userAccessToken}`,
file_offset: startOffset.toString(), 'file_offset': startOffset.toString(),
'Content-Length': chunkSize.toString() 'Content-Type': 'application/octet-stream',
'Content-Length': bytesRead.toString()
}, },
body: fileStream body: chunk
}); });
const uploadData = await uploadResponse.json(); const uploadData = await uploadResponse.json();
@ -1398,7 +1406,7 @@ private async sendButtonList(to: string, buttons: string[]) {
h = uploadData.h; h = uploadData.h;
} }
if (!uploadResponse.ok) { if (!uploadResponse.ok) {
throw new Error(uploadData.error.message); throw new Error(`Upload failed: ${uploadData.error?.message || 'Unknown error'}`);
} }
startOffset = endOffset; startOffset = endOffset;
@ -1408,21 +1416,22 @@ private async sendButtonList(to: string, buttons: string[]) {
const finalizeResponse = await fetch(`https://graph.facebook.com/v20.0/upload:${uploadSessionId}`, { const finalizeResponse = await fetch(`https://graph.facebook.com/v20.0/upload:${uploadSessionId}`, {
method: 'GET', method: 'GET',
headers: { headers: {
Authorization: `OAuth ${userAccessToken}` 'Authorization': `OAuth ${userAccessToken}`
} }
}); });
const finalizeData = await finalizeResponse.json(); const finalizeData = await finalizeResponse.json();
if (!finalizeResponse.ok) { if (!finalizeResponse.ok) {
throw new Error(finalizeData.error.message); throw new Error(`Finalize failed: ${finalizeData.error?.message || 'Unknown error'}`);
} }
console.log('Upload completed successfully with file handle:', finalizeData.h); console.log('Upload completed successfully with file handle:', finalizeData.h);
return h; return finalizeData.h; // Return the final handle from the response
} catch (error) { } catch (error) {
console.error('Error during file upload:', error); console.error('Error during file upload:', error);
throw error; // Re-throw to allow caller to handle
} }
} }
public async downloadImage(mediaId, outputPath) { public async downloadImage(mediaId, outputPath) {
const userAccessToken = this.whatsappServiceKey; const userAccessToken = this.whatsappServiceKey;