mac9087 commited on
Commit
efc9cd2
·
verified ·
1 Parent(s): 97de2ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -8
app.py CHANGED
@@ -92,14 +92,20 @@ def compress():
92
 
93
  # If final size is too large, inform but still provide the file
94
  if final_size_kb > target_kb:
95
- return jsonify({
96
- "warning": f"Unable to compress below {target_kb:.2f} KB. Best compressed size is {int(final_size_kb)} KB.",
97
- "original_size_kb": round(original_size_kb, 2),
98
- "compressed_size_kb": round(final_size_kb, 2),
99
- "target_size_kb": round(target_kb, 2),
100
- "compression_ratio": round((original_size_kb - final_size_kb) / original_size_kb * 100, 2),
101
- "download_available": True
102
- }), 200
 
 
 
 
 
 
103
 
104
  # Return the compressed file
105
  response = send_file(output_path, as_attachment=True, download_name="compressed.pdf")
 
92
 
93
  # If final size is too large, inform but still provide the file
94
  if final_size_kb > target_kb:
95
+ # Important! Check Accept header to determine what client expects
96
+ accepts = request.headers.get('Accept', '')
97
+
98
+ # If client expects JSON (or we're not sure), send the warning JSON first
99
+ if 'application/json' in accepts or '*/*' in accepts:
100
+ # The frontend should handle this warning specially
101
+ return jsonify({
102
+ "warning": f"Unable to compress below {target_kb:.2f} KB. Best compressed size is {int(final_size_kb)} KB.",
103
+ "original_size_kb": round(original_size_kb, 2),
104
+ "compressed_size_kb": round(final_size_kb, 2),
105
+ "target_size_kb": round(target_kb, 2),
106
+ "compression_ratio": round((original_size_kb - final_size_kb) / original_size_kb * 100, 2),
107
+ "download_available": True
108
+ }), 200
109
 
110
  # Return the compressed file
111
  response = send_file(output_path, as_attachment=True, download_name="compressed.pdf")