Cxops commited on
Commit
48f6247
·
verified ·
1 Parent(s): 318764a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -18
app.py CHANGED
@@ -1,3 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os, re, sys, time, base64, hashlib, shutil, logging, secrets, sqlite3, tempfile, threading, asyncio
2
  from pathlib import Path
3
  from typing import Optional, List, Tuple, Union
@@ -119,7 +131,6 @@ 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
- # Temporary storage for duplicate resolution
123
  duplicate_prompt_map: dict[str, dict] = {} # token → {user_id, chat_id, file_msg_or_id, orig_filename, job_id, file_id, expected_size}
124
 
125
  def make_token() -> str:
@@ -530,18 +541,14 @@ async def refresh_file_list(chat_id: int, user_id: int, message: Message):
530
  for enc_path, orig_name, _ in user_files:
531
  token = make_token()
532
  file_callback_map[token] = enc_path
533
- # Generate a direct Hugging Face link
534
- direct_url = hf_hub_url(repo_id=DATASET_REPO, filename=enc_path, repo_type=DATASET_TYPE)
535
  buttons.append([
536
- InlineKeyboardButton(f"📥 {orig_name[:30]}", callback_data=f"get:{token}"),
537
  InlineKeyboardButton("🗑 Delete", callback_data=f"delete:{token}"),
538
  ])
539
- # Add a separate row for the direct link
540
- buttons.append([InlineKeyboardButton(f"🔗 Direct Link", url=direct_url)])
541
  buttons.append([InlineKeyboardButton("⚠️ Delete All Files", callback_data=f"delete_all:{user_id}")])
542
 
543
  await message.edit_text(
544
- "Your uploaded files (click to download, delete to remove, or use the direct link):",
545
  reply_markup=InlineKeyboardMarkup(buttons),
546
  )
547
 
@@ -634,14 +641,13 @@ async def cmd_help(_, message: Message):
634
  text = (
635
  "**Available Commands:**\n\n"
636
  "/start – Welcome message\n"
637
- "/files – List your uploaded files (download/delete/direct link)\n"
638
  "/status – System & dataset space\n"
639
  "/delete_all – Delete all your files (confirmation)\n"
640
  "/upload <link> – Upload a file from a Telegram message link instantly\n"
641
  "/help – This help\n\n"
642
  "**Features:**\n"
643
  "• Duplicate detection – you'll be asked to skip or keep a duplicate.\n"
644
- "• Direct download links – click 🔗 to get a shareable link.\n"
645
  "• File splitting for large files (>1.9 GB).\n"
646
  "• Real‑time progress with speed and ETA."
647
  )
@@ -701,15 +707,13 @@ async def cmd_files(_, message: Message):
701
  for enc_path, orig_name, _ in user_files:
702
  token = make_token()
703
  file_callback_map[token] = enc_path
704
- direct_url = hf_hub_url(repo_id=DATASET_REPO, filename=enc_path, repo_type=DATASET_TYPE)
705
  buttons.append([
706
- InlineKeyboardButton(f"📥 {orig_name[:25]}", callback_data=f"get:{token}"),
707
  InlineKeyboardButton("🗑 Delete", callback_data=f"delete:{token}"),
708
  ])
709
- buttons.append([InlineKeyboardButton("🔗 Direct Link", url=direct_url)])
710
  buttons.append([InlineKeyboardButton("⚠️ Delete All Files", callback_data=f"delete_all:{user_id}")])
711
  await message.reply_text(
712
- "Your uploaded files (click to download, delete, or use the direct link):",
713
  reply_markup=InlineKeyboardMarkup(buttons),
714
  disable_web_page_preview=True,
715
  )
@@ -776,7 +780,7 @@ async def cmd_upload(_, message: Message):
776
  file_id = msg.voice.file_id
777
  else:
778
  filename = f"file_{msg_id}"
779
- # Check for duplicate
780
  existing = db_file_exists(message.from_user.id, filename)
781
  if existing:
782
  token = make_token()
@@ -824,14 +828,13 @@ async def cb_dup_keep(_, query: CallbackQuery):
824
  # Append a suffix like "_1" to make it unique
825
  base, ext = os.path.splitext(data['orig_filename'])
826
  new_filename = f"{base}_1{ext}"
827
- # Queue with the modified filename
828
  job_id = data['job_id']
829
  active_jobs[job_id] = {"status": "queued"}
