1
0
Fork 0
mirror of https://github.com/SlavikMIPT/tgcloud.git synced 2025-02-12 11:12:09 +00:00

flush() has been added to telegram_client_x.py

trying to read 1 byte from pipe if block is already uploaded
This commit is contained in:
Вячеслав Баженов 2019-07-10 08:11:30 +03:00
parent d194c27e0f
commit 89c4132b46
2 changed files with 15 additions and 8 deletions

View file

@ -6,11 +6,11 @@ from __future__ import unicode_literals
import os import os
import time import time
from telethon.tl.types import DocumentAttributeFilename from telethon.tl.types import DocumentAttributeFilename
from secret import * from telethon.telegram_client import TelegramClient
from telegram_client_x import TelegramClientX from telegram_client_x import TelegramClientX
from secret import *
path_home = './' # os.path.abspath('.') path_home = './' # os.path.abspath('.')
client = TelegramClientX(entity, api_id, api_hash, update_workers=None, spawn_read_thread=True) client = TelegramClientX(entity, api_id, api_hash, update_workers=None, spawn_read_thread=True)
@ -64,9 +64,9 @@ def download_block(hash_uid, filename):
# if oe.errno != errno.EEXIST: # if oe.errno != errno.EEXIST:
# raise # raise
# outbuf = open(FIFO, "wb"): # outbuf = open(FIFO, "wb"):
# os.unlink(FIFO) # os.unlink(FIFO)
client.download_media(msg, file=filename, progress_callback=on_download_progress) client.download_media(msg, file=filename, progress_callback=on_download_progress)
# outbuf.flush() # outbuf.flush()
return 0 return 0
except Exception as e: except Exception as e:
return -1 return -1
@ -80,15 +80,18 @@ def upload_block(hash_uid):
os.chdir(path_home) os.chdir(path_home)
entity = client.get_entity(client.get_me()) entity = client.get_entity(client.get_me())
FIFO = f"upipe_{hash_uid}" FIFO = f"upipe_{hash_uid}"
import errno import errno
try: try:
os.mkfifo(FIFO) os.mkfifo(FIFO)
except OSError as oe: except OSError as oe:
if oe.errno != errno.EEXIST: if oe.errno != errno.EEXIST:
raise raise
messages = client.get_messages(entity, limit=1, search=hash_uid) messages = client.get_messages(entity, limit=1, search=hash_uid)
with open(FIFO, 'rb') as bytesin: with open(FIFO, 'rb') as bytesin:
if messages: if len(messages):
bytesin.read(1)
bytesin.close()
return 0 return 0
message = client.send_file(entity, message = client.send_file(entity,
file=bytesin, file=bytesin,
@ -110,8 +113,8 @@ def main(argv):
service = str(argv[1]) service = str(argv[1])
if service == 'download': if service == 'download':
uid = str(argv[2]) uid = str(argv[2])
filename = str(argv[3]) filename = str (argv[3])
download_block(hash_uid=uid, filename=filename) download_block(hash_uid=uid,filename=filename)
return 0 return 0
elif service == 'upload': elif service == 'upload':
uid = str(argv[2]) uid = str(argv[2])

View file

@ -375,6 +375,8 @@ class TelegramClientX(TelegramClient):
cdn_decrypter, result = CdnDecrypter.prepare_decrypter(client, self._get_cdn_client(result), result) cdn_decrypter, result = CdnDecrypter.prepare_decrypter(client, self._get_cdn_client(result), result)
else: else:
f.write(result.bytes) f.write(result.bytes)
if callable(getattr(f, 'flush', None)):
f.flush()
offset += part_size offset += part_size
except FileMigrateError as e: except FileMigrateError as e:
__log__.info('File lives in another DC') __log__.info('File lives in another DC')
@ -410,6 +412,8 @@ class TelegramClientX(TelegramClient):
for th in download_thread: for th in download_thread:
if th.result and th.result.bytes: if th.result and th.result.bytes:
f.write(th.result.bytes) f.write(th.result.bytes)
if callable(getattr(f, 'flush', None)):
f.flush()
if progress_callback: if progress_callback:
progress_callback(f.tell(), file_size) progress_callback(f.tell(), file_size)
else: else: