PDF inline fix 1

This commit is contained in:
2026-08-20 16:41:20 -04:00
parent 4066e2f7bd
commit fc62d77131
+22 -5
View File
@@ -725,12 +725,29 @@ def serve_file(filepath):
print(f"Serving file: {full_path}") print(f"Serving file: {full_path}")
# Determine MIME type # Determine MIME type. Don't rely solely on mimetypes.guess_type() -
mime_type, _ = mimetypes.guess_type(full_path) # its results can vary by OS/container base image depending on what
if mime_type is None: # system mime databases are present. Force the types that matter for
mime_type = 'application/octet-stream' # inline preview (PDF above all) so this can't silently regress.
ext = os.path.splitext(full_path)[1].lower()
KNOWN_MIME_TYPES = {
'.pdf': 'application/pdf',
'.html': 'text/html',
'.htm': 'text/html',
'.txt': 'text/plain',
'.md': 'text/plain',
}
if ext in KNOWN_MIME_TYPES:
mime_type = KNOWN_MIME_TYPES[ext]
else:
mime_type, _ = mimetypes.guess_type(full_path)
if mime_type is None:
mime_type = 'application/octet-stream'
return send_file(full_path, mimetype=mime_type) # as_attachment=False (the default) sends Content-Disposition: inline
# so the browser renders PDFs/HTML in the iframe instead of prompting
# a download. Being explicit here so this can't drift.
return send_file(full_path, mimetype=mime_type, as_attachment=False)
except Exception as e: except Exception as e:
print(f"Error serving file: {str(e)}") print(f"Error serving file: {str(e)}")
return f"Error serving file: {str(e)}", 500 return f"Error serving file: {str(e)}", 500