(accessToken: string, filePath: string)
| 70 | } |
| 71 | |
| 72 | async function downloadFileContent(accessToken: string, filePath: string): Promise<string> { |
| 73 | const response = await fetchWithRetry('https://content.dropboxapi.com/2/files/download', { |
| 74 | method: 'POST', |
| 75 | headers: { |
| 76 | Authorization: `Bearer ${accessToken}`, |
| 77 | 'Dropbox-API-Arg': JSON.stringify({ path: filePath }), |
| 78 | }, |
| 79 | }) |
| 80 | |
| 81 | if (!response.ok) { |
| 82 | throw new Error(`Failed to download file ${filePath}: ${response.status}`) |
| 83 | } |
| 84 | |
| 85 | // Stream with a hard byte cap so a file whose listing metadata under-reported |
| 86 | // (or omitted) its size can never be fully buffered into memory. Oversize raises |
| 87 | // so getDocument can surface it as a skipped (failed) row rather than dropping it. |
| 88 | const buffer = await readBodyWithLimit(response, MAX_FILE_SIZE) |
| 89 | if (!buffer) { |
| 90 | throw new ConnectorFileTooLargeError(MAX_FILE_SIZE) |
| 91 | } |
| 92 | |
| 93 | const text = buffer.toString('utf8') |
| 94 | |
| 95 | if (filePath.endsWith('.html') || filePath.endsWith('.htm')) { |
| 96 | return htmlToPlainText(text) |
| 97 | } |
| 98 | |
| 99 | return text |
| 100 | } |
| 101 | |
| 102 | function fileToStub(entry: DropboxFileEntry): ExternalDocument { |
| 103 | return { |
no test coverage detected