import { app } from '@/app.ts'; import path from 'node:path' import glob from 'fast-glob'; import fs from 'node:fs' import { codeRoot } from '@/modules/config.ts'; const list = async () => { const files = await glob('**/*.ts', { cwd: codeRoot }); type FileContent = { path: string; content: string; } const filesContent: FileContent[] = []; for (const file of files) { if (file.startsWith('node_modules') || file.startsWith('dist') || file.startsWith('.git')) continue; const fullPath = path.join(codeRoot, file); const content = fs.readFileSync(fullPath, 'utf-8'); if (content) { filesContent.push({ path: file, content: content }); } } return filesContent; } app.route({ path: 'file-code' }).define(async (ctx) => { const files = await list(); ctx.body = files }).addTo(app); type UploadProps = { user: string; key: string; files: { type: 'file' | 'base64'; filepath: string; content: string; }[]; } app.route({ path: 'file-code', key: 'upload', middleware: ['auth'] }).define(async (ctx) => { const upload = ctx.query?.upload as UploadProps; if (!upload || !upload.user || !upload.key || !upload.files) { ctx.throw(400, 'Invalid upload data'); } const user = upload.user; const key = upload.key; for (const file of upload.files) { if (file.type === 'file') { const fullPath = path.join(codeRoot, user, key, file.filepath); const dir = path.dirname(fullPath); fs.mkdirSync(dir, { recursive: true }); fs.writeFileSync(fullPath, file.content, 'utf-8'); } else if (file.type === 'base64') { const fullPath = path.join(codeRoot, user, key, file.filepath); const dir = path.dirname(fullPath); fs.mkdirSync(dir, { recursive: true }); const buffer = Buffer.from(file.content, 'base64'); fs.writeFileSync(fullPath, buffer); } } ctx.body = { success: true }; }).addTo(app)