toxygen/toxygen/file_transfers/file_transfers.py

349 lines
11 KiB
Python
Raw Normal View History

from wrapper.toxcore_enums_and_consts import TOX_FILE_KIND, TOX_FILE_CONTROL
2016-06-11 21:40:58 +00:00
from os.path import basename, getsize, exists, dirname
from os import remove, rename, chdir
2016-04-20 09:32:28 +00:00
from time import time, sleep
from wrapper.tox import Tox
from user_data import settings
from PyQt5 import QtCore
2016-03-16 20:11:18 +00:00
2016-03-17 17:46:18 +00:00
TOX_FILE_TRANSFER_STATE = {
'RUNNING': 0,
'PAUSED_BY_USER': 1,
'CANCELLED': 2,
2016-03-17 17:46:18 +00:00
'FINISHED': 3,
'PAUSED_BY_FRIEND': 4,
'INCOMING_NOT_STARTED': 5,
'OUTGOING_NOT_STARTED': 6
2016-03-17 17:46:18 +00:00
}
ACTIVE_FILE_TRANSFERS = (0, 1, 4, 5, 6)
PAUSED_FILE_TRANSFERS = (1, 4, 5, 6)
DO_NOT_SHOW_ACCEPT_BUTTON = (2, 3, 4, 6)
SHOW_PROGRESS_BAR = (0, 1, 4)
ALLOWED_FILES = ('toxygen_inline.png', 'utox-inline.png', 'sticker.png')
2016-03-17 17:46:18 +00:00
2016-06-08 19:53:41 +00:00
2016-12-24 19:05:29 +00:00
def is_inline(file_name):
return file_name in ALLOWED_FILES or file_name.startswith('qTox_Screenshot_')
2016-03-21 17:19:13 +00:00
class StateSignal(QtCore.QObject):
2016-07-26 16:09:57 +00:00
signal = QtCore.pyqtSignal(int, float, int) # state, progress, time in sec
2016-08-03 20:32:11 +00:00
class TransferFinishedSignal(QtCore.QObject):
signal = QtCore.pyqtSignal(int, int) # friend number, file number
2016-03-19 11:41:01 +00:00
class FileTransfer(QtCore.QObject):
2016-04-14 12:01:59 +00:00
"""
Superclass for file transfers
"""
2016-03-19 11:41:01 +00:00
2016-03-21 17:19:13 +00:00
def __init__(self, path, tox, friend_number, size, file_number=None):
super().__init__(self)
2016-03-16 20:11:18 +00:00
self._path = path
self._tox = tox
self._friend_number = friend_number
2016-03-17 17:46:18 +00:00
self.state = TOX_FILE_TRANSFER_STATE['RUNNING']
self._file_number = file_number
self._creation_time = None
2016-03-21 18:53:02 +00:00
self._size = float(size)
2016-03-21 17:19:13 +00:00
self._done = 0
self._state_changed = StateSignal()
2016-08-03 20:32:11 +00:00
self._finished = TransferFinishedSignal()
2016-07-30 18:43:28 +00:00
self._file_id = None
2016-03-16 20:11:18 +00:00
def set_tox(self, tox):
self._tox = tox
2016-03-21 17:19:13 +00:00
def set_state_changed_handler(self, handler):
self._state_changed.signal.connect(handler)
2016-03-19 11:41:01 +00:00
2016-08-03 20:32:11 +00:00
def set_transfer_finished_handler(self, handler):
self._finished.signal.connect(handler)
def signal(self):
percentage = self._done / self._size if self._size else 0
if self._creation_time is None or not percentage:
t = -1
else:
t = ((time() - self._creation_time) / percentage) * (1 - percentage)
self._state_changed.signal.emit(self.state, percentage, int(t))
2016-09-14 21:21:06 +00:00
def finished(self):
self._finished.signal.emit(self._friend_number, self._file_number)
2016-08-03 20:32:11 +00:00
2016-03-17 17:46:18 +00:00
def get_file_number(self):
return self._file_number
def get_friend_number(self):
return self._friend_number
2016-07-30 18:43:28 +00:00
def get_id(self):
return self._file_id
def get_path(self):
return self._path
def cancel(self):
self.send_control(TOX_FILE_CONTROL['CANCEL'])
2016-03-23 15:22:32 +00:00
if hasattr(self, '_file'):
self._file.close()
self.signal()
def cancelled(self):
2016-03-23 15:22:32 +00:00
if hasattr(self, '_file'):
2016-04-20 09:32:28 +00:00
sleep(0.1)
2016-03-23 15:22:32 +00:00
self._file.close()
self.state = TOX_FILE_TRANSFER_STATE['CANCELLED']
self.signal()
def pause(self, by_friend):
if not by_friend:
self.send_control(TOX_FILE_CONTROL['PAUSE'])
2016-05-09 15:32:29 +00:00
else:
self.state = TOX_FILE_TRANSFER_STATE['PAUSED_BY_FRIEND']
self.signal()
2016-03-17 17:46:18 +00:00
def send_control(self, control):
if self._tox.file_control(self._friend_number, self._file_number, control):
self.state = control
self.signal()
2016-03-17 17:46:18 +00:00
def get_file_id(self):
return self._tox.file_get_file_id(self._friend_number, self._file_number)
# -----------------------------------------------------------------------------------------------------------------
# Send file
# -----------------------------------------------------------------------------------------------------------------
2016-03-16 20:11:18 +00:00
class SendTransfer(FileTransfer):
2016-03-18 13:20:07 +00:00
def __init__(self, path, tox, friend_number, kind=TOX_FILE_KIND['DATA'], file_id=None):
if path is not None:
self._file = open(path, 'rb')
2016-03-21 17:19:13 +00:00
size = getsize(path)
else:
size = 0
super().__init__(path, tox, friend_number, size)
self.state = TOX_FILE_TRANSFER_STATE['OUTGOING_NOT_STARTED']
2016-03-21 17:19:13 +00:00
self._file_number = tox.file_send(friend_number, kind, size, file_id,
bytes(basename(path), 'utf-8') if path else b'')
2016-07-30 18:43:28 +00:00
self._file_id = self.get_file_id()
2016-03-16 20:11:18 +00:00
def send_chunk(self, position, size):
2016-04-14 12:01:59 +00:00
"""
Send chunk
:param position: start position in file
:param size: chunk max size
"""
if self._creation_time is None:
self._creation_time = time()
2016-03-18 13:50:32 +00:00
if size:
self._file.seek(position)
data = self._file.read(size)
2016-03-21 20:51:29 +00:00
self._tox.file_send_chunk(self._friend_number, self._file_number, position, data)
2016-03-21 17:19:13 +00:00
self._done += size
2016-03-18 13:50:32 +00:00
else:
2016-05-02 18:54:32 +00:00
if hasattr(self, '_file'):
self._file.close()
2016-03-18 13:50:32 +00:00
self.state = TOX_FILE_TRANSFER_STATE['FINISHED']
2016-08-03 20:32:11 +00:00
self.finished()
self.signal()
2016-03-16 20:11:18 +00:00
2016-03-18 13:20:07 +00:00
class SendAvatar(SendTransfer):
2016-04-14 12:01:59 +00:00
"""
Send avatar to friend. Doesn't need file transfer item
"""
2016-03-18 13:20:07 +00:00
def __init__(self, path, tox, friend_number):
if path is None:
avatar_hash = None
2016-03-18 13:20:07 +00:00
else:
with open(path, 'rb') as fl:
avatar_hash = Tox.hash(fl.read())
super(SendAvatar, self).__init__(path, tox, friend_number, TOX_FILE_KIND['AVATAR'], avatar_hash)
2016-03-18 13:20:07 +00:00
class SendFromBuffer(FileTransfer):
2016-04-14 12:01:59 +00:00
"""
Send inline image
"""
def __init__(self, tox, friend_number, data, file_name):
super().__init__(None, tox, friend_number, len(data))
self.state = TOX_FILE_TRANSFER_STATE['OUTGOING_NOT_STARTED']
self._data = data
2016-06-22 08:48:00 +00:00
self._file_number = tox.file_send(friend_number, TOX_FILE_KIND['DATA'],
len(data), None, bytes(file_name, 'utf-8'))
2016-04-13 21:46:28 +00:00
def get_data(self):
return self._data
def send_chunk(self, position, size):
if self._creation_time is None:
self._creation_time = time()
if size:
data = self._data[position:position + size]
self._tox.file_send_chunk(self._friend_number, self._file_number, position, data)
self._done += size
else:
self.state = TOX_FILE_TRANSFER_STATE['FINISHED']
2016-08-03 20:32:11 +00:00
self.finished()
self.signal()
2016-06-11 21:40:58 +00:00
class SendFromFileBuffer(SendTransfer):
def __init__(self, *args):
super().__init__(*args)
2016-06-11 21:40:58 +00:00
def send_chunk(self, position, size):
super().send_chunk(position, size)
2016-06-11 21:40:58 +00:00
if not size:
chdir(dirname(self._path))
remove(self._path)
# -----------------------------------------------------------------------------------------------------------------
# Receive file
# -----------------------------------------------------------------------------------------------------------------
2016-03-16 20:11:18 +00:00
class ReceiveTransfer(FileTransfer):
2016-07-30 18:43:28 +00:00
def __init__(self, path, tox, friend_number, size, file_number, position=0):
super().__init__(path, tox, friend_number, size, file_number)
2016-08-04 09:56:47 +00:00
self._file = open(self._path, 'wb')
2016-07-30 18:43:28 +00:00
self._file_size = position
self._file.truncate(position)
self._missed = set()
self._file_id = self.get_file_id()
2016-07-30 19:51:25 +00:00
self._done = position
2016-03-16 20:11:18 +00:00
def cancel(self):
super().cancel()
remove(self._path)
2016-07-30 18:43:28 +00:00
def total_size(self):
self._missed.add(self._file_size)
2016-07-30 18:43:28 +00:00
return min(self._missed)
2016-03-16 20:11:18 +00:00
def write_chunk(self, position, data):
2016-04-14 12:01:59 +00:00
"""
Incoming chunk
:param position: position in file to save data
:param data: raw data (string)
"""
if self._creation_time is None:
self._creation_time = time()
2016-03-21 17:19:13 +00:00
if data is None:
self._file.close()
self.state = TOX_FILE_TRANSFER_STATE['FINISHED']
2016-08-03 20:32:11 +00:00
self.finished()
2016-06-17 19:35:05 +00:00
else:
data = bytearray(data)
2016-03-21 17:19:13 +00:00
if self._file_size < position:
2016-03-17 15:55:18 +00:00
self._file.seek(0, 2)
2016-06-22 13:09:44 +00:00
self._file.write(b'\0' * (position - self._file_size))
2016-07-30 18:43:28 +00:00
self._missed.add(self._file_size)
else:
self._missed.discard(position)
2016-03-16 21:07:09 +00:00
self._file.seek(position)
2016-03-17 17:46:18 +00:00
self._file.write(data)
2016-03-21 18:53:02 +00:00
l = len(data)
if position + l > self._file_size:
self._file_size = position + l
self._done += l
self.signal()
2016-03-18 13:20:07 +00:00
2016-04-12 13:11:10 +00:00
class ReceiveToBuffer(FileTransfer):
2016-04-14 12:01:59 +00:00
"""
Inline image - save in buffer not in file system
"""
2016-04-12 13:11:10 +00:00
def __init__(self, tox, friend_number, size, file_number):
super().__init__(None, tox, friend_number, size, file_number)
2016-06-22 08:48:00 +00:00
self._data = bytes()
2016-04-12 13:11:10 +00:00
self._data_size = 0
def get_data(self):
return self._data
def write_chunk(self, position, data):
if self._creation_time is None:
self._creation_time = time()
2016-04-12 13:11:10 +00:00
if data is None:
self.state = TOX_FILE_TRANSFER_STATE['FINISHED']
2016-08-03 20:32:11 +00:00
self.finished()
2016-04-12 13:11:10 +00:00
else:
2016-06-22 08:48:00 +00:00
data = bytes(data)
2016-04-12 13:11:10 +00:00
l = len(data)
if self._data_size < position:
2016-06-22 08:48:00 +00:00
self._data += (b'\0' * (position - self._data_size))
2016-04-12 13:11:10 +00:00
self._data = self._data[:position] + data + self._data[position + l:]
if position + l > self._data_size:
self._data_size = position + l
self._done += l
2016-06-22 08:48:00 +00:00
self.signal()
2016-04-12 13:11:10 +00:00
2016-03-18 13:20:07 +00:00
class ReceiveAvatar(ReceiveTransfer):
2016-04-14 12:01:59 +00:00
"""
Get friend's avatar. Doesn't need file transfer item
"""
2016-04-02 11:41:06 +00:00
MAX_AVATAR_SIZE = 512 * 1024
2016-03-21 17:19:13 +00:00
def __init__(self, tox, friend_number, size, file_number):
path = settings.ProfileManager.get_path() + 'avatars/{}.png'.format(tox.friend_get_public_key(friend_number))
super().__init__(path + '.tmp', tox, friend_number, size, file_number)
2016-04-02 11:41:06 +00:00
if size > self.MAX_AVATAR_SIZE:
self.send_control(TOX_FILE_CONTROL['CANCEL'])
2016-06-22 13:09:44 +00:00
self._file.close()
2016-04-28 19:57:32 +00:00
remove(path + '.tmp')
2016-05-25 13:05:16 +00:00
elif not size:
self.send_control(TOX_FILE_CONTROL['CANCEL'])
self._file.close()
if exists(path):
remove(path)
2016-06-22 13:09:44 +00:00
self._file.close()
2016-05-25 13:05:16 +00:00
remove(path + '.tmp')
2016-04-02 11:41:06 +00:00
elif exists(path):
2016-05-25 13:05:16 +00:00
hash = self.get_file_id()
2016-06-21 11:58:11 +00:00
with open(path, 'rb') as fl:
2016-05-25 13:05:16 +00:00
data = fl.read()
existing_hash = Tox.hash(data)
if hash == existing_hash:
2016-03-18 13:20:07 +00:00
self.send_control(TOX_FILE_CONTROL['CANCEL'])
2016-06-22 13:09:44 +00:00
self._file.close()
2016-04-28 19:57:32 +00:00
remove(path + '.tmp')
2016-03-18 13:20:07 +00:00
else:
2016-05-25 13:05:16 +00:00
self.send_control(TOX_FILE_CONTROL['RESUME'])
2016-03-18 13:20:07 +00:00
else:
self.send_control(TOX_FILE_CONTROL['RESUME'])
2016-04-28 19:57:32 +00:00
def write_chunk(self, position, data):
super().write_chunk(position, data)
2016-04-28 19:57:32 +00:00
if self.state:
avatar_path = self._path[:-4]
if exists(avatar_path):
2016-06-11 21:40:58 +00:00
chdir(dirname(avatar_path))
2016-04-28 19:57:32 +00:00
remove(avatar_path)
rename(self._path, avatar_path)
2016-08-28 12:32:02 +00:00
self.finished(True)
2016-09-14 21:21:06 +00:00
def finished(self, emit=False):
if emit:
super().finished()