gnumanth commited on
Commit
0a27e5d
·
verified ·
1 Parent(s): 5a13c15

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -7
app.py CHANGED
@@ -21,19 +21,45 @@ def process_with_markitdown(input_path):
21
  except Exception as e:
22
  return f"Error processing input: {str(e)}"
23
 
 
 
 
 
24
  def save_uploaded_file(file):
25
- """Save uploaded file to temporary location and return path"""
 
 
26
  if file is None:
27
  return None
28
-
29
  try:
30
  temp_dir = tempfile.gettempdir()
31
- temp_path = os.path.join(temp_dir, file.name)
32
-
33
- with open(temp_path, 'wb') as f:
34
- f.write(file.read())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
- return temp_path
 
 
 
 
37
  except Exception as e:
38
  return f"Error saving file: {str(e)}"
39
 
 
21
  except Exception as e:
22
  return f"Error processing input: {str(e)}"
23
 
24
+ import tempfile
25
+ import os
26
+ import io
27
+
28
  def save_uploaded_file(file):
29
+ """Save uploaded file to temporary location and return path.
30
+ Handles only file uploads, not URLs."""
31
+
32
  if file is None:
33
  return None
34
+
35
  try:
36
  temp_dir = tempfile.gettempdir()
37
+ temp_filename = tempfile.mkstemp(dir=temp_dir, suffix=os.path.splitext(file.name)[1])[1]
38
+
39
+ # Crucial: Check for valid file-like object
40
+ if hasattr(file, 'read'):
41
+ with open(temp_filename, 'wb') as f:
42
+ try:
43
+ while True:
44
+ chunk = file.read(8192)
45
+ if not chunk:
46
+ break
47
+ f.write(chunk)
48
+ except Exception as e:
49
+ return f"Error during file write: {str(e)}"
50
+
51
+ elif hasattr(file, 'getvalue'): # In-memory file-like
52
+ with open(temp_filename, 'wb') as f:
53
+ try:
54
+ f.write(file.getvalue())
55
+ except Exception as e:
56
+ return f"Error during file write: {str(e)}"
57
 
58
+ else:
59
+ return "Error: Unsupported file type."
60
+
61
+ return temp_filename
62
+
63
  except Exception as e:
64
  return f"Error saving file: {str(e)}"
65