I forgot what I did because I have been asleep for 16 hours

This commit is contained in:
gnat 2024-09-29 09:14:16 -07:00
parent faf711fbdf
commit 1f074f80f5
2 changed files with 50 additions and 14 deletions

View File

@ -11,6 +11,7 @@ pkgs.mkShell {
pkgs.python312Packages.pygraphviz pkgs.python312Packages.pygraphviz
pkgs.python312Packages.requests_toolbelt pkgs.python312Packages.requests_toolbelt
pkgs.python312Packages.pyaml pkgs.python312Packages.pyaml
pkgs.python312Packages.pillow
]; ];
shellHook = '' shellHook = ''
python src/main.py python src/main.py

View File

@ -13,6 +13,10 @@ import os
import traceback import traceback
import mimetypes import mimetypes
import hashlib import hashlib
import base64
with open('files/secrets', 'r') as f:
allowed_secrets = f.read().split('\n')
@dataclass @dataclass
class Route: class Route:
@ -32,7 +36,7 @@ class Route:
return response return response
except Exception as e: except Exception as e:
log.error(traceback.format_exc) log.error(traceback.format_exc())
return error_page(500) return error_page(500)
def matches(self, request: 'Request') -> bool: def matches(self, request: 'Request') -> bool:
@ -40,20 +44,45 @@ class Route:
return self.matcher(request.path) return self.matcher(request.path)
def generate_opengraph_html(file_url): def generate_opengraph_html(file_url):
print('FILE URL: ', file_url)
mime_type, _ = mimetypes.guess_type(file_url) mime_type, _ = mimetypes.guess_type(file_url)
file_name = os.path.basename(file_url) file_name = os.path.basename(file_url)
og_meta = ''
twitter_meta = ''
if mime_type and mime_type.startswith('image/'): if mime_type and mime_type.startswith('image/'):
content_type = 'image' content_type = 'image'
embed_html = f'<img src="{file_url}" alt="{file_name}" style="max-width: 100%; height: auto;">' embed_html = f'<img src="{file_url}" alt="{file_name}" style="max-width: 100%; height: auto;">'
og_meta = f'''
<meta property="og:image" content="{file_url}" />
<meta property="og:url" content="{file_url}" />
<meta property="og:image:url" content="{file_url}" />
<meta property="og:image:width" content="500" />
'''
elif mime_type and mime_type.startswith('video/'): elif mime_type and mime_type.startswith('video/'):
content_type = 'video' content_type = 'video'
embed_html = f'<video controls style="max-width: 100%;"><source src="{file_url}" type="{mime_type}">your browser does not support the video tag.</video>' embed_html = f'<video controls style="max-width: 100%;"><source src="{file_url}" type="{mime_type}">Your browser does not support the video tag.</video>'
og_meta = f"""
<meta property="og:video" content="{file_url}" />
<meta property="og:video:url" content="{file_url}" />
<meta property="og:video:secure_url" content="{file_url}" />
<meta property="og:video:type" content="{mime_type}" />
<meta property="og:video:width" content="406" />
<meta property="og:video:height" content="720" />
"""
twitter_meta = f"""
<meta name="twitter:card" content="player" />
<meta name="twitter:title" content="{file_name}" />
<meta name="twitter:player" content="{file_url}" />
<meta name="twitter:player:width" content="406" />
<meta name="twitter:player:height" content="720" />
"""
else: else:
content_type = 'document' content_type = 'document'
embed_html = f'<iframe src="{file_url}" title="{file_name}" style="width: 100%; height: 600px; border: none;"></iframe>' embed_html = f'<iframe src="{file_url}" title="{file_name}" style="width: 100%; height: 600px; border: none;"></iframe>'
og_meta = ''
html_content = f""" html_content = f"""
<!DOCTYPE html> <!DOCTYPE html>
@ -63,19 +92,11 @@ def generate_opengraph_html(file_url):
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Embed File: {file_name}</title> <title>Embed File: {file_name}</title>
<!-- OpenGraph Meta Tags -->
<meta property="og:title" content="{file_name}" /> <meta property="og:title" content="{file_name}" />
<meta property="og:type" content="{content_type}" /> <meta property="og:type" content="{content_type}" />
<meta property="og:url" content="{file_url}" /> <meta property="og:url" content="{file_url}" />
<meta property="og:description" content="Embedded file: {file_name}" /> {og_meta}
<meta property="og:image" content="URL_TO_AN_IMAGE" /> <!-- Optional: Replace with an actual image URL --> {twitter_meta}
<style>
body {{
font-family: Arial, sans-serif;
margin: 20px;
}}
</style>
</head> </head>
<body> <body>
<p>{file_name}</p> <p>{file_name}</p>
@ -127,6 +148,20 @@ routes = [
ResponseCode.OK, ResponseCode.OK,
*raw_file_contents('.'+request.path.path) *raw_file_contents('.'+request.path.path)
) if request.path.params['hash'] == compute_md5('.'+request.path.path) else error_page(403) ) 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))
) )
] ]