bewcloud/routes/api/notes/save.tsx
Bruno Bernardino d86e65475c
Improve App Selection
This allows not enabling Dashboard and Files. It also sorts the apps in the menu according to the order in the `config.core.enabledApps` array.

Since this will require a major version upgrade (`v3.0.0`), I also took the opportunity to upgrade PostgreSQL. You can [follow this guide on how to upgrade PostgreSQL on Docker containers](https://news.onbrn.com/step-by-step-guide-upgrading-postgresql-docker-containers/).

Finally, this has some minor security improvements (confirming API endpoints won't work if their app is disabled in the config).

Closes #114
Closes #108
2025-12-01 12:25:21 +00:00

71 lines
1.9 KiB
TypeScript

import { Handlers } from 'fresh/server.ts';
import { FreshContextState } from '/lib/types.ts';
import { FileModel } from '/lib/models/files.ts';
import { AppConfig } from '/lib/config.ts';
interface Data {}
export interface RequestBody {
fileName: string;
currentPath: string;
contents: string;
}
export interface ResponseBody {
success: boolean;
}
export const handler: Handlers<Data, FreshContextState> = {
async POST(request, context) {
if (!context.state.user) {
return new Response('Unauthorized', { status: 401 });
}
if (!(await AppConfig.isAppEnabled('notes'))) {
return new Response('Forbidden', { status: 403 });
}
const requestBody = await request.clone().json() as RequestBody;
if (
!requestBody.currentPath || !requestBody.fileName || !requestBody.currentPath.startsWith('/Notes/') ||
requestBody.currentPath.includes('../') || !requestBody.currentPath.endsWith('/')
) {
return new Response('Bad Request', { status: 400 });
}
if (
!requestBody.currentPath || !requestBody.currentPath.startsWith('/Notes/') ||
requestBody.currentPath.includes('../')
) {
return new Response('Bad Request', { status: 400 });
}
// Don't allow non-markdown files here
if (!requestBody.fileName.endsWith('.md')) {
return new Response('Not Found', { status: 404 });
}
const fileResult = await FileModel.get(
context.state.user.id,
requestBody.currentPath,
decodeURIComponent(requestBody.fileName),
);
if (!fileResult.success) {
return new Response('Not Found', { status: 404 });
}
const updatedFile = await FileModel.update(
context.state.user.id,
requestBody.currentPath,
decodeURIComponent(requestBody.fileName),
requestBody.contents || '',
);
const responseBody: ResponseBody = { success: updatedFile };
return new Response(JSON.stringify(responseBody));
},
};