Telegram API 个文件上传中

Telegram API files uploading

Telegram documentation 说明文件 ID 如下:

The file’s binary content is then split into parts. All parts must have the same size (part_size) and the following conditions must be met:

part_size % 1024 = 0 (divisible by 1KB)

524288 % part_size = 0 (512KB must be evenly divisible by part_size)

The last part does not have to satisfy these conditions, provided its size is less than part_size. Each part should have a sequence number, file_part, with a value ranging from 0 to 2,999.

我的代码:

def check_conditions(file_name):
    b = False
    file_binary_data = open("D:\" + file_name, "br").read()
    length = len(bytearray(file_binary_data))
    print(file_name + ", size: " + str(length) + " bytes")
    for i in range(1, 3000):
        part = length // i
        if part % 1024 == 0 and 524288 % part == 0:
            print("i: " + str(i) + " | part size: " + str(part))
            b = True
    if not b:
        print("No mathces")
    print()

check_conditions("The White Stripes - Truth Doesn't Make A Noise.mp3")
check_conditions("Depeche Mode - Precious.mp3")
check_conditions("Placebo - Meds.mp3")

输出:

The White Stripes - Truth Doesn't Make A Noise.mp3, size: 7782220 bytes
No mathces

Depeche Mode - Precious.mp3, size: 10298248 bytes
i: 1257 | part size: 8192
i: 2514 | part size: 4096

Placebo - Meds.mp3, size: 11808625 bytes
No mathces

错在哪里?或者如果一切正常,不满足的文件怎么办?

你弄错了。

您只需将文件分成大小相等的部分即可。

"The White Stripes - Truth Doesn't Make A Noise.mp3" , size: 7782220 bytes

假设您使用的最大块大小为 512k(即 524288),那么您只需:

7782220 / 524288 = 14 雷姆 442188

因此你有 14 块 512k 字节和最后一块 442188 字节。

对其他文件应用相同的逻辑。