-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathgetFileContentRoute.ts
More file actions
29 lines (26 loc) · 1.39 KB
/
getFileContentRoute.ts
File metadata and controls
29 lines (26 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import type { AppFastifyInstance } from '#app/applicationTypes';
import { CodeTaskServiceImpl } from '#codeTask/codeTaskServiceImpl';
import { sendNotFound, sendServerError } from '#fastify/responses';
import { logger } from '#o11y/logger';
import { CODE_TASK_API } from '#shared/codeTask/codeTask.api';
import { currentUser } from '#user/userContext';
import { registerApiRoute } from '../routeUtils';
export async function getFileContentRoute(fastify: AppFastifyInstance): Promise<void> {
const codeTaskService = new CodeTaskServiceImpl(fastify.codeTaskRepository);
registerApiRoute(fastify, CODE_TASK_API.getFileContent, async (request, reply) => {
const userId = currentUser().id;
const { codeTaskId } = request.params;
const { path: filePath } = request.query; // path is required by schema
try {
const content = await codeTaskService.getFileContent(userId, codeTaskId, filePath);
return reply.sendJSON({ content });
} catch (error: any) {
logger.error(error, `Error getting file content for codeTask ${codeTaskId} (path: ${filePath}), user ${userId}`);
if (error.message?.includes('not found') || error.name === 'ENOENT' /* For fs errors */) {
return sendNotFound(reply, `File or CodeTask not found: ${filePath}`);
}
// Add more specific error handling if service throws typed errors
return sendServerError(reply, error.message || 'Failed to get file content');
}
});
}