From d87da6822de243947be5cc515b79387e3c131ba2 Mon Sep 17 00:00:00 2001 From: LittleCoder Date: Fri, 17 Mar 2017 10:33:41 +0800 Subject: [PATCH] Fix image storage enhancement [ER#160: 6bigfire] --- itchat/components/contact.py | 7 +++++-- itchat/components/messages.py | 25 +++++++++++++++++-------- itchat/config.py | 2 +- itchat/utils.py | 13 ++++++++++++- 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/itchat/components/contact.py b/itchat/components/contact.py index 53bcd38..35793d6 100644 --- a/itchat/components/contact.py +++ b/itchat/components/contact.py @@ -413,10 +413,13 @@ def get_head_img(self, userName=None, chatroomUserName=None, picDir=None): tempStorage.write(block) if picDir is None: return tempStorage.getvalue() - with open(picDir, 'wb') as f: f.write(tempStorage.getvalue()) + with open(picDir, 'wb') as f: + f.write(tempStorage.getvalue()) + tempStorage.seek(0) return ReturnValue({'BaseResponse': { 'ErrMsg': 'Successfully downloaded', - 'Ret': 0, }}) + 'Ret': 0, }, + 'PostFix': utils.get_image_postfix(tempStorage.read(20)), }) def create_chatroom(self, memberList, topic=''): url = '%s/webwxcreatechatroom?pass_ticket=%s&r=%s' % ( diff --git a/itchat/components/messages.py b/itchat/components/messages.py index 3f5652b..2c3d008 100644 --- a/itchat/components/messages.py +++ b/itchat/components/messages.py @@ -31,11 +31,15 @@ def get_download_fn(core, url, msgId): tempStorage = io.BytesIO() for block in r.iter_content(1024): tempStorage.write(block) - if downloadDir is None: return tempStorage.getvalue() - with open(downloadDir, 'wb') as f: f.write(tempStorage.getvalue()) + if downloadDir is None: + return tempStorage.getvalue() + with open(downloadDir, 'wb') as f: + f.write(tempStorage.getvalue()) + tempStorage.seek(0) return ReturnValue({'BaseResponse': { 'ErrMsg': 'Successfully downloaded', - 'Ret': 0, }}) + 'Ret': 0, }, + 'PostFix': utils.get_image_postfix(tempStorage.read(20)), }) return download_fn def produce_msg(core, msgList): @@ -101,8 +105,10 @@ def produce_msg(core, msgList): tempStorage = io.BytesIO() for block in r.iter_content(1024): tempStorage.write(block) - if videoDir is None: return tempStorage.getvalue() - with open(videoDir, 'wb') as f: f.write(tempStorage.getvalue()) + if videoDir is None: + return tempStorage.getvalue() + with open(videoDir, 'wb') as f: + f.write(tempStorage.getvalue()) return ReturnValue({'BaseResponse': { 'ErrMsg': 'Successfully downloaded', 'Ret': 0, }}) @@ -128,8 +134,10 @@ def produce_msg(core, msgList): tempStorage = io.BytesIO() for block in r.iter_content(1024): tempStorage.write(block) - if attaDir is None: return tempStorage.getvalue() - with open(attaDir, 'wb') as f: f.write(tempStorage.getvalue()) + if attaDir is None: + return tempStorage.getvalue() + with open(attaDir, 'wb') as f: + f.write(tempStorage.getvalue()) return ReturnValue({'BaseResponse': { 'ErrMsg': 'Successfully downloaded', 'Ret': 0, }}) @@ -255,7 +263,8 @@ def upload_file(self, fileDir, isPicture=False, isVideo=False, 'Ret': -1002, }}) fileSize = os.path.getsize(fileDir) fileSymbol = 'pic' if isPicture else 'video' if isVideo else'doc' - with open(fileDir, 'rb') as f: fileMd5 = hashlib.md5(f.read()).hexdigest() + with open(fileDir, 'rb') as f: + fileMd5 = hashlib.md5(f.read()).hexdigest() file_ = open(fileDir, 'rb') chunks = int((fileSize - 1) / 524288) + 1 clientMediaId = int(time.time() * 1e4) diff --git a/itchat/config.py b/itchat/config.py index 796de48..6a987a0 100644 --- a/itchat/config.py +++ b/itchat/config.py @@ -1,6 +1,6 @@ import os, platform -VERSION = '1.2.30' +VERSION = '1.2.31' BASE_URL = 'https://login.weixin.qq.com' OS = platform.system() #Windows, Linux, Darwin DIR = os.getcwd() diff --git a/itchat/utils.py b/itchat/utils.py index 7637948..f5cc7a8 100644 --- a/itchat/utils.py +++ b/itchat/utils.py @@ -70,7 +70,8 @@ def msg_formatter(d, k): def check_file(fileDir): try: - with open(fileDir): pass + with open(fileDir): + pass return True except: return False @@ -130,3 +131,13 @@ def test_connect(retryTime=5): def contact_deep_copy(core, contact): with core.storageClass.updateLock: return copy.deepcopy(contact) + +def get_image_postfix(data): + data = data[:20] + if 'GIF' in data: + return 'gif' + elif 'PNG' in data: + return 'png' + elif 'JFIF' in data: + return 'jpg' + return '' -- GitLab