830
  await upload_queue.put((
831
  data['user_id'],
832
  data['chat_id'],
833
  data['file_obj'],
834
- new_filename, # use modified name
835
  job_id,
836
  data['file_id'],
837
  data['expected_size'],
@@ -931,7 +934,6 @@ async def cb_upload(_, query: CallbackQuery):
931
  file_id, filename = entry[1], entry[2]
932
  file_obj = file_id
933
  expected_size = None
934
- # For remote files, duplicate check is not possible without downloading, skip
935
  job_id = int(time.time() * 1000 + hash(filename))
936
  active_jobs[job_id] = {"status": "queued"}
937
  await upload_queue.put((user_id, query.message.chat.id, file_obj, filename, job_id, file_id, expected_size))
@@ -1140,7 +1142,7 @@ async def on_direct_file(_, message: Message):
1140
  album_groups[group_id]['timer'] = asyncio.create_task(timer_callback())
1141
  return
1142
 
1143
- # Single file – check duplicate
1144
  existing = db_file_exists(message.from_user.id, filename)
1145
  if existing:
1146
  token = make_token()
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Telegram ↔ Hugging Face Dataset Storage Bot
4
+ -------------------------------------------
5
+ - AES‑SIV deterministic encryption for per‑user folders and filenames.
6
+ - Full support for all media types, albums, message links, instant uploads, CRUD.
7
+ - Real‑time progress updates for Telegram transfers (shows exact size, percentage, speed, ETA).
8
+ - Duplicate detection with skip / keep duplicate options.
9
+ - Automatic disk space guard, keep‑alive pings, and system status reporting.
10
+ - Built on Pyrogram + tgcrypto for maximum speed.
11
+ """
12
+
13
  import os, re, sys, time, base64, hashlib, shutil, logging, secrets, sqlite3, tempfile, threading, asyncio
14
  from pathlib import Path
15
  from typing import Optional, List, Tuple, Union
 
131
  file_callback_map: dict[str, str] = {} # token → enc_path
132
  upload_prompt_map: dict[str, List[Tuple]] = {} # token → list of entries
133
  album_groups: dict[str, dict] = {}
 
134
  duplicate_prompt_map: dict[str, dict] = {} # token → {user_id, chat_id, file_msg_or_id, orig_filename, job_id, file_id, expected_size}
135
 
136
  def make_token() -> str:
 
541
  for enc_path, orig_name, _ in user_files:
542
  token = make_token()
543
  file_callback_map[token] = enc_path
 
 
544
  buttons.append([
545
+ InlineKeyboardButton(f"📥 {orig_name[:40]}", callback_data=f"get:{token}"),
546
  InlineKeyboardButton("🗑 Delete", callback_data=f"delete:{token}"),
547
  ])
 
 
548
  buttons.append([InlineKeyboardButton("⚠️ Delete All Files", callback_data=f"delete_all:{user_id}")])
549
 
550
  await message.edit_text(
551
+ "Your uploaded files (click to download, delete to remove):",
552
  reply_markup=InlineKeyboardMarkup(buttons),
553
  )
554
 
 
641
  text = (
642
  "**Available Commands:**\n\n"
643
  "/start – Welcome message\n"
644
+ "/files – List your uploaded files (download/delete buttons)\n"
645
  "/status – System & dataset space\n"
646
  "/delete_all – Delete all your files (confirmation)\n"
647
  "/upload <link> – Upload a file from a Telegram message link instantly\n"
648
  "/help – This help\n\n"
649
  "**Features:**\n"
650
  "• Duplicate detection – you'll be asked to skip or keep a duplicate.\n"
 
651
  "• File splitting for large files (>1.9 GB).\n"
652
  "• Real‑time progress with speed and ETA."
653
  )
 
707
  for enc_path, orig_name, _ in user_files:
708
  token = make_token()
709
  file_callback_map[token] = enc_path
 
710
  buttons.append([
711
+ InlineKeyboardButton(f"📥 {orig_name[:40]}", callback_data=f"get:{token}"),
712
  InlineKeyboardButton("🗑 Delete", callback_data=f"delete:{token}"),
713
  ])
 
714
  buttons.append([InlineKeyboardButton("⚠️ Delete All Files", callback_data=f"delete_all:{user_id}")])
715
  await message.reply_text(
716
+ "Your uploaded files:",
717
  reply_markup=InlineKeyboardMarkup(buttons),
718
  disable_web_page_preview=True,
719
  )
 
780
  file_id = msg.voice.file_id
781
  else:
782
  filename = f"file_{msg_id}"
783
+ # Duplicate check
784
  existing = db_file_exists(message.from_user.id, filename)
785
  if existing:
786
  token = make_token()
 
828
  # Append a suffix like "_1" to make it unique
829
  base, ext = os.path.splitext(data['orig_filename'])
830
  new_filename = f"{base}_1{ext}"
 
831
  job_id = data['job_id']
832
  active_jobs[job_id] = {"status": "queued"}
833
  await upload_queue.put((
834
  data['user_id'],
835
  data['chat_id'],
836
  data['file_obj'],
837
+ new_filename,
838
  job_id,
839
  data['file_id'],
840
  data['expected_size'],
 
934
  file_id, filename = entry[1], entry[2]
935
  file_obj = file_id
936
  expected_size = None
 
937
  job_id = int(time.time() * 1000 + hash(filename))
938
  active_jobs[job_id] = {"status": "queued"}
939
  await upload_queue.put((user_id, query.message.chat.id, file_obj, filename, job_id, file_id, expected_size))
 
1142
  album_groups[group_id]['timer'] = asyncio.create_task(timer_callback())
1143
  return
1144
 
1145
+ # Single file – duplicate check
1146
  existing = db_file_exists(message.from_user.id, filename)
1147
  if existing:
1148
  token = make_token()