Revert static file fix

This commit is contained in:
Anup Raj Gopinathan
2025-06-25 16:49:02 +02:00
parent cc74e3ef41
commit ec05c201f2
+1 -39
View File
@@ -7,34 +7,9 @@ from logging.handlers import RotatingFileHandler
from flask import render_template from flask import render_template
from flask_migrate import upgrade from flask_migrate import upgrade
import awsgi import awsgi
import base64
import mimetypes
from flask import send_from_directory, request, make_response
app = create_app() app = create_app()
# Patch static file serving to base64-encode for Lambda/awsgi
@app.route('/static/<path:filename>')
def static_files(filename):
static_folder = os.path.join(app.root_path, 'static')
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):
# 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") @app.cli.command("init-db")
def init_db_command(): def init_db_command():
"""Initialize the database.""" """Initialize the database."""
@@ -69,20 +44,7 @@ if __name__ == '__main__':
app.run(host='0.0.0.0', port=5004) app.run(host='0.0.0.0', port=5004)
def lambda_handler(event, context): def lambda_handler(event, context):
response = awsgi.response(app, event, context) return awsgi.response(app, event, context)
headers = response.get("headers", {})
# Only base64-encode if our custom header is set (for /static)
if headers.get("X-Is-Binary") == "1":
if not response.get("isBase64Encoded"):
body = response["body"]
if isinstance(body, str):
body = body.encode("latin1")
import base64
response["body"] = base64.b64encode(body).decode("utf-8")
response["isBase64Encoded"] = True
# Remove the custom header from the response
response["headers"].pop("X-Is-Binary", None)
return response
def lambda_handler_upgrade_db(event, context): def lambda_handler_upgrade_db(event, context):
with app.app_context(): with app.app_context():