From 6025c70f22d42e0ed6b0f49f29b3ca710a35f558 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D0=B4=D1=80=D0=B5=D0=B9=20=D0=92=D0=BB=D0=B0?= =?UTF-8?q?=D0=B4=D0=B8=D0=BC=D0=B8=D1=80=D0=BE=D0=B2=D0=B8=D1=87?= Date: Thu, 17 Mar 2016 00:07:09 +0300 Subject: [PATCH] some improvements --- src/file_transfers.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/file_transfers.py b/src/file_transfers.py index 486410e..b52795d 100644 --- a/src/file_transfers.py +++ b/src/file_transfers.py @@ -18,29 +18,30 @@ class SendTransfer(FileTransfer): def __init__(self, path, tox, friend_number): super(self.__class__, self).__init__(path, tox, friend_number) self._file_number = tox.file_send(friend_number, TOX_FILE_KIND['DATA'], getsize(path), None, basename(path)) + self._file = open(path, 'rb') def get_file_number(self): return self._file_number def send_chunk(self, position, size): - with open(self._path) as f: - f.seek(position) - data = f.read(size) + self._file.seek(position) + data = self._file.read(size) return self._tox.file_send_chunk(self._friend_number, self._file_number, position, data) class ReceiveTransfer(FileTransfer): def __init__(self, path, tox, friend_number): super(self.__class__, self).__init__(path, tox, friend_number) + self._file = open(self._path, 'wb') def write_chunk(self, position, data): size = getsize(self._path) if size < position + len(data): - with open(self._path, 'w') as f: - f.write('\0' * (position + len(data) - size)) - f.seek(position) - f.write(data) + self._file.write('\0' * (position + len(data) - size)) + self._file.seek(position) + self._file.write(data) + self._file.flush() else: - with open(self._path, 'w') as f: - f.seek(position) - f.write(data) + self._file.seek(position) + self._file.write(data) + self._file.flush()