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

Function resolveOne

src/bridge/inboundAttachments.ts:69–118  ·  view source on GitHub ↗

* Fetch + write one attachment. Returns the absolute path on success, * undefined on any failure.

(att: InboundAttachment)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 8

randomUUIDFunction · 0.90
getBridgeAccessTokenFunction · 0.85
getBridgeBaseUrlFunction · 0.85
sanitizeFileNameFunction · 0.85
uploadsDirFunction · 0.85
mkdirFunction · 0.85
debugFunction · 0.70
getMethod · 0.45

Tested by

no test coverage detected