Spaces:
Paused
Paused
Update app.py
Browse files
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 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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")
|