diff --git a/app/routes.py b/app/routes.py index 2991762..425f63c 100644 --- a/app/routes.py +++ b/app/routes.py @@ -6,7 +6,7 @@ from app.utils import * # flask dependencies -from flask import Blueprint, request, render_template, jsonify, Response, stream_with_context, session, redirect, url_for, send_from_directory, current_app +from flask import Blueprint, request, render_template, Response, stream_with_context, session, redirect, url_for, send_from_directory, current_app, send_file import uuid, time @@ -27,18 +27,16 @@ def home(): @routes_bp.route('/viewer') def view(): session_id = session.get('session_id') - print('pulled', session_id) + if not session_id: + return "Session not found", 404 - session_template_path = os.path.join('static', 'sessions_data', session_id, 'output') - full_path = os.path.join(current_app.root_path, session_template_path) - - print('session_template_path:', session_template_path) - print('full_path:', full_path) + output_path = os.path.join(current_app.root_path, 'static', 'sessions_data', + session_id, 'output') - if not os.path.isdir(full_path): + if not os.path.isdir(output_path): return "Directory not found", 404 - return send_from_directory(full_path, 'viewer.html') + return send_from_directory(output_path, 'viewer.html') @routes_bp.route('/upload', methods=['POST']) def upload_file(): @@ -127,30 +125,43 @@ def generate(): @routes_bp.route('/upload/complete', methods=['GET']) def successful_upload(): print("Entering successful_upload function") - # Get the session ID from query parameter session_id = request.args.get('session_id') or session.get('session_id') - print('in successful upload func, id is: ', session_id) - if not session_id: print("No session ID found") return redirect(url_for('routes_bp.home')) try: - # make sure output.html can generate images - modify_img_paths(session_id) - # Generate the ZIP file and save it + # Generate the ZIP file with HTML and images generate_zip_file(session_id) - - print('before download url set') - download_url = url_for('static', filename=f'sessions_data/{session_id}/output/converted_xbrl.zip', _external=True) - print('download url is', download_url) - - return render_template("site/upload.html", session_id=session_id, download_url=download_url) + return render_template("site/upload.html", session_id=session_id) except Exception as e: print(f"Error in successful_upload: {str(e)}") return redirect(url_for('routes_bp.home')) +@routes_bp.route('/download_zip/') +def download_zip(session_id): + try: + # Use current_app.root_path to get absolute path + zip_path = os.path.join(current_app.root_path, 'static', 'sessions_data', + session_id, 'output', 'converted_xbrl.zip') + + if not os.path.exists(zip_path): + print(f"ZIP file not found at: {zip_path}") + return redirect(url_for('routes_bp.home')) + + print(f"Sending file from: {zip_path}") + + return send_file( + zip_path, + mimetype='application/zip', + as_attachment=True, + download_name='converted_xbrl.zip' + ) + except Exception as e: + print(f"Error in download_zip: {str(e)}") + return redirect(url_for('routes_bp.home')) + @routes_bp.route('/serve_image//') def serve_image(session_id, filename): # Specify the directory to send from. diff --git a/app/templates/site/upload.html b/app/templates/site/upload.html index 17a5074..155524a 100644 --- a/app/templates/site/upload.html +++ b/app/templates/site/upload.html @@ -12,7 +12,7 @@ {% block card1 %}
Download your machine-readable financial statement
diff --git a/app/utils/helper_functions.py b/app/utils/helper_functions.py index 441ca1c..8e3dcbe 100644 --- a/app/utils/helper_functions.py +++ b/app/utils/helper_functions.py @@ -129,32 +129,22 @@ def generate_zip_file(session_id): img_directory_path = os.path.join('app/static', 'sessions_data', session_id, 'input', 'img') zip_file_path = os.path.join(output_dir, 'converted_xbrl.zip') - # Ensure the output directory exists - os.makedirs(output_dir, exist_ok=True) - - # Debug prints to check paths - print(f"HTML file path: {html_file_path}") - print(f"Image directory path: {img_directory_path}") - print(f"ZIP file path: {zip_file_path}") - - # Create ZIP file - with zipfile.ZipFile(zip_file_path, 'w', zipfile.ZIP_DEFLATED) as zf: - # Add HTML file to zip - if os.path.exists(html_file_path): - print(f"Adding HTML file to zip: {html_file_path}") - zf.write(html_file_path, os.path.basename(html_file_path)) - else: - print(f"HTML file not found: {html_file_path}") + try: + with zipfile.ZipFile(zip_file_path, 'w', zipfile.ZIP_DEFLATED) as zf: + # Add HTML file + if os.path.exists(html_file_path): + zf.write(html_file_path, 'output.html') + + # Add images if they exist + if os.path.exists(img_directory_path): + for root, dirs, files in os.walk(img_directory_path): + for file in files: + file_path = os.path.join(root, file) + archive_name = os.path.join('img', file) + zf.write(file_path, archive_name) - # Add images to zip - if os.path.exists(img_directory_path): - for root, dirs, files in os.walk(img_directory_path): - for file in files: - file_path = os.path.join(root, file) - archive_name = os.path.relpath(file_path, img_directory_path) - print(f"Adding image to zip: {file_path} as {archive_name}") - zf.write(file_path, archive_name) - else: - print(f"Image directory not found: {img_directory_path}") - - zf.close() \ No newline at end of file + return zip_file_path + + except Exception as e: + print(f"Error generating zip file: {str(e)}") + raise \ No newline at end of file