File size: 2,294 Bytes
dd0bf70
 
822206f
8b2f555
822206f
dd0bf70
 
 
822206f
 
8b2f555
 
 
 
 
 
 
 
 
 
 
 
 
 
dd0bf70
 
822206f
 
 
 
 
 
 
 
8b2f555
 
 
 
 
 
 
 
822206f
8b2f555
 
822206f
8b2f555
 
 
 
822206f
8b2f555
822206f
dd0bf70
8b2f555
 
 
 
 
822206f
dd0bf70
8b2f555
 
 
 
 
 
 
 
 
 
 
 
 
822206f
dd0bf70
8b2f555
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# PATH: bot/telegram/media.py
import os
import tempfile
from typing import Callable, Awaitable, Optional, Tuple

from hydrogram import Client
from hydrogram.types import Message

ProgressCB = Callable[[int, int], Awaitable[None]]


async def download_to_temp(
    app: Client,
    m: Message,
    progress_cb: Optional[ProgressCB] = None,
) -> Tuple[str, int, str]:
    """
    Download telegram video/document to a temp path.
    IMPORTANT:
      - mkstemp creates an EMPTY FILE
      - some libs will NOT overwrite it and will save to a different path
      - so we delete the empty file first, and we MUST capture the returned path
    Returns: (path, size, original_file_name)
    """
    media = m.video or m.document
    if not media:
        return "", 0, ""

    file_name = getattr(media, "file_name", None) or "video.mp4"
    suffix = "." + file_name.rsplit(".", 1)[1] if "." in file_name else ""

    fd, path = tempfile.mkstemp(prefix="studytube_", suffix=suffix)
    os.close(fd)

    # ✅ Critical: remove the empty file so download_media can safely create it
    try:
        if os.path.exists(path):
            os.remove(path)
    except Exception:
        pass

    saved_path = ""
    try:
        # ✅ Critical: capture returned path (some libs save elsewhere)
        saved_path = await app.download_media(message=m, file_name=path, progress=progress_cb)
    except TypeError:
        # older signature
        saved_path = await app.download_media(message=m, file_name=path)

    real_path = saved_path if isinstance(saved_path, str) and saved_path else path

    if not os.path.exists(real_path):
        return "", 0, file_name

    try:
        size = int(os.path.getsize(real_path))
    except Exception:
        size = 0

    expected = int(getattr(media, "file_size", 0) or 0)

    # If telegram didn't provide expected size, still reject 0-byte downloads
    if size <= 0:
        try:
            os.remove(real_path)
        except Exception:
            pass
        raise RuntimeError("download_zero_bytes")

    if expected and expected != size:
        try:
            os.remove(real_path)
        except Exception:
            pass
        raise RuntimeError(f"download_size_mismatch expected={expected} got={size}")

    return real_path, size, file_name