avatar cancelling fixed

This commit is contained in:
ingvar1995 2016-04-28 22:08:47 +03:00
parent 4cda4ed4a1
commit 12d2ee52dc
2 changed files with 10 additions and 7 deletions

View file

@ -1,6 +1,6 @@
from toxcore_enums_and_consts import TOX_FILE_KIND, TOX_FILE_CONTROL
from os.path import basename, getsize, exists
from os import remove
from os import remove, chdir
from time import time, sleep
from tox import Tox
import settings
@ -149,8 +149,8 @@ class ReceiveTransfer(FileTransfer):
def __init__(self, path, tox, friend_number, size, file_number):
super(ReceiveTransfer, self).__init__(path, tox, friend_number, size, file_number)
self._file = open(self._path, 'wb')
if type(self) is not ReceiveAvatar:
self._file = open(self._path, 'wb')
self._file.truncate(0)
self._file_size = 0
@ -219,7 +219,7 @@ class ReceiveAvatar(ReceiveTransfer):
MAX_AVATAR_SIZE = 512 * 1024
def __init__(self, tox, friend_number, size, file_number):
path = settings.ProfileHelper.get_path() + '/avatars/{}.png'.format(tox.friend_get_public_key(friend_number))
path = settings.ProfileHelper.get_path() + 'avatars/{}.png'.format(tox.friend_get_public_key(friend_number))
super(ReceiveAvatar, self).__init__(path, tox, friend_number, size, file_number)
if size > self.MAX_AVATAR_SIZE:
self.send_control(TOX_FILE_CONTROL['CANCEL'])
@ -231,14 +231,17 @@ class ReceiveAvatar(ReceiveTransfer):
remove(path)
else:
hash = self.get_file_id()
with open(path, 'rb') as fl:
existing_hash = Tox.hash(fl.read())
with open(path) as fl:
data = fl.read()
existing_hash = Tox.hash(data)
if hash == existing_hash:
self.send_control(TOX_FILE_CONTROL['CANCEL'])
self.state = TOX_FILE_TRANSFER_STATE['CANCELED']
else:
self._file = open(self._path, 'wb')
self._file.truncate(0)
self.send_control(TOX_FILE_CONTROL['RESUME'])
else:
self._file = open(self._path, 'wb')
self._file.truncate(0)
self.send_control(TOX_FILE_CONTROL['RESUME'])

View file

@ -1017,7 +1017,7 @@ class Tox(object):
# -----------------------------------------------------------------------------------------------------------------
@staticmethod
def hash(data, size, hash=None):
def hash(data, hash=None):
"""
Generates a cryptographic hash of the given data.
@ -1034,7 +1034,7 @@ class Tox(object):
"""
if hash is None:
hash = create_string_buffer(TOX_HASH_LENGTH)
Tox.libtoxcore.tox_hash(hash, c_void_p(data), c_size_t(size))
Tox.libtoxcore.tox_hash(hash, c_char_p(data), len(data))
return bin_to_string(hash, TOX_HASH_LENGTH)
def file_control(self, friend_number, file_number, control):