MCPcopy Create free account
hub / github.com/Noumena-Network/code / downloadFile

Function downloadFile

src/services/api/filesApi.ts:133–181  ·  view source on GitHub ↗
(
  fileId: string,
  config: FilesApiConfig,
)

Source from the content-addressed store, hash-verified

131 * @returns The file content as a Buffer
132 */
133export async function downloadFile(
134 fileId: string,
135 config: FilesApiConfig,
136): Promise<Buffer> {
137 const baseUrl = config.baseUrl || getDefaultApiBaseUrl()
138 const url = `${baseUrl}/v1/files/${fileId}/content`
139
140 const headers = {
141 Authorization: `Bearer ${config.oauthToken}`,
142 'anthropic-version': ANTHROPIC_VERSION,
143 'anthropic-beta': FILES_API_BETA_HEADER,
144 }
145
146 logDebug(`Downloading file ${fileId} from ${url}`)
147
148 return retryWithBackoff(`Download file ${fileId}`, async () => {
149 try {
150 const response = await axios.get(url, {
151 headers,
152 responseType: 'arraybuffer',
153 timeout: 60000, // 60 second timeout for large files
154 validateStatus: status => status < 500,
155 })
156
157 if (response.status === 200) {
158 logDebug(`Downloaded file ${fileId} (${response.data.length} bytes)`)
159 return { done: true, value: Buffer.from(response.data) }
160 }
161
162 // Non-retriable errors - throw immediately
163 if (response.status === 404) {
164 throw new Error(`File not found: ${fileId}`)
165 }
166 if (response.status === 401) {
167 throw new Error('Authentication failed: invalid or missing API key')
168 }
169 if (response.status === 403) {
170 throw new Error(`Access denied to file: ${fileId}`)
171 }
172
173 return { done: false, error: `status ${response.status}` }
174 } catch (error) {
175 if (!axios.isAxiosError(error)) {
176 throw error
177 }
178 return { done: false, error: error.message }
179 }
180 })
181}
182
183/**
184 * Normalizes a relative path, strips redundant prefixes, and builds the full

Callers 1

downloadAndSaveFileFunction · 0.85

Calls 4

getDefaultApiBaseUrlFunction · 0.85
retryWithBackoffFunction · 0.85
logDebugFunction · 0.70
getMethod · 0.45

Tested by

no test coverage detected