some improvements

This commit is contained in:
Андрей Владимирович 2016-03-17 00:07:09 +03:00
parent 24109004cf
commit 6025c70f22

View file

@ -18,29 +18,30 @@ class SendTransfer(FileTransfer):
def __init__(self, path, tox, friend_number): def __init__(self, path, tox, friend_number):
super(self.__class__, self).__init__(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_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): def get_file_number(self):
return self._file_number return self._file_number
def send_chunk(self, position, size): def send_chunk(self, position, size):
with open(self._path) as f: self._file.seek(position)
f.seek(position) data = self._file.read(size)
data = f.read(size)
return self._tox.file_send_chunk(self._friend_number, self._file_number, position, data) return self._tox.file_send_chunk(self._friend_number, self._file_number, position, data)
class ReceiveTransfer(FileTransfer): class ReceiveTransfer(FileTransfer):
def __init__(self, path, tox, friend_number): def __init__(self, path, tox, friend_number):
super(self.__class__, self).__init__(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): def write_chunk(self, position, data):
size = getsize(self._path) size = getsize(self._path)
if size < position + len(data): if size < position + len(data):
with open(self._path, 'w') as f: self._file.write('\0' * (position + len(data) - size))
f.write('\0' * (position + len(data) - size)) self._file.seek(position)
f.seek(position) self._file.write(data)
f.write(data) self._file.flush()
else: else:
with open(self._path, 'w') as f: self._file.seek(position)
f.seek(position) self._file.write(data)
f.write(data) self._file.flush()