* Downloads the text content of a drive item.
( accessToken: string, siteId: string, itemId: string, fileName: string )
| 127 | * Downloads the text content of a drive item. |
| 128 | */ |
| 129 | async function downloadFileContent( |
| 130 | accessToken: string, |
| 131 | siteId: string, |
| 132 | itemId: string, |
| 133 | fileName: string |
| 134 | ): Promise<string> { |
| 135 | const url = `${GRAPH_BASE}/sites/${siteId}/drive/items/${itemId}/content` |
| 136 | |
| 137 | const response = await fetchWithRetry(url, { |
| 138 | method: 'GET', |
| 139 | headers: { Authorization: `Bearer ${accessToken}` }, |
| 140 | redirect: 'follow', |
| 141 | }) |
| 142 | |
| 143 | if (!response.ok) { |
| 144 | throw new Error(`Failed to download file "${fileName}" (${itemId}): ${response.status}`) |
| 145 | } |
| 146 | |
| 147 | // Stream with a hard byte cap so a file with missing/under-reported listing |
| 148 | // size metadata is never fully buffered into memory. Oversized files are |
| 149 | // skipped (returned empty) rather than indexed as truncated partial content. |
| 150 | const buffer = await readBodyWithLimit(response, MAX_DOWNLOAD_SIZE) |
| 151 | if (!buffer) { |
| 152 | throw new ConnectorFileTooLargeError(MAX_DOWNLOAD_SIZE) |
| 153 | } |
| 154 | return buffer.toString('utf8') |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Fetches file content, applying HTML-to-text conversion for .html files. |
no test coverage detected