Try another fix
This commit is contained in:
+16
-8
@@ -8,7 +8,7 @@ from flask import render_template
|
|||||||
from flask_migrate import upgrade
|
from flask_migrate import upgrade
|
||||||
import awsgi
|
import awsgi
|
||||||
import base64
|
import base64
|
||||||
|
import mimetypes
|
||||||
|
|
||||||
from flask import send_from_directory, request, make_response
|
from flask import send_from_directory, request, make_response
|
||||||
app = create_app()
|
app = create_app()
|
||||||
@@ -16,16 +16,24 @@ app = create_app()
|
|||||||
# Patch static file serving to base64-encode for Lambda/awsgi
|
# Patch static file serving to base64-encode for Lambda/awsgi
|
||||||
@app.route('/static/<path:filename>')
|
@app.route('/static/<path:filename>')
|
||||||
def static_files(filename):
|
def static_files(filename):
|
||||||
# Serve static files with base64 encoding for Lambda
|
|
||||||
static_folder = os.path.join(app.root_path, 'static')
|
static_folder = os.path.join(app.root_path, 'static')
|
||||||
response = make_response(send_from_directory(static_folder, filename))
|
file_path = os.path.join(static_folder, filename)
|
||||||
# Only base64-encode for binary types
|
content_type, _ = mimetypes.guess_type(file_path)
|
||||||
content_type = response.headers.get('Content-Type', '')
|
if not content_type:
|
||||||
|
content_type = "application/octet-stream"
|
||||||
binary_types = ["application/octet-stream", "image/", "application/pdf", "font/"]
|
binary_types = ["application/octet-stream", "image/", "application/pdf", "font/"]
|
||||||
if any(content_type.startswith(b) for b in binary_types):
|
if any(content_type.startswith(b) for b in binary_types):
|
||||||
# Mark for lambda_handler to base64 encode
|
# Read file as bytes and base64 encode
|
||||||
response.headers['X-Is-Binary'] = '1'
|
with open(file_path, "rb") as f:
|
||||||
return response
|
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():
|
||||||
|
|||||||
Reference in New Issue
Block a user