understanding commited on
Commit
8b2f555
·
verified ·
1 Parent(s): 00608ed

Update bot/telegram/media.py

Browse files
Files changed (1) hide show
  1. bot/telegram/media.py +49 -10
bot/telegram/media.py CHANGED
@@ -1,14 +1,27 @@
1
  # PATH: bot/telegram/media.py
2
  import os
3
  import tempfile
4
- from typing import Callable, Awaitable, Optional
5
 
6
  from hydrogram import Client
7
  from hydrogram.types import Message
8
 
9
  ProgressCB = Callable[[int, int], Awaitable[None]]
10
 
11
- async def download_to_temp(app: Client, m: Message, progress_cb: Optional[ProgressCB] = None):
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  media = m.video or m.document
13
  if not media:
14
  return "", 0, ""
@@ -19,20 +32,46 @@ async def download_to_temp(app: Client, m: Message, progress_cb: Optional[Progre
19
  fd, path = tempfile.mkstemp(prefix="studytube_", suffix=suffix)
20
  os.close(fd)
21
 
 
 
 
 
 
 
 
 
22
  try:
23
- await app.download_media(message=m, file_name=path, progress=progress_cb)
 
24
  except TypeError:
25
- await app.download_media(message=m, file_name=path)
 
 
 
26
 
27
- if not os.path.exists(path):
28
  return "", 0, file_name
29
 
30
- size = os.path.getsize(path)
 
 
 
 
31
  expected = int(getattr(media, "file_size", 0) or 0)
32
 
33
- if expected and size and expected != size:
34
- try: os.remove(path)
35
- except: pass
 
 
 
 
 
 
 
 
 
 
36
  raise RuntimeError(f"download_size_mismatch expected={expected} got={size}")
37
 
38
- return path, size, file_name
 
1
  # PATH: bot/telegram/media.py
2
  import os
3
  import tempfile
4
+ from typing import Callable, Awaitable, Optional, Tuple
5
 
6
  from hydrogram import Client
7
  from hydrogram.types import Message
8
 
9
  ProgressCB = Callable[[int, int], Awaitable[None]]
10
 
11
+
12
+ async def download_to_temp(
13
+ app: Client,
14
+ m: Message,
15
+ progress_cb: Optional[ProgressCB] = None,
16
+ ) -> Tuple[str, int, str]:
17
+ """
18
+ Download telegram video/document to a temp path.
19
+ IMPORTANT:
20
+ - mkstemp creates an EMPTY FILE
21
+ - some libs will NOT overwrite it and will save to a different path
22
+ - so we delete the empty file first, and we MUST capture the returned path
23
+ Returns: (path, size, original_file_name)
24
+ """
25
  media = m.video or m.document
26
  if not media:
27
  return "", 0, ""
 
32
  fd, path = tempfile.mkstemp(prefix="studytube_", suffix=suffix)
33
  os.close(fd)
34
 
35
+ # ✅ Critical: remove the empty file so download_media can safely create it
36
+ try:
37
+ if os.path.exists(path):
38
+ os.remove(path)
39
+ except Exception:
40
+ pass
41
+
42
+ saved_path = ""
43
  try:
44
+ # Critical: capture returned path (some libs save elsewhere)
45
+ saved_path = await app.download_media(message=m, file_name=path, progress=progress_cb)
46
  except TypeError:
47
+ # older signature
48
+ saved_path = await app.download_media(message=m, file_name=path)
49
+
50
+ real_path = saved_path if isinstance(saved_path, str) and saved_path else path
51
 
52
+ if not os.path.exists(real_path):
53
  return "", 0, file_name
54
 
55
+ try:
56
+ size = int(os.path.getsize(real_path))
57
+ except Exception:
58
+ size = 0
59
+
60
  expected = int(getattr(media, "file_size", 0) or 0)
61
 
62
+ # If telegram didn't provide expected size, still reject 0-byte downloads
63
+ if size <= 0:
64
+ try:
65
+ os.remove(real_path)
66
+ except Exception:
67
+ pass
68
+ raise RuntimeError("download_zero_bytes")
69
+
70
+ if expected and expected != size:
71
+ try:
72
+ os.remove(real_path)
73
+ except Exception:
74
+ pass
75
  raise RuntimeError(f"download_size_mismatch expected={expected} got={size}")
76
 
77
+ return real_path, size, file_name