2016-03-16 16:54:46 +00:00
|
|
|
# TODO: add support of file transfers
|
|
|
|
# TODO: add support of avatars
|
2016-03-16 20:11:18 +00:00
|
|
|
from toxcore_enums_and_consts import TOX_FILE_KIND
|
|
|
|
from os.path import basename, getsize
|
|
|
|
|
|
|
|
|
|
|
|
class FileTransfer(object):
|
|
|
|
def __init__(self, path, tox, friend_number):
|
|
|
|
self._path = path
|
|
|
|
self._tox = tox
|
|
|
|
self._friend_number = friend_number
|
|
|
|
|
|
|
|
def set_tox(self, tox):
|
|
|
|
self._tox = tox
|
|
|
|
|
|
|
|
|
|
|
|
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))
|
2016-03-16 21:07:09 +00:00
|
|
|
self._file = open(path, 'rb')
|
2016-03-16 20:11:18 +00:00
|
|
|
|
|
|
|
def get_file_number(self):
|
|
|
|
return self._file_number
|
|
|
|
|
|
|
|
def send_chunk(self, position, size):
|
2016-03-16 21:07:09 +00:00
|
|
|
self._file.seek(position)
|
|
|
|
data = self._file.read(size)
|
2016-03-16 20:11:18 +00:00
|
|
|
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)
|
2016-03-16 21:07:09 +00:00
|
|
|
self._file = open(self._path, 'wb')
|
2016-03-16 20:11:18 +00:00
|
|
|
|
|
|
|
def write_chunk(self, position, data):
|
|
|
|
size = getsize(self._path)
|
|
|
|
if size < position + len(data):
|
2016-03-16 21:07:09 +00:00
|
|
|
self._file.write('\0' * (position + len(data) - size))
|
|
|
|
self._file.seek(position)
|
|
|
|
self._file.write(data)
|
|
|
|
self._file.flush()
|
2016-03-16 20:11:18 +00:00
|
|
|
else:
|
2016-03-16 21:07:09 +00:00
|
|
|
self._file.seek(position)
|
|
|
|
self._file.write(data)
|
|
|
|
self._file.flush()
|