Try another fix
This commit is contained in:
+15
-7
@@ -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/<path:filename>')
|
||||
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'
|
||||
# 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():
|
||||
|
||||
Reference in New Issue
Block a user