saifisvibin commited on
Commit
0deeea1
·
verified ·
1 Parent(s): e963d81

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -2
app.py CHANGED
@@ -348,9 +348,9 @@ def process_file_background(task_id: str, file_data: bytes, filename: str, extra
348
  output_dir = Path(app.config['OUTPUT_FOLDER']) / stem
349
  output_dir.mkdir(parents=True, exist_ok=True)
350
 
351
- # Copy PDF to output directory
352
  pdf_path = output_dir / filename
353
- upload_path.rename(pdf_path)
354
 
355
  _update_task_progress(task_id, filename, 25, f'Loading model and processing {filename}...')
356
 
@@ -812,6 +812,48 @@ def delete_pdf_by_path(stem: str):
812
  return jsonify({'error': str(e)}), 500
813
 
814
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
815
  if __name__ == '__main__':
816
  # Run on port 7860 for Hugging Face Spaces, or 5000 for local development
817
  port = int(os.environ.get('PORT', 7860))
 
348
  output_dir = Path(app.config['OUTPUT_FOLDER']) / stem
349
  output_dir.mkdir(parents=True, exist_ok=True)
350
 
351
+ # Copy PDF to output directory, using replace to overwrite if it already exists
352
  pdf_path = output_dir / filename
353
+ upload_path.replace(pdf_path)
354
 
355
  _update_task_progress(task_id, filename, 25, f'Loading model and processing {filename}...')
356
 
 
812
  return jsonify({'error': str(e)}), 500
813
 
814
 
815
+ @app.route('/api/download-zip/<path:stem>', methods=['GET'])
816
+ def download_zip(stem: str):
817
+ """Download the processed output as a zip archive."""
818
+ import io
819
+ import zipfile
820
+
821
+ stem = stem.strip()
822
+ if not stem:
823
+ return jsonify({'error': 'Missing stem'}), 400
824
+
825
+ output_root = Path(app.config['OUTPUT_FOLDER']).resolve()
826
+ target_dir = (output_root / stem).resolve()
827
+
828
+ # Prevent path traversal
829
+ if output_root not in target_dir.parents and target_dir != output_root:
830
+ return jsonify({'error': 'Invalid stem path'}), 400
831
+
832
+ if not target_dir.exists() or not target_dir.is_dir():
833
+ return jsonify({'error': 'PDF not found or not processed completely'}), 404
834
+
835
+ try:
836
+ memory_file = io.BytesIO()
837
+ with zipfile.ZipFile(memory_file, 'w', zipfile.ZIP_DEFLATED) as zf:
838
+ for root, _, files in os.walk(target_dir):
839
+ for file in files:
840
+ file_path = Path(root) / file
841
+ arcname = file_path.relative_to(target_dir)
842
+ zf.write(file_path, arcname)
843
+
844
+ memory_file.seek(0)
845
+
846
+ return send_file(
847
+ memory_file,
848
+ mimetype='application/zip',
849
+ as_attachment=True,
850
+ download_name=f"{stem}_extracted.zip"
851
+ )
852
+ except Exception as e:
853
+ logger.error(f"Zip creation failed: {e}")
854
+ return jsonify({'error': str(e)}), 500
855
+
856
+
857
  if __name__ == '__main__':
858
  # Run on port 7860 for Hugging Face Spaces, or 5000 for local development
859
  port = int(os.environ.get('PORT', 7860))