Code editor and widget
This commit is contained in:
@@ -79,6 +79,23 @@ router.get('/read', async (ctx) => {
|
||||
return ctx.json({ content, size: s.size });
|
||||
});
|
||||
|
||||
// Write file contents
|
||||
router.post('/write', async (ctx) => {
|
||||
const user = ctx.get('user');
|
||||
const rootDir = getRootDir(user, ctx.req.query('root') ?? undefined);
|
||||
const { path, content } = ctx.get('body') as { path: string; content: string };
|
||||
if (!path) throw errors.BAD_REQUEST('path is required');
|
||||
if (typeof content !== 'string') throw errors.BAD_REQUEST('content must be a string');
|
||||
|
||||
const MAX_SIZE = 5 * 1024 * 1024; // 5 MB
|
||||
if (new TextEncoder().encode(content).length > MAX_SIZE) throw errors.BAD_REQUEST('Content too large (max 5 MB)');
|
||||
|
||||
const absPath = resolveUserPath(rootDir, path);
|
||||
await mkdir(dirname(absPath), { recursive: true });
|
||||
await Bun.write(absPath, content);
|
||||
return ctx.json({ ok: true });
|
||||
});
|
||||
|
||||
// Create directory
|
||||
router.post('/mkdir', async (ctx) => {
|
||||
const user = ctx.get('user');
|
||||
|
||||
Reference in New Issue
Block a user