* Fetch + write one attachment. Returns the absolute path on success, * undefined on any failure.
(att: InboundAttachment)
| 66 | * undefined on any failure. |
| 67 | */ |
| 68 | async function resolveOne(att: InboundAttachment): Promise<string | undefined> { |
| 69 | const token = getBridgeAccessToken() |
| 70 | if (!token) { |
| 71 | debug('skip: no oauth token') |
| 72 | return undefined |
| 73 | } |
| 74 | |
| 75 | let data: Buffer |
| 76 | try { |
| 77 | // getOauthConfig() (via getBridgeBaseUrl) throws on a non-allowlisted |
| 78 | // CLAUDE_CODE_CUSTOM_OAUTH_URL — keep it inside the try so a bad |
| 79 | // FedStart URL degrades to "no @path" instead of crashing print.ts's |
| 80 | // reader loop (which has no catch around the await). |
| 81 | const url = `${getBridgeBaseUrl()}/api/oauth/files/${encodeURIComponent(att.file_uuid)}/content` |
| 82 | const response = await axios.get(url, { |
| 83 | headers: { Authorization: `Bearer ${token}` }, |
| 84 | responseType: 'arraybuffer', |
| 85 | timeout: DOWNLOAD_TIMEOUT_MS, |
| 86 | validateStatus: () => true, |
| 87 | }) |
| 88 | if (response.status !== 200) { |
| 89 | debug(`fetch ${att.file_uuid} failed: status=${response.status}`) |
| 90 | return undefined |
| 91 | } |
| 92 | data = Buffer.from(response.data) |
| 93 | } catch (e) { |
| 94 | debug(`fetch ${att.file_uuid} threw: ${e}`) |
| 95 | return undefined |
| 96 | } |
| 97 | |
| 98 | // uuid-prefix makes collisions impossible across messages and within one |
| 99 | // (same filename, different files). 8 chars is enough — this isn't security. |
| 100 | const safeName = sanitizeFileName(att.file_name) |
| 101 | const prefix = ( |
| 102 | att.file_uuid.slice(0, 8) || randomUUID().slice(0, 8) |
| 103 | ).replace(/[^a-zA-Z0-9_-]/g, '_') |
| 104 | const dir = uploadsDir() |
| 105 | const outPath = join(dir, `${prefix}-${safeName}`) |
| 106 | |
| 107 | try { |
| 108 | await mkdir(dir, { recursive: true }) |
| 109 | await writeFile(outPath, data) |
| 110 | } catch (e) { |
| 111 | debug(`write ${outPath} failed: ${e}`) |
| 112 | return undefined |
| 113 | } |
| 114 | |
| 115 | debug(`resolved ${att.file_uuid} → ${outPath} (${data.length} bytes)`) |
| 116 | return outPath |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Resolve all attachments on an inbound message to a prefix string of |
nothing calls this directly
no test coverage detected