Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

139 download blocked #182

Merged
merged 4 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 32 additions & 21 deletions app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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():
Expand Down Expand Up @@ -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/<session_id>')
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/<session_id>/<filename>')
def serve_image(session_id, filename):
# Specify the directory to send from.
Expand Down
2 changes: 1 addition & 1 deletion app/templates/site/upload.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
{% block card1 %}
<div class="card-header card-header-lg">Download your machine-readable financial statement</div>
<div class="card-body text-center">
<a href="{{ download_url }}" download>
<a href="{{ url_for('routes_bp.download_zip', session_id=session_id) }}">
<button type="button" class="btn btn-michigan btn-lg">Download file</button>
</a>
</div>
Expand Down
46 changes: 18 additions & 28 deletions app/utils/helper_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
return zip_file_path

except Exception as e:
print(f"Error generating zip file: {str(e)}")
raise
Loading