Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,11 @@
|
|
| 1 |
-
from flask import Flask, request, send_file, jsonify
|
| 2 |
from flask_cors import CORS
|
| 3 |
import fitz # PyMuPDF
|
| 4 |
import os
|
| 5 |
import tempfile
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
-
CORS(app)
|
| 9 |
|
| 10 |
def compress_pdf(input_path, output_path):
|
| 11 |
"""
|
|
@@ -28,7 +28,6 @@ def compress():
|
|
| 28 |
if not target_size_str:
|
| 29 |
return jsonify({"error": "Target size not specified."}), 400
|
| 30 |
|
| 31 |
-
# Handle KB or MB
|
| 32 |
try:
|
| 33 |
if target_size_str.lower().endswith("mb"):
|
| 34 |
target_kb = float(target_size_str[:-2]) * 1024
|
|
@@ -50,7 +49,7 @@ def compress():
|
|
| 50 |
|
| 51 |
final_size_kb = os.path.getsize(output_path) / 1024
|
| 52 |
|
| 53 |
-
if final_size_kb > target_kb + 50:
|
| 54 |
os.remove(output_path)
|
| 55 |
return jsonify({"error": f"Unable to compress below {target_kb} KB. Compressed size was {int(final_size_kb)} KB."}), 500
|
| 56 |
|
|
|
|
| 1 |
+
from flask import Flask, request, send_file, jsonify
|
| 2 |
from flask_cors import CORS
|
| 3 |
import fitz # PyMuPDF
|
| 4 |
import os
|
| 5 |
import tempfile
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
+
CORS(app) # Allow all origins by default (or restrict via origins=["http://localhost:3000"])
|
| 9 |
|
| 10 |
def compress_pdf(input_path, output_path):
|
| 11 |
"""
|
|
|
|
| 28 |
if not target_size_str:
|
| 29 |
return jsonify({"error": "Target size not specified."}), 400
|
| 30 |
|
|
|
|
| 31 |
try:
|
| 32 |
if target_size_str.lower().endswith("mb"):
|
| 33 |
target_kb = float(target_size_str[:-2]) * 1024
|
|
|
|
| 49 |
|
| 50 |
final_size_kb = os.path.getsize(output_path) / 1024
|
| 51 |
|
| 52 |
+
if final_size_kb > target_kb + 50:
|
| 53 |
os.remove(output_path)
|
| 54 |
return jsonify({"error": f"Unable to compress below {target_kb} KB. Compressed size was {int(final_size_kb)} KB."}), 500
|
| 55 |
|