( filename: string, userId: string, raw: boolean, versioned: boolean, signal: AbortSignal | undefined )
| 159 | ) |
| 160 | |
| 161 | async function handleLocalFile( |
| 162 | filename: string, |
| 163 | userId: string, |
| 164 | raw: boolean, |
| 165 | versioned: boolean, |
| 166 | signal: AbortSignal | undefined |
| 167 | ): Promise<NextResponse> { |
| 168 | const ownerKey = `user:${userId}` |
| 169 | try { |
| 170 | const contextParam: StorageContext | undefined = inferContextFromKey(filename) as |
| 171 | | StorageContext |
| 172 | | undefined |
| 173 | |
| 174 | const hasAccess = await verifyFileAccess( |
| 175 | filename, |
| 176 | userId, |
| 177 | undefined, // customConfig |
| 178 | contextParam, // context |
| 179 | true // isLocal |
| 180 | ) |
| 181 | |
| 182 | if (!hasAccess) { |
| 183 | logger.warn('Unauthorized local file access attempt', { userId, filename }) |
| 184 | throw new FileNotFoundError(`File not found: ${filename}`) |
| 185 | } |
| 186 | |
| 187 | const filePath = await findLocalFile(filename) |
| 188 | |
| 189 | if (!filePath) { |
| 190 | throw new FileNotFoundError(`File not found: ${filename}`) |
| 191 | } |
| 192 | |
| 193 | const rawBuffer = await readFile(filePath) |
| 194 | const segment = filename.split('/').pop() || filename |
| 195 | const displayName = stripStorageKeyPrefix(segment) |
| 196 | const workspaceId = getWorkspaceIdForCompile(filename) |
| 197 | const { buffer: fileBuffer, contentType } = await compileDocumentIfNeeded( |
| 198 | rawBuffer, |
| 199 | displayName, |
| 200 | workspaceId, |
| 201 | raw, |
| 202 | ownerKey, |
| 203 | signal |
| 204 | ) |
| 205 | |
| 206 | logger.info('Local file served', { userId, filename, size: fileBuffer.length }) |
| 207 | |
| 208 | return createFileResponse({ |
| 209 | buffer: fileBuffer, |
| 210 | contentType, |
| 211 | filename: displayName, |
| 212 | cacheControl: resolveServeCacheControl(versioned, contextParam), |
| 213 | }) |
| 214 | } catch (error) { |
| 215 | logger.error('Error reading local file:', error) |
| 216 | throw error |
| 217 | } |
| 218 | } |
no test coverage detected