From 5c6fb1e9358cd8b260a3c0f9057ee75404e94c69 Mon Sep 17 00:00:00 2001 From: nmemmert <67961292+nmemmert@users.noreply.github.com> Date: Thu, 6 Nov 2025 10:35:10 -0500 Subject: [PATCH] Add download route and improve ZIP creation with error handling --- pdf_renamer.py | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/pdf_renamer.py b/pdf_renamer.py index 1c5de29..24fc920 100644 --- a/pdf_renamer.py +++ b/pdf_renamer.py @@ -162,16 +162,22 @@ def process_job(job_id, files): # Create ZIP of the entire Episode directory structure successful_files = [p for p in processed_files if p[0] == 'success'] if successful_files: - zip_output = os.path.join(app.config['UPLOAD_FOLDER'], f'renamed_{job_id}.zip') - with zipfile.ZipFile(zip_output, 'w') as zip_out: - for root, dirs, files_in_dir in os.walk(episode_dir): - for file in files_in_dir: - file_path = os.path.join(root, file) - arcname = os.path.relpath(file_path, episode_dir) - zip_out.write(file_path, arcname) - job_queue[job_id]['zip_path'] = zip_output - job_queue[job_id]['zip_name'] = f'renamed_{job_id}.zip' - job_queue[job_id]['progress'] = f'Created ZIP archive with {len(successful_files)} organized files' + try: + zip_output = os.path.join(app.config['UPLOAD_FOLDER'], f'renamed_{job_id}.zip') + with zipfile.ZipFile(zip_output, 'w', zipfile.ZIP_DEFLATED) as zip_out: + for root, dirs, files_in_dir in os.walk(episode_dir): + for file in files_in_dir: + file_path = os.path.join(root, file) + arcname = os.path.relpath(file_path, episode_dir) + zip_out.write(file_path, arcname) + job_queue[job_id]['zip_path'] = zip_output + job_queue[job_id]['zip_name'] = f'renamed_{job_id}.zip' + job_queue[job_id]['progress'] = f'Created ZIP archive with {len(successful_files)} organized files' + except Exception as e: + job_queue[job_id]['progress'] = f'Error creating ZIP: {str(e)}' + job_queue[job_id]['status'] = 'error' + else: + job_queue[job_id]['progress'] = 'No files to archive - all files were skipped' job_queue[job_id]['status'] = 'completed' job_queue[job_id]['processed_files'] = processed_files @@ -353,6 +359,7 @@ def jobs(): .status.queued { background-color: #ffc107; } .status.processing { background-color: #17a2b8; } .status.completed { background-color: #28a745; } + .status.error { background-color: #dc3545; } .progress-text { color: #17a2b8; font-style: italic; } .download-btn { background-color: #007bff; color: white; text-decoration: none; padding: 8px 16px; border-radius: 5px; margin-left: 10px; } .download-btn:hover { background-color: #0056b3; } @@ -377,6 +384,8 @@ def jobs(): {% if job.status == 'completed' %} {% if job.zip_name %}

Download ZIP

+ {% else %} +

No files were successfully processed to create a ZIP archive.

{% endif %} {% if job.skipped %}

Skipped Files:

@@ -396,5 +405,16 @@ def jobs(): ''', jobs=job_queue) +@app.route('/download/') +@login_required +def download(filename): + # Security check: only allow downloading files that belong to completed jobs + for job_id, job in job_queue.items(): + if job.get('status') == 'completed' and job.get('zip_name') == filename: + file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) + if os.path.exists(file_path): + return send_from_directory(app.config['UPLOAD_FOLDER'], filename, as_attachment=True) + return "File not found or access denied", 404 + if __name__ == '__main__': app.run(debug=True, port=3000) \ No newline at end of file