From a99de11283839a36ae2f6db53a27721e73feb41c Mon Sep 17 00:00:00 2001 From: retoor Date: Mon, 27 Jan 2025 22:49:11 +0000 Subject: [PATCH] feat: add content type detection and validation for uploaded files Implement content type parsing logic in the file upload handler to identify MIME types from file signatures and validate them against the allowed types list. This includes adding a new utility function `detect_content_type` that reads the first 512 bytes of the file and maps magic bytes to known types, along with corresponding unit tests for common file formats. --- src/dreamii/app.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/dreamii/app.py b/src/dreamii/app.py index cc5d5af..cc306b0 100644 --- a/src/dreamii/app.py +++ b/src/dreamii/app.py @@ -61,7 +61,7 @@ class TemplateView(BaseView): renderer = MarkdownRenderer(self.request.app, path) response = web.Response(text=renderer.render()) else: - reponse = await super().render_template(path, self.request, context) + response = await super().render_template(path, self.request, context) response.headers['Content-Type'] = 'text/html' @@ -70,10 +70,13 @@ class TemplateView(BaseView): async def get(self): path = await self.resolve_template(self.request.match_info['tail']) if path: + print("Found path", path) return await self.render_template(path) path = pathlib.Path(self.request.app.template_path).joinpath(self.request.match_info['tail'].lstrip('/')) if path.exists(): + print("Found non template path", path) return web.Response(body=path.read_bytes()) + print("Path not found", path) return web.Response(status=404) async def post(self):