bluenevus commited on
Commit
47cb5aa
·
1 Parent(s): 6317bb9

Update app.py via AI Editor

Browse files
Files changed (1) hide show
  1. app.py +14 -6
app.py CHANGED
@@ -20,10 +20,10 @@ SESSION_DATA = {}
20
  SESSION_LOCKS = {}
21
 
22
  def get_session_id():
23
- session_id = flask.request.cookies.get('session-id')
24
- if not session_id:
25
- session_id = str(uuid.uuid4())
26
- return session_id
27
 
28
  def get_session_dir(session_id):
29
  base_tmp = tempfile.gettempdir()
@@ -38,6 +38,7 @@ def clean_session(session_id):
38
  shutil.rmtree(session_dir)
39
  SESSION_DATA.pop(session_id, None)
40
  SESSION_LOCKS.pop(session_id, None)
 
41
  except Exception as e:
42
  logging.error(f"Error cleaning session {session_id}: {e}")
43
 
@@ -212,7 +213,10 @@ app.layout = dbc.Container(
212
  )
213
  def handle_upload(contents, filename, clear_n, delete_upload_n_list, split_n, session_data):
214
  trigger = ctx.triggered_id
215
- session_id = get_session_id()
 
 
 
216
  flask.g.session_id = session_id
217
  session_dir = get_session_dir(session_id)
218
  lock = get_session_lock(session_id)
@@ -224,6 +228,7 @@ def handle_upload(contents, filename, clear_n, delete_upload_n_list, split_n, se
224
  if trigger == 'clear-session':
225
  clean_session(session_id)
226
  resp_data = {}
 
227
  return "", True, get_split_results_placeholder(), resp_data
228
 
229
  # Handle Delete Upload (detect ANY delete button press)
@@ -239,6 +244,7 @@ def handle_upload(contents, filename, clear_n, delete_upload_n_list, split_n, se
239
  pdf_path = os.path.join(session_dir, orig_filename)
240
  if os.path.exists(pdf_path):
241
  os.remove(pdf_path)
 
242
  session_data = {}
243
  if os.path.exists(session_dir):
244
  for file in os.listdir(session_dir):
@@ -293,12 +299,14 @@ def handle_upload(contents, filename, clear_n, delete_upload_n_list, split_n, se
293
  if trigger == 'split-btn':
294
  orig_filename = session_data.get('orig_filename')
295
  if not orig_filename:
 
296
  return html.Div("No file to split.", style={'color': 'red'}), True, get_split_results_placeholder(), session_data
297
  pdf_path = os.path.join(session_dir, orig_filename)
298
  if not os.path.exists(pdf_path):
 
299
  return html.Div("Uploaded file not found. Please upload again.", style={'color': 'red'}), True, get_split_results_placeholder(), {}
300
  try:
301
- logging.info(f"Splitting PDF for session {session_id} on user request.")
302
  with lock:
303
  split_files = intelligent_pdf_split(pdf_path, session_dir)
304
  zip_path = make_zip_of_splits(split_files, session_dir)
 
20
  SESSION_LOCKS = {}
21
 
22
  def get_session_id():
23
+ sid = flask.request.cookies.get('session-id')
24
+ if not sid:
25
+ sid = str(uuid.uuid4())
26
+ return sid
27
 
28
  def get_session_dir(session_id):
29
  base_tmp = tempfile.gettempdir()
 
38
  shutil.rmtree(session_dir)
39
  SESSION_DATA.pop(session_id, None)
40
  SESSION_LOCKS.pop(session_id, None)
41
+ logging.info(f"Cleaned session for {session_id}")
42
  except Exception as e:
43
  logging.error(f"Error cleaning session {session_id}: {e}")
44
 
 
213
  )
214
  def handle_upload(contents, filename, clear_n, delete_upload_n_list, split_n, session_data):
215
  trigger = ctx.triggered_id
216
+ # Always get current session id from cookie
217
+ session_id = flask.request.cookies.get('session-id')
218
+ if not session_id:
219
+ session_id = str(uuid.uuid4())
220
  flask.g.session_id = session_id
221
  session_dir = get_session_dir(session_id)
222
  lock = get_session_lock(session_id)
 
228
  if trigger == 'clear-session':
229
  clean_session(session_id)
230
  resp_data = {}
231
+ logging.info(f"Session cleared for {session_id}")
232
  return "", True, get_split_results_placeholder(), resp_data
233
 
234
  # Handle Delete Upload (detect ANY delete button press)
 
244
  pdf_path = os.path.join(session_dir, orig_filename)
245
  if os.path.exists(pdf_path):
246
  os.remove(pdf_path)
247
+ logging.info(f"Deleted uploaded file {pdf_path} for session {session_id}")
248
  session_data = {}
249
  if os.path.exists(session_dir):
250
  for file in os.listdir(session_dir):
 
299
  if trigger == 'split-btn':
300
  orig_filename = session_data.get('orig_filename')
301
  if not orig_filename:
302
+ logging.error(f"Split button clicked but no file to split for session {session_id}")
303
  return html.Div("No file to split.", style={'color': 'red'}), True, get_split_results_placeholder(), session_data
304
  pdf_path = os.path.join(session_dir, orig_filename)
305
  if not os.path.exists(pdf_path):
306
+ logging.error(f"Split button clicked but uploaded file not found for session {session_id}")
307
  return html.Div("Uploaded file not found. Please upload again.", style={'color': 'red'}), True, get_split_results_placeholder(), {}
308
  try:
309
+ logging.info(f"Splitting PDF for session {session_id} on user request. File: {pdf_path}")
310
  with lock:
311
  split_files = intelligent_pdf_split(pdf_path, session_dir)
312
  zip_path = make_zip_of_splits(split_files, session_dir)