Cxops commited on
Commit
f2da24c
·
verified ·
1 Parent(s): 22ea417

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -7
app.py CHANGED
@@ -24,6 +24,7 @@ SPACE_HOST: str = "https://cxops-push.hf.space" # Keep‑alive URL
24
  DATASET_REPO: str = "Cxops/Bzxv"
25
  DATASET_TYPE: str = "dataset"
26
  MAX_FILE_SIZE_TG: int = 1900 * 1024 * 1024 # 1900 MB per Telegram part
 
27
  MIN_FREE_DISK: int = 45 * 1024 * 1024 * 1024 # 45 GB
28
  DATASET_MAX_SIZE: int = int(8.7 * 1024**4) # 8.7 TB
29
  KEEPALIVE_INTERVAL: int = 300 # 5 minutes
@@ -119,7 +120,7 @@ def db_file_exists(user_id: int, orig_name: str) -> Optional[str]:
119
  file_callback_map: dict[str, str] = {} # token → enc_path
120
  upload_prompt_map: dict[str, List[Tuple]] = {} # token → list of entries
121
  album_groups: dict[str, dict] = {}
122
- duplicate_prompt_map: dict[str, dict] = {} # token → {user_id, chat_id, file_msg_or_id, orig_filename, job_id, file_id, expected_size}
123
 
124
  def make_token() -> str:
125
  return secrets.token_hex(8)
@@ -276,7 +277,7 @@ async def do_upload_job(
276
  ) -> None:
277
  """
278
  Download file from Telegram (by message or file_id) then upload to Hugging Face.
279
- A timeout of UPLOAD_TIMEOUT is enforced.
280
  """
281
  temp_dir = tempfile.mkdtemp()
282
  local_path = None
@@ -340,6 +341,14 @@ async def do_upload_job(
340
 
341
  file_size = os.path.getsize(local_path)
342
 
 
 
 
 
 
 
 
 
343
  # ---- Upload to Hugging Face (no progress, with timeout) ----
344
  enc_folder = get_encrypted_folder(user_id)
345
  enc_filename = get_encrypted_filename(orig_filename)
@@ -530,7 +539,7 @@ async def refresh_file_list(chat_id: int, user_id: int, message: Message):
530
  token = make_token()
531
  file_callback_map[token] = enc_path
532
  buttons.append([
533
- InlineKeyboardButton(f"📥 {orig_name[:40]}", callback_data=f"get:{token}"),
534
  InlineKeyboardButton("🗑 Delete", callback_data=f"delete:{token}"),
535
  ])
536
  buttons.append([InlineKeyboardButton("⚠️ Delete All Files", callback_data=f"delete_all:{user_id}")])
@@ -573,7 +582,7 @@ async def process_album_group(media_group_id: str):
573
  for msg_id, fname, fid in messages:
574
  individual_token = make_token()
575
  upload_prompt_map[individual_token] = [(user_id, msg_id, fname, fid)]
576
- button_rows.append([InlineKeyboardButton(f"📤 {fname[:40]}", callback_data=f"upload:{individual_token}")])
577
  button_rows.append([InlineKeyboardButton("❌ Cancel", callback_data="cancel_upload:0")])
578
 
579
  await app.send_message(chat_id,
@@ -582,7 +591,7 @@ async def process_album_group(media_group_id: str):
582
 
583
 
584
  # =============================================================================
585
- # UPLOAD QUEUE WORKER
586
  # =============================================================================
587
 
588
  async def upload_worker():
@@ -592,6 +601,7 @@ async def upload_worker():
592
  cancel_event = asyncio.Event()
593
  active_jobs[job_id] = {"status": "running", "cancel_event": cancel_event}
594
 
 
595
  while get_free_disk() < MIN_FREE_DISK:
596
  logger.warning("Low disk space – pausing uploads")
597
  await asyncio.sleep(10)
@@ -636,7 +646,8 @@ async def cmd_help(_, message: Message):
636
  "/help – This help\n\n"
637
  "**Features:**\n"
638
  "• Duplicate detection – you'll be asked to skip or keep a duplicate.\n"
639
- "• File splitting for large files (>1.9 GB).\n"
 
640
  "• Real‑time progress with speed and ETA."
641
  )
642
  await message.reply_text(text, disable_web_page_preview=True)
@@ -696,7 +707,7 @@ async def cmd_files(_, message: Message):
696
  token = make_token()
697
  file_callback_map[token] = enc_path
698
  buttons.append([
699
- InlineKeyboardButton(f"📥 {orig_name[:40]}", callback_data=f"get:{token}"),
700
  InlineKeyboardButton("🗑 Delete", callback_data=f"delete:{token}"),
701
  ])
702
  buttons.append([InlineKeyboardButton("⚠️ Delete All Files", callback_data=f"delete_all:{user_id}")])
 
24
  DATASET_REPO: str = "Cxops/Bzxv"
25
  DATASET_TYPE: str = "dataset"
26
  MAX_FILE_SIZE_TG: int = 1900 * 1024 * 1024 # 1900 MB per Telegram part
27
+ MAX_SINGLE_FILE_SIZE: int = 45 * 1024 * 1024 * 1024 # 45 GB max per file upload
28
  MIN_FREE_DISK: int = 45 * 1024 * 1024 * 1024 # 45 GB
29
  DATASET_MAX_SIZE: int = int(8.7 * 1024**4) # 8.7 TB
30
  KEEPALIVE_INTERVAL: int = 300 # 5 minutes
 
120
  file_callback_map: dict[str, str] = {} # token → enc_path
121
  upload_prompt_map: dict[str, List[Tuple]] = {} # token → list of entries
122
  album_groups: dict[str, dict] = {}
123
+ duplicate_prompt_map: dict[str, dict] = {} # token → {user_id, chat_id, file_obj, ...}
124
 
125
  def make_token() -> str:
126
  return secrets.token_hex(8)
 
277
  ) -> None:
278
  """
279
  Download file from Telegram (by message or file_id) then upload to Hugging Face.
280
+ A timeout of UPLOAD_TIMEOUT is enforced, and files larger than MAX_SINGLE_FILE_SIZE are rejected.
281
  """
282
  temp_dir = tempfile.mkdtemp()
283
  local_path = None
 
341
 
342
  file_size = os.path.getsize(local_path)
343
 
344
+ # ---- Size limit check ----
345
+ if file_size > MAX_SINGLE_FILE_SIZE:
346
+ await status_msg.edit_text(
347
+ f"❌ File too large ({format_size(file_size)}). Maximum allowed is {format_size(MAX_SINGLE_FILE_SIZE)}.",
348
+ reply_markup=None,
349
+ )
350
+ return
351
+
352
  # ---- Upload to Hugging Face (no progress, with timeout) ----
353
  enc_folder = get_encrypted_folder(user_id)
354
  enc_filename = get_encrypted_filename(orig_filename)
 
539
  token = make_token()
540
  file_callback_map[token] = enc_path
541
  buttons.append([
542
+ InlineKeyboardButton(f"📥 {orig_name[:60]}", callback_data=f"get:{token}"),
543
  InlineKeyboardButton("🗑 Delete", callback_data=f"delete:{token}"),
544
  ])
545
  buttons.append([InlineKeyboardButton("⚠️ Delete All Files", callback_data=f"delete_all:{user_id}")])
 
582
  for msg_id, fname, fid in messages:
583
  individual_token = make_token()
584
  upload_prompt_map[individual_token] = [(user_id, msg_id, fname, fid)]
585
+ button_rows.append([InlineKeyboardButton(f"📤 {fname[:60]}", callback_data=f"upload:{individual_token}")])
586
  button_rows.append([InlineKeyboardButton("❌ Cancel", callback_data="cancel_upload:0")])
587
 
588
  await app.send_message(chat_id,
 
591
 
592
 
593
  # =============================================================================
594
+ # UPLOAD QUEUE WORKER (FIFO with disk space guard)
595
  # =============================================================================
596
 
597
  async def upload_worker():
 
601
  cancel_event = asyncio.Event()
602
  active_jobs[job_id] = {"status": "running", "cancel_event": cancel_event}
603
 
604
+ # Wait until enough disk space (at least 45 GB free)
605
  while get_free_disk() < MIN_FREE_DISK:
606
  logger.warning("Low disk space – pausing uploads")
607
  await asyncio.sleep(10)
 
646
  "/help – This help\n\n"
647
  "**Features:**\n"
648
  "• Duplicate detection – you'll be asked to skip or keep a duplicate.\n"
649
+ "• File splitting for large files (>1.9 GB) when sending back to Telegram.\n"
650
+ "• Maximum single file upload size: 45 GB.\n"
651
  "• Real‑time progress with speed and ETA."
652
  )
653
  await message.reply_text(text, disable_web_page_preview=True)
 
707
  token = make_token()
708
  file_callback_map[token] = enc_path
709
  buttons.append([
710
+ InlineKeyboardButton(f"📥 {orig_name[:60]}", callback_data=f"get:{token}"),
711
  InlineKeyboardButton("🗑 Delete", callback_data=f"delete:{token}"),
712
  ])
713
  buttons.append([InlineKeyboardButton("⚠️ Delete All Files", callback_data=f"delete_all:{user_id}")])