Files
bewcloud/routes/api/notes/save.tsx
Bruno Bernardino 6cfb62d1a2 Refactor data handlers + misc fixes
This refactors the data handlers into a more standard/understood model-like architecture, to prepare for a new, more robust config system.

It also fixes a problem with creating new Notes and uploading new Photos via the web interface (related to #58).

Finally, it speeds up docker builds by sending in less files, which aren't necessary or will be built anyway.

This is all in preparation to allow building #13 more robustly.
2025-05-24 08:24:10 +01:00

67 lines
1.8 KiB
TypeScript

import { Handlers } from 'fresh/server.ts';
import { FreshContextState } from '/lib/types.ts';
import { FileModel } from '/lib/models/files.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 });
}
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));
},
};