-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathroute.ts
93 lines (84 loc) · 2.35 KB
/
route.ts
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// app/api/file/route.ts
import { NextResponse } from 'next/server';
import { FileReader } from '@/utils/file-reader';
import { promises as fs } from 'fs';
import path from 'path';
import { logger } from '@/app/log/logger';
export async function POST(req: Request) {
logger.info('🚀 [API] Received POST request to update file');
try {
const { filePath, newContent } = await req.json();
if (!filePath || !newContent) {
logger.error('[API] Missing required parameters');
return NextResponse.json(
{ error: "Missing 'filePath' or 'newContent'" },
{ status: 400 }
);
}
const reader = FileReader.getInstance();
reader.updateFile(filePath, newContent);
logger.info('[API] File updated successfully');
return NextResponse.json({
message: 'File updated successfully',
filePath,
});
} catch (error) {
logger.error('[API] Error updating file:', error);
return NextResponse.json(
{ error: 'Failed to update file' },
{ status: 500 }
);
}
}
export async function GET(req: Request) {
try {
const { searchParams } = new URL(req.url);
const filePath = searchParams.get('path');
if (!filePath) {
return NextResponse.json(
{ error: "Missing 'path' parameter" },
{ status: 400 }
);
}
const reader = FileReader.getInstance();
const content = await reader.readFileContent(filePath);
const fileType = getFileType(filePath);
const res = NextResponse.json({ filePath, content, type: fileType });
return res;
} catch (error) {
return NextResponse.json({ error: 'Failed to read file' }, { status: 500 });
}
}
function getFileType(filePath: string): string {
const extension = filePath.split('.').pop()?.toLowerCase() || '';
const typeMap: { [key: string]: string } = {
//TODO: Add more file types
tsx: 'typescriptreact',
txt: 'text',
md: 'markdown',
json: 'json',
js: 'javascript',
ts: 'typescript',
html: 'html',
css: 'css',
scss: 'scss',
xml: 'xml',
csv: 'csv',
yml: 'yaml',
yaml: 'yaml',
jpg: 'image',
jpeg: 'image',
png: 'image',
gif: 'image',
svg: 'vector',
webp: 'image',
mp4: 'video',
mp3: 'audio',
wav: 'audio',
pdf: 'pdf',
zip: 'archive',
tar: 'archive',
gz: 'archive',
};
return typeMap[extension] || 'unknown';
}