1const SINGLE_STORAGE_UPLOAD_MAX_BYTES = 30 * 1024 * 1024; 2const STORAGE_MCP_CHUNK_BYTES = 768 * 1024; 3 4function decodedBase64Size(base64) { 5 const clean = String(base64 || '').replace(/\s+/g, ''); 6 if (!clean) return 0; 7 const padding = clean.endsWith('==') ? 2 : clean.endsWith('=') ? 1 : 0; 8 return Math.floor(clean.length * 3 / 4) - padding; 9} 10 11function storedFileId(stored) { 12 return String(stored?.id ?? stored?.file?.id ?? ''); 13} 14 15async function downloadDriveFile(context, fileId) { 16 const r = await context.integration('google-drive', 'download_file', { fileId, alt: 'media' }); 17 if (r?._binary && r.base64) return r; 18 if (r?.base64) { 19 return { 20 _binary: true, 21 base64: r.base64, 22 mimeType: r.mimeType || r.mime_type || 'application/octet-stream', 23 size: r.size, 24 }; 25 } 26 throw new Error('google-drive.download_file returned no binary payload'); 27} 28 29async function uploadAudioToStorage(context, appel, file, dlRes) { 30 const name = file.name; 31 const contentType = dlRes.mimeType || file.mimeType || 'application/octet-stream'; 32 const folder = `flexylead/audios/${appel.centre_appel_id}`; 33 const tags = [`centre:${appel.centre_appel_id}`, `appel:${appel.local_id}`, `drive:${file.drive_file_id}`]; 34 const source = `google-drive:${file.drive_file_id}`; 35 const sizeBytes = Number(dlRes.size || file.size || decodedBase64Size(dlRes.base64) || 0); 36 37 if (sizeBytes > 0 && sizeBytes <= SINGLE_STORAGE_UPLOAD_MAX_BYTES) { 38 return await context.call('storage', 'files_upload', { 39 name, 40 content_base64: dlRes.base64, 41 content_type: contentType, 42 folder, 43 tags, 44 source, 45 }); 46 } 47 48 const bytes = Buffer.from(dlRes.base64, 'base64'); 49 const declaredSize = sizeBytes || bytes.length; 50 const init = await context.call('storage', 'storage_upload_init', { 51 name, 52 size_bytes: declaredSize, 53 content_type: contentType, 54 folder, 55 tags, 56 source, 57 }); 58 if (init?.was_existing && init?.file) return init; 59 60 const uploadId = init?.upload_id;