2016-03-18 14:20:07 +01:00
|
|
|
from toxcore_enums_and_consts import TOX_FILE_KIND, TOX_FILE_CONTROL
|
|
|
|
from os.path import basename, getsize, exists
|
|
|
|
from os import remove
|
2016-03-17 21:51:15 +01:00
|
|
|
from time import time
|
2016-03-18 14:20:07 +01:00
|
|
|
from tox import Tox
|
|
|
|
import profile
|
2016-03-16 21:11:18 +01:00
|
|
|
|
|
|
|
|
2016-03-17 18:46:18 +01:00
|
|
|
TOX_FILE_TRANSFER_STATE = {
|
|
|
|
'RUNNING': 0,
|
|
|
|
'PAUSED': 1,
|
|
|
|
'CANCELED': 2,
|
|
|
|
'FINISHED': 3,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-16 21:11:18 +01:00
|
|
|
class FileTransfer(object):
|
2016-03-17 21:51:15 +01:00
|
|
|
def __init__(self, path, tox, friend_number, file_number=None):
|
2016-03-16 21:11:18 +01:00
|
|
|
self._path = path
|
|
|
|
self._tox = tox
|
|
|
|
self._friend_number = friend_number
|
2016-03-17 18:46:18 +01:00
|
|
|
self.state = TOX_FILE_TRANSFER_STATE['RUNNING']
|
|
|
|
self._file_number = file_number
|
2016-03-17 21:51:15 +01:00
|
|
|
self._creation_time = time()
|
2016-03-16 21:11:18 +01:00
|
|
|
|
|
|
|
def set_tox(self, tox):
|
|
|
|
self._tox = tox
|
|
|
|
|
2016-03-17 18:46:18 +01:00
|
|
|
def get_file_number(self):
|
|
|
|
return self._file_number
|
|
|
|
|
|
|
|
def get_friend_number(self):
|
|
|
|
return self._friend_number
|
|
|
|
|
|
|
|
def send_control(self, control):
|
|
|
|
if self._tox.file_control(self._friend_number, self._file_number, control):
|
|
|
|
self.state = control
|
|
|
|
|
|
|
|
def get_file_id(self):
|
|
|
|
return self._tox.file_get_file_id(self._friend_number, self._file_number)
|
|
|
|
|
|
|
|
def file_seek(self):
|
|
|
|
# TODO implement
|
|
|
|
pass
|
|
|
|
|
2016-03-16 21:11:18 +01:00
|
|
|
|
|
|
|
class SendTransfer(FileTransfer):
|
2016-03-18 14:20:07 +01:00
|
|
|
def __init__(self, path, tox, friend_number, kind=TOX_FILE_KIND['DATA'], file_id=None):
|
|
|
|
super(SendTransfer, self).__init__(path, tox, friend_number)
|
|
|
|
self._file_number = tox.file_send(friend_number,
|
|
|
|
kind,
|
|
|
|
getsize(path) if path else 0,
|
|
|
|
file_id,
|
|
|
|
basename(path) if path else '')
|
|
|
|
if path is not None:
|
|
|
|
self._file = open(path, 'rb')
|
2016-03-16 21:11:18 +01:00
|
|
|
|
|
|
|
def send_chunk(self, position, size):
|
2016-03-18 14:50:32 +01:00
|
|
|
if size:
|
|
|
|
self._file.seek(position)
|
|
|
|
data = self._file.read(size)
|
|
|
|
return self._tox.file_send_chunk(self._friend_number, self._file_number, position, data)
|
|
|
|
else:
|
|
|
|
self._file.close()
|
|
|
|
self.state = TOX_FILE_TRANSFER_STATE['FINISHED']
|
2016-03-16 21:11:18 +01:00
|
|
|
|
|
|
|
|
2016-03-18 14:20:07 +01:00
|
|
|
class SendAvatar(SendTransfer):
|
|
|
|
def __init__(self, path, tox, friend_number):
|
|
|
|
if path is None:
|
|
|
|
super(SendAvatar, self).__init__(path, tox, friend_number, TOX_FILE_KIND['AVATAR'])
|
|
|
|
else:
|
|
|
|
with open(path, 'rb') as fl:
|
|
|
|
hash = Tox.hash(fl.read())
|
|
|
|
super(self.__class__, self).__init__(path, tox, friend_number, TOX_FILE_KIND['AVATAR'], hash)
|
|
|
|
|
|
|
|
|
2016-03-16 21:11:18 +01:00
|
|
|
class ReceiveTransfer(FileTransfer):
|
2016-03-17 18:46:18 +01:00
|
|
|
def __init__(self, path, tox, friend_number, file_number):
|
2016-03-18 14:20:07 +01:00
|
|
|
super(ReceiveTransfer, self).__init__(path, tox, friend_number, file_number)
|
2016-03-16 22:07:09 +01:00
|
|
|
self._file = open(self._path, 'wb')
|
2016-03-17 17:28:52 +01:00
|
|
|
self._file.truncate(0)
|
2016-03-17 18:46:18 +01:00
|
|
|
self._size = 0
|
2016-03-16 21:11:18 +01:00
|
|
|
|
|
|
|
def write_chunk(self, position, data):
|
2016-03-17 16:55:18 +01:00
|
|
|
if data is not None:
|
2016-03-17 18:46:18 +01:00
|
|
|
data = ''.join(chr(x) for x in data)
|
|
|
|
if self._size < position:
|
2016-03-17 16:55:18 +01:00
|
|
|
self._file.seek(0, 2)
|
2016-03-17 18:46:18 +01:00
|
|
|
self._file.write('\0' * (position - self._size))
|
2016-03-16 22:07:09 +01:00
|
|
|
self._file.seek(position)
|
2016-03-17 18:46:18 +01:00
|
|
|
self._file.write(data)
|
2016-03-16 22:07:09 +01:00
|
|
|
self._file.flush()
|
2016-03-17 18:46:18 +01:00
|
|
|
if position + len(data) > self._size:
|
|
|
|
self._size = position + len(data)
|
2016-03-16 21:11:18 +01:00
|
|
|
else:
|
2016-03-17 16:55:18 +01:00
|
|
|
self._file.close()
|
2016-03-18 14:20:07 +01:00
|
|
|
self.state = TOX_FILE_TRANSFER_STATE['FINISHED']
|
|
|
|
|
|
|
|
|
|
|
|
class ReceiveAvatar(ReceiveTransfer):
|
|
|
|
def __init__(self, tox, friend_number, file_number, has_size=True):
|
|
|
|
path = profile.ProfileHelper.get_path() + '/avatars/{}.png'.format(tox.friend_get_public_key(friend_number))
|
|
|
|
super(ReceiveAvatar, self).__init__(path, tox, friend_number, file_number)
|
|
|
|
if exists(path):
|
|
|
|
if not has_size:
|
|
|
|
remove(path)
|
|
|
|
self.send_control(TOX_FILE_CONTROL['CANCEL'])
|
|
|
|
self.state = TOX_FILE_TRANSFER_STATE['CANCELED']
|
|
|
|
else:
|
|
|
|
hash = self.get_file_id()
|
|
|
|
with open(path, 'rb') as fl:
|
|
|
|
existing_hash = Tox.hash(fl.read())
|
|
|
|
if hash == existing_hash:
|
|
|
|
self.send_control(TOX_FILE_CONTROL['CANCEL'])
|
|
|
|
self.state = TOX_FILE_TRANSFER_STATE['CANCELED']
|
|
|
|
else:
|
|
|
|
self.send_control(TOX_FILE_CONTROL['RESUME'])
|
|
|
|
else:
|
|
|
|
self.send_control(TOX_FILE_CONTROL['RESUME'])
|