(
messageId: string,
attachmentInfo: Array<{ attachmentId: string; filename: string; mimeType: string; size: number }>,
accessToken: string
)
| 221 | |
| 222 | // Helper function to download attachments from Gmail API |
| 223 | export async function downloadAttachments( |
| 224 | messageId: string, |
| 225 | attachmentInfo: Array<{ attachmentId: string; filename: string; mimeType: string; size: number }>, |
| 226 | accessToken: string |
| 227 | ): Promise<GmailAttachment[]> { |
| 228 | const downloadedAttachments: GmailAttachment[] = [] |
| 229 | |
| 230 | for (const attachment of attachmentInfo) { |
| 231 | try { |
| 232 | // Download attachment from Gmail API |
| 233 | const attachmentResponse = await fetch( |
| 234 | `${GMAIL_API_BASE}/messages/${messageId}/attachments/${attachment.attachmentId}`, |
| 235 | { |
| 236 | headers: { |
| 237 | Authorization: `Bearer ${accessToken}`, |
| 238 | 'Content-Type': 'application/json', |
| 239 | }, |
| 240 | } |
| 241 | ) |
| 242 | |
| 243 | if (!attachmentResponse.ok) { |
| 244 | await attachmentResponse.body?.cancel().catch(() => {}) |
| 245 | continue |
| 246 | } |
| 247 | |
| 248 | const attachmentData = (await attachmentResponse.json()) as { data: string; size: number } |
| 249 | |
| 250 | // Decode base64url data to buffer |
| 251 | // Gmail API returns data in base64url format (URL-safe base64) |
| 252 | const base64Data = attachmentData.data.replace(/-/g, '+').replace(/_/g, '/') |
| 253 | const buffer = Buffer.from(base64Data, 'base64') |
| 254 | |
| 255 | downloadedAttachments.push({ |
| 256 | name: attachment.filename, |
| 257 | data: buffer.toString('base64'), |
| 258 | mimeType: attachment.mimeType, |
| 259 | size: attachment.size, |
| 260 | }) |
| 261 | } catch (error) { |
| 262 | // Continue with other attachments |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | return downloadedAttachments |
| 267 | } |
| 268 | |
| 269 | // Helper function to create a summary of multiple messages |
| 270 | export function createMessagesSummary(messages: any[]): string { |
no test coverage detected