Skip to content

Commit 89f219c

Browse files
committed
Fix image storage enhancement [ER#160: 6bigfire]
1 parent dd47af0 commit 89f219c

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

itchat/components/contact.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -413,10 +413,13 @@ def get_head_img(self, userName=None, chatroomUserName=None, picDir=None):
413413
tempStorage.write(block)
414414
if picDir is None:
415415
return tempStorage.getvalue()
416-
with open(picDir, 'wb') as f: f.write(tempStorage.getvalue())
416+
with open(picDir, 'wb') as f:
417+
f.write(tempStorage.getvalue())
418+
tempStorage.seek(0)
417419
return ReturnValue({'BaseResponse': {
418420
'ErrMsg': 'Successfully downloaded',
419-
'Ret': 0, }})
421+
'Ret': 0, },
422+
'PostFix': utils.get_image_postfix(tempStorage.read(20)), })
420423

421424
def create_chatroom(self, memberList, topic=''):
422425
url = '%s/webwxcreatechatroom?pass_ticket=%s&r=%s' % (

itchat/components/messages.py

+17-8
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,15 @@ def download_fn(downloadDir=None):
3131
tempStorage = io.BytesIO()
3232
for block in r.iter_content(1024):
3333
tempStorage.write(block)
34-
if downloadDir is None: return tempStorage.getvalue()
35-
with open(downloadDir, 'wb') as f: f.write(tempStorage.getvalue())
34+
if downloadDir is None:
35+
return tempStorage.getvalue()
36+
with open(downloadDir, 'wb') as f:
37+
f.write(tempStorage.getvalue())
38+
tempStorage.seek(0)
3639
return ReturnValue({'BaseResponse': {
3740
'ErrMsg': 'Successfully downloaded',
38-
'Ret': 0, }})
41+
'Ret': 0, },
42+
'PostFix': utils.get_image_postfix(tempStorage.read(20)), })
3943
return download_fn
4044

4145
def produce_msg(core, msgList):
@@ -101,8 +105,10 @@ def download_video(videoDir=None):
101105
tempStorage = io.BytesIO()
102106
for block in r.iter_content(1024):
103107
tempStorage.write(block)
104-
if videoDir is None: return tempStorage.getvalue()
105-
with open(videoDir, 'wb') as f: f.write(tempStorage.getvalue())
108+
if videoDir is None:
109+
return tempStorage.getvalue()
110+
with open(videoDir, 'wb') as f:
111+
f.write(tempStorage.getvalue())
106112
return ReturnValue({'BaseResponse': {
107113
'ErrMsg': 'Successfully downloaded',
108114
'Ret': 0, }})
@@ -128,8 +134,10 @@ def download_atta(attaDir=None):
128134
tempStorage = io.BytesIO()
129135
for block in r.iter_content(1024):
130136
tempStorage.write(block)
131-
if attaDir is None: return tempStorage.getvalue()
132-
with open(attaDir, 'wb') as f: f.write(tempStorage.getvalue())
137+
if attaDir is None:
138+
return tempStorage.getvalue()
139+
with open(attaDir, 'wb') as f:
140+
f.write(tempStorage.getvalue())
133141
return ReturnValue({'BaseResponse': {
134142
'ErrMsg': 'Successfully downloaded',
135143
'Ret': 0, }})
@@ -255,7 +263,8 @@ def upload_file(self, fileDir, isPicture=False, isVideo=False,
255263
'Ret': -1002, }})
256264
fileSize = os.path.getsize(fileDir)
257265
fileSymbol = 'pic' if isPicture else 'video' if isVideo else'doc'
258-
with open(fileDir, 'rb') as f: fileMd5 = hashlib.md5(f.read()).hexdigest()
266+
with open(fileDir, 'rb') as f:
267+
fileMd5 = hashlib.md5(f.read()).hexdigest()
259268
file_ = open(fileDir, 'rb')
260269
chunks = int((fileSize - 1) / 524288) + 1
261270
clientMediaId = int(time.time() * 1e4)

itchat/utils.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ def msg_formatter(d, k):
7070

7171
def check_file(fileDir):
7272
try:
73-
with open(fileDir): pass
73+
with open(fileDir):
74+
pass
7475
return True
7576
except:
7677
return False
@@ -130,3 +131,13 @@ def test_connect(retryTime=5):
130131
def contact_deep_copy(core, contact):
131132
with core.storageClass.updateLock:
132133
return copy.deepcopy(contact)
134+
135+
def get_image_postfix(data):
136+
data = data[:20]
137+
if 'GIF' in data:
138+
return 'gif'
139+
elif 'PNG' in data:
140+
return 'png'
141+
elif 'JFIF' in data:
142+
return 'jpg'
143+
return ''

0 commit comments

Comments
 (0)