From 1f074f80f530ed89e41e4d5c8948f118a4eb4720 Mon Sep 17 00:00:00 2001 From: gnat Date: Sun, 29 Sep 2024 09:14:16 -0700 Subject: [PATCH] I forgot what I did because I have been asleep for 16 hours --- shell.nix | 1 + src/lib/router.py | 63 ++++++++++++++++++++++++++++++++++++----------- 2 files changed, 50 insertions(+), 14 deletions(-) diff --git a/shell.nix b/shell.nix index 49322b8..f53f2dc 100644 --- a/shell.nix +++ b/shell.nix @@ -11,6 +11,7 @@ pkgs.mkShell { pkgs.python312Packages.pygraphviz pkgs.python312Packages.requests_toolbelt pkgs.python312Packages.pyaml + pkgs.python312Packages.pillow ]; shellHook = '' python src/main.py diff --git a/src/lib/router.py b/src/lib/router.py index b3450f4..9ff219d 100644 --- a/src/lib/router.py +++ b/src/lib/router.py @@ -13,6 +13,10 @@ import os import traceback import mimetypes import hashlib +import base64 + +with open('files/secrets', 'r') as f: + allowed_secrets = f.read().split('\n') @dataclass class Route: @@ -32,7 +36,7 @@ class Route: return response except Exception as e: - log.error(traceback.format_exc) + log.error(traceback.format_exc()) return error_page(500) def matches(self, request: 'Request') -> bool: @@ -40,20 +44,45 @@ class Route: return self.matcher(request.path) def generate_opengraph_html(file_url): - print('FILE URL: ', file_url) mime_type, _ = mimetypes.guess_type(file_url) - file_name = os.path.basename(file_url) + og_meta = '' + twitter_meta = '' + if mime_type and mime_type.startswith('image/'): content_type = 'image' embed_html = f'{file_name}' + og_meta = f''' + + + + + ''' elif mime_type and mime_type.startswith('video/'): content_type = 'video' - embed_html = f'' + embed_html = f'' + + og_meta = f""" + + + + + + + """ + + twitter_meta = f""" + + + + + + """ else: content_type = 'document' embed_html = f'' + og_meta = '' html_content = f""" @@ -63,19 +92,11 @@ def generate_opengraph_html(file_url): Embed File: {file_name} - - - - - + {og_meta} + {twitter_meta}

{file_name}

@@ -127,6 +148,20 @@ routes = [ ResponseCode.OK, *raw_file_contents('.'+request.path.path) ) if request.path.params['hash'] == compute_md5('.'+request.path.path) else error_page(403) + ), + Route( + lambda request: request.path == '/post', + [Method.POST], + lambda request, *_: [print(request), Response( + ResponseCode.OK, + {'Content-Type': 'text/html'}, + (lambda f: [f.write(base64.b64decode(request.body.content)), f.close(), f'https://files.natalieee.net/{request.path.params['filename']}?hash={compute_md5(request.path.params['filename'])}'][-1])(open(request.path.params['filename'], 'wb')).encode('utf-8') + ) if request.path.params['secret'] in allowed_secrets else Response(*error_page(403))][-1] + ), + Route( + lambda request: True, + [Method.GET], + lambda *_: Response(*error_page(404)) ) ]