Cxops commited on
Commit
80e98ea
·
verified ·
1 Parent(s): 2768860

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -40
app.py CHANGED
@@ -285,7 +285,7 @@ async def send_progress_updater(
285
  return task, bytes_holder
286
 
287
  # =============================================================================
288
- # CORE UPLOAD / DOWNLOAD LOGIC (FIXED THREAD‑SAFE PROGRESS + NO chunk_size)
289
  # =============================================================================
290
 
291
  async def do_upload_job(
@@ -310,47 +310,11 @@ async def do_upload_job(
310
  [[InlineKeyboardButton("❌ Cancel", callback_data=f"cancel_job:{job_id}")]]
311
  ),
312
  )
313
- last_update = [0]
314
- download_start = time.time()
315
- total_size = expected_size if expected_size and expected_size > 0 else 0
316
-
317
- # Sync progress callback called from a thread -> use run_coroutine_threadsafe
318
- def download_progress(current, total):
319
- nonlocal total_size
320
- now = time.time()
321
- if now - last_update[0] < 3:
322
- return
323
- last_update[0] = now
324
- elapsed = max(time.time() - download_start, 0.01)
325
- speed = current / elapsed
326
- if total and total > 0:
327
- total_size = total
328
- elif total_size == 0 and expected_size and expected_size > 0:
329
- total_size = expected_size
330
- text = "⬇️ Downloading from Telegram...\n"
331
- if total_size > 0:
332
- pct = (100 * current / total_size)
333
- text += f"{pct:.1f}% of {format_size(total_size)}\n"
334
- text += f"{format_size(current)} downloaded / {format_size(total_size)}\n"
335
- else:
336
- text += f"{format_size(current)} downloaded\n"
337
- text += f"Speed: {format_speed(speed)}"
338
- # Schedule message update on the main event loop
339
- asyncio.run_coroutine_threadsafe(
340
- status_msg.edit_text(
341
- text,
342
- reply_markup=InlineKeyboardMarkup(
343
- [[InlineKeyboardButton("❌ Cancel", callback_data=f"cancel_job:{job_id}")]]
344
- ),
345
- ),
346
- app.loop
347
- )
348
 
349
- # Download from Telegram (no block_size)
350
  local_path = await app.download_media(
351
  file_msg_or_id,
352
  file_name=os.path.join(temp_dir, orig_filename),
353
- progress=download_progress,
354
  )
355
 
356
  if cancel_event.is_set():
@@ -372,7 +336,6 @@ async def do_upload_job(
372
 
373
  loop = asyncio.get_event_loop()
374
  def sync_upload():
375
- # No chunk_size argument (compatible with huggingface_hub)
376
  hf.upload_file(
377
  path_or_fileobj=local_path,
378
  path_in_repo=repo_path,
@@ -572,14 +535,18 @@ async def refresh_file_list(chat_id: int, user_id: int, message: Message):
572
  # =============================================================================
573
 
574
  def check_duplicate(user_id: int, orig_name: str) -> Optional[int]:
 
575
  row = db_file_exists(user_id, orig_name)
576
  if row:
577
- return row[1]
 
 
578
  enc_folder = get_encrypted_folder(user_id)
579
  enc_filename = get_encrypted_filename(orig_name)
580
  enc_path = f"{enc_folder}/{enc_filename}"
581
  try:
582
  if hf.file_exists(repo_id=DATASET_REPO, filename=enc_path, repo_type=DATASET_TYPE):
 
583
  db_add_file(user_id, enc_path, orig_name, 0)
584
  return 0
585
  except Exception as e:
@@ -611,6 +578,7 @@ async def handle_duplicate(
611
  reserved_names: Optional[Set[str]] = None,
612
  ):
613
  if expected_size is not None and existing_size != expected_size:
 
614
  base, ext = os.path.splitext(orig_filename)
615
  new_name = generate_unique_filename(user_id, base, ext, reserved_names)
616
  if reserved_names is not None:
@@ -625,6 +593,7 @@ async def handle_duplicate(
625
  f"Uploading as `{new_name}`."
626
  )
627
  else:
 
628
  token = secrets.token_hex(8)
629
  duplicate_prompt_map[token] = {
630
  'user_id': user_id,
 
285
  return task, bytes_holder
286
 
287
  # =============================================================================
288
+ # CORE UPLOAD / DOWNLOAD LOGIC (FIXED – NO PROGRESS CALLBACK DURING DOWNLOAD)
289
  # =============================================================================
290
 
291
  async def do_upload_job(
 
310
  [[InlineKeyboardButton("❌ Cancel", callback_data=f"cancel_job:{job_id}")]]
311
  ),
312
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
313
 
314
+ # ✅ Download without progress callback – prevents thread‑safety errors
315
  local_path = await app.download_media(
316
  file_msg_or_id,
317
  file_name=os.path.join(temp_dir, orig_filename),
 
318
  )
319
 
320
  if cancel_event.is_set():
 
336
 
337
  loop = asyncio.get_event_loop()
338
  def sync_upload():
 
339
  hf.upload_file(
340
  path_or_fileobj=local_path,
341
  path_in_repo=repo_path,
 
535
  # =============================================================================
536
 
537
  def check_duplicate(user_id: int, orig_name: str) -> Optional[int]:
538
+ # Check local DB first
539
  row = db_file_exists(user_id, orig_name)
540
  if row:
541
+ return row[1] # size
542
+
543
+ # Check HF dataset
544
  enc_folder = get_encrypted_folder(user_id)
545
  enc_filename = get_encrypted_filename(orig_name)
546
  enc_path = f"{enc_folder}/{enc_filename}"
547
  try:
548
  if hf.file_exists(repo_id=DATASET_REPO, filename=enc_path, repo_type=DATASET_TYPE):
549
+ # Add to DB with placeholder size (will be updated on next /files)
550
  db_add_file(user_id, enc_path, orig_name, 0)
551
  return 0
552
  except Exception as e:
 
578
  reserved_names: Optional[Set[str]] = None,
579
  ):
580
  if expected_size is not None and existing_size != expected_size:
581
+ # Different size → auto‑rename
582
  base, ext = os.path.splitext(orig_filename)
583
  new_name = generate_unique_filename(user_id, base, ext, reserved_names)
584
  if reserved_names is not None:
 
593
  f"Uploading as `{new_name}`."
594
  )
595
  else:
596
+ # Same size → prompt user
597
  token = secrets.token_hex(8)
598
  duplicate_prompt_map[token] = {
599
  'user_id': user_id,