diff --git a/src/run.py b/src/run.py index 1b2e40a..a1dab00 100644 --- a/src/run.py +++ b/src/run.py @@ -8,7 +8,7 @@ from flask import render_template from flask_migrate import upgrade import awsgi import base64 - +import mimetypes from flask import send_from_directory, request, make_response app = create_app() @@ -16,16 +16,24 @@ app = create_app() # Patch static file serving to base64-encode for Lambda/awsgi @app.route('/static/') def static_files(filename): - # Serve static files with base64 encoding for Lambda static_folder = os.path.join(app.root_path, 'static') - response = make_response(send_from_directory(static_folder, filename)) - # Only base64-encode for binary types - content_type = response.headers.get('Content-Type', '') + file_path = os.path.join(static_folder, filename) + content_type, _ = mimetypes.guess_type(file_path) + if not content_type: + content_type = "application/octet-stream" binary_types = ["application/octet-stream", "image/", "application/pdf", "font/"] if any(content_type.startswith(b) for b in binary_types): - # Mark for lambda_handler to base64 encode - response.headers['X-Is-Binary'] = '1' - return response + # Read file as bytes and base64 encode + with open(file_path, "rb") as f: + data = f.read() + encoded = base64.b64encode(data).decode("utf-8") + response = make_response(encoded) + response.headers["Content-Type"] = content_type + response.headers["Content-Transfer-Encoding"] = "base64" + response.headers["X-Is-Binary"] = "1" + return response + # For non-binary, use normal send_from_directory + return send_from_directory(static_folder, filename) @app.cli.command("init-db") def init_db_command():