bluenevus commited on
Commit
39ae163
·
verified ·
1 Parent(s): 38a4541

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -12
app.py CHANGED
@@ -13,21 +13,24 @@ def compress_pdf(input_file, url, strength):
13
  if input_file is not None and url and url.strip() != "":
14
  return None, "Please provide either a file or a URL, not both."
15
 
16
- if url and url.strip() != "":
17
- try:
18
  response = requests.get(url)
19
  response.raise_for_status()
20
  pdf_content = io.BytesIO(response.content)
21
- except requests.RequestException as e:
22
- return None, f"Error downloading PDF: {str(e)}"
23
- else:
24
- pdf_content = input_file
25
-
26
- try:
27
- # Get the initial file size
28
- pdf_content.seek(0, os.SEEK_END)
29
- initial_size = pdf_content.tell()
30
- pdf_content.seek(0)
 
 
 
31
 
32
  reader = PyPDF2.PdfReader(pdf_content)
33
  writer = PyPDF2.PdfWriter()
@@ -85,6 +88,8 @@ def compress_pdf(input_file, url, strength):
85
  except Exception as e:
86
  return None, f"Error compressing PDF: {str(e)}"
87
 
 
 
88
  def process_and_compress(input_file, url, strength):
89
  sys.setrecursionlimit(10000)
90
 
 
13
  if input_file is not None and url and url.strip() != "":
14
  return None, "Please provide either a file or a URL, not both."
15
 
16
+ try:
17
+ if url and url.strip() != "":
18
  response = requests.get(url)
19
  response.raise_for_status()
20
  pdf_content = io.BytesIO(response.content)
21
+ initial_size = len(response.content)
22
+ else:
23
+ if hasattr(input_file, 'name'):
24
+ # If input_file is a file path
25
+ with open(input_file.name, 'rb') as file:
26
+ pdf_content = io.BytesIO(file.read())
27
+ initial_size = os.path.getsize(input_file.name)
28
+ else:
29
+ # If input_file is file-like object
30
+ pdf_content = io.BytesIO(input_file.read())
31
+ pdf_content.seek(0, io.SEEK_END)
32
+ initial_size = pdf_content.tell()
33
+ pdf_content.seek(0)
34
 
35
  reader = PyPDF2.PdfReader(pdf_content)
36
  writer = PyPDF2.PdfWriter()
 
88
  except Exception as e:
89
  return None, f"Error compressing PDF: {str(e)}"
90
 
91
+ # The rest of the code remains the same
92
+
93
  def process_and_compress(input_file, url, strength):
94
  sys.setrecursionlimit(10000)
95