fix(WhatsappDirectLine): refactor large file upload process to improve error handling and add public URL registration
Some checks are pending
GBCI / build (push) Waiting to run

This commit is contained in:
Rodrigo Rodriguez (Pragmatismo) 2025-06-11 09:19:16 -03:00
parent 25e688aed9
commit 51831d2f24

View file

@ -1345,92 +1345,72 @@ 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
let uploadSessionId;
const fileSize = (await fs.stat(filePath)).size;
const fileName = filePath.split('/').pop();
const fileType = mime.lookup(filePath);
const appId = this.whatsappFBAppId;
const userAccessToken = this.whatsappServiceKey;
let h;
try { try {
if (!fileType) { // 1. Save file locally (cache)
throw new Error('Unsupported file type'); const gbaiName = GBUtil.getGBAIPath(min.botId);
} const localName = path.join(
'work',
gbaiName,
'cache',
`tmp${GBAdminService.getRndReadableIdentifier()}${path.extname(filePath)}`
);
await fs.copyFile(filePath, localName);
// Step 1: Start an upload session // 2. Generate a public URL to the cached file
const startResponse = await fetch( const publicUrl = urlJoin(
`https://graph.facebook.com/v20.0/${appId}/uploads?file_name=${fileName}&file_length=${fileSize}&file_type=${fileType}&access_token=${userAccessToken}`, GBServer.globals.publicAddress,
{ min.botId,
method: 'POST' 'cache',
} path.basename(localName)
); );
const startData = await startResponse.json(); // 3. Register the public URL with Meta's API to get media_id
if (!startResponse.ok) { const mediaId = await this.registerWithMeta(publicUrl, path.extname(filePath));
throw new Error(startData.error.message);
return mediaId; // Return Meta's media_id (as original function expected)
} catch (error) {
console.error('Error in uploadLargeFile:', error);
throw error;
} }
uploadSessionId = startData.id.split(':')[1]; }
// Step 2: Upload the file in chunks // Helper: Register a public URL with Meta's /media endpoint
let startOffset = 0; private async registerWithMeta(fileUrl, fileExtension) {
const fileType = this.getWhatsAppFileType(fileExtension); // e.g., 'image', 'document'
while (startOffset < fileSize) { const response = await fetch(
const endOffset = Math.min(startOffset + CHUNK_SIZE, fileSize); `https://graph.facebook.com/v20.0/${this.whatsappFBAppId}/media`,
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}`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Authorization': `OAuth ${userAccessToken}`, 'Authorization': `Bearer ${this.whatsappServiceKey}`,
'file_offset': startOffset.toString(), 'Content-Type': 'application/json'
'Content-Type': 'application/octet-stream',
'Content-Length': bytesRead.toString()
}, },
body: chunk body: JSON.stringify({
}); url: fileUrl,
type: fileType,
messaging_product: 'whatsapp'
})
}
);
const data = await response.json();
if (!response.ok) {
throw new Error(`Failed to register media: ${data.error?.message}`);
}
return response['data'].id; // Meta's media_id
}
const uploadData = await uploadResponse.json(); // Helper: Map file extension to WhatsApp file type
if (!h) { private getWhatsAppFileType(ext) {
h = uploadData.h; const types = {
} '.jpg': 'image',
if (!uploadResponse.ok) { '.png': 'image',
throw new Error(`Upload failed: ${uploadData.error?.message || 'Unknown error'}`); '.pdf': 'document',
} '.mp4': 'video',
// Add others as needed
startOffset = endOffset; };
} return types[ext.toLowerCase()] || 'document';
// Step 3: Get the file handle
const finalizeResponse = await fetch(`https://graph.facebook.com/v20.0/upload:${uploadSessionId}`, {
method: 'GET',
headers: {
'Authorization': `OAuth ${userAccessToken}`
}
});
const finalizeData = await finalizeResponse.json();
if (!finalizeResponse.ok) {
throw new Error(`Finalize failed: ${finalizeData.error?.message || 'Unknown error'}`);
}
console.log('Upload completed successfully with file handle:', finalizeData.h);
return finalizeData.h; // Return the final handle from the response
} catch (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) {