Add download route and improve ZIP creation with error handling

This commit is contained in:
nmemmert committed 2025-11-06 10:35:10 -05:00
1 parent db6da360f7
commit 5c6fb1e935
1 file changed
+30 -10
+30 -10
View File
@@ -162,16 +162,22 @@ def process_job(job_id, files):
# Create ZIP of the entire Episode directory structure # Create ZIP of the entire Episode directory structure
successful_files = [p for p in processed_files if p[0] == 'success'] successful_files = [p for p in processed_files if p[0] == 'success']
if successful_files: if successful_files:
zip_output = os.path.join(app.config['UPLOAD_FOLDER'], f'renamed_{job_id}.zip') try:
with zipfile.ZipFile(zip_output, 'w') as zip_out: zip_output = os.path.join(app.config['UPLOAD_FOLDER'], f'renamed_{job_id}.zip')
for root, dirs, files_in_dir in os.walk(episode_dir): with zipfile.ZipFile(zip_output, 'w', zipfile.ZIP_DEFLATED) as zip_out:
for file in files_in_dir: for root, dirs, files_in_dir in os.walk(episode_dir):
file_path = os.path.join(root, file) for file in files_in_dir:
arcname = os.path.relpath(file_path, episode_dir) file_path = os.path.join(root, file)
zip_out.write(file_path, arcname) arcname = os.path.relpath(file_path, episode_dir)
job_queue[job_id]['zip_path'] = zip_output zip_out.write(file_path, arcname)
job_queue[job_id]['zip_name'] = f'renamed_{job_id}.zip' job_queue[job_id]['zip_path'] = zip_output
job_queue[job_id]['progress'] = f'Created ZIP archive with {len(successful_files)} organized files' 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]['status'] = 'completed'
job_queue[job_id]['processed_files'] = processed_files job_queue[job_id]['processed_files'] = processed_files
@@ -353,6 +359,7 @@ def jobs():
.status.queued { background-color: #ffc107; } .status.queued { background-color: #ffc107; }
.status.processing { background-color: #17a2b8; } .status.processing { background-color: #17a2b8; }
.status.completed { background-color: #28a745; } .status.completed { background-color: #28a745; }
.status.error { background-color: #dc3545; }
.progress-text { color: #17a2b8; font-style: italic; } .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 { background-color: #007bff; color: white; text-decoration: none; padding: 8px 16px; border-radius: 5px; margin-left: 10px; }
.download-btn:hover { background-color: #0056b3; } .download-btn:hover { background-color: #0056b3; }
@@ -377,6 +384,8 @@ def jobs():
{% if job.status == 'completed' %} {% if job.status == 'completed' %}
{% if job.zip_name %} {% if job.zip_name %}
<p><a href="/download/{{ job.zip_name }}" class="download-btn">Download ZIP</a></p> <p><a href="/download/{{ job.zip_name }}" class="download-btn">Download ZIP</a></p>
{% else %}
<p><em>No files were successfully processed to create a ZIP archive.</em></p>
{% endif %} {% endif %}
{% if job.skipped %} {% if job.skipped %}
<p><strong>Skipped Files:</strong></p> <p><strong>Skipped Files:</strong></p>
@@ -396,5 +405,16 @@ def jobs():
</html> </html>
''', jobs=job_queue) ''', jobs=job_queue)
@app.route('/download/<filename>')
@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__': if __name__ == '__main__':
app.run(debug=True, port=3000) app.run(debug=True, port=3000)