重构大部分API,响应速度翻倍

This commit is contained in:
xaoyaoo 2024-04-19 12:18:38 +08:00
parent 4c5d410347
commit 4fea15930b

View File

@ -17,9 +17,11 @@ from flask import Flask, request, render_template, g, Blueprint, send_file, make
from pywxdump import analyzer, read_img_dat, read_audio, get_wechat_db, get_core_db
from pywxdump.analyzer.export_chat import get_contact, get_room_user_list
from pywxdump.api.rjson import ReJson, RqJson
from pywxdump.api.utils import read_session, save_session, error9999, gen_base64
from pywxdump.api.utils import read_session, get_session_wxids, save_session, error9999, gen_base64, validate_title
from pywxdump import read_info, VERSION_LIST, batch_decrypt, BiasAddr, merge_db, decrypt_merge, merge_real_time_db
import pywxdump
from pywxdump.dbpreprocess import wxid2userinfo, ParsingMSG, get_user_list, get_recent_user_list, ParsingMediaMSG
from pywxdump.dbpreprocess.utils import download_file
# app = Flask(__name__, static_folder='../ui/web/dist', static_url_path='/')
@ -27,93 +29,108 @@ api = Blueprint('api', __name__, template_folder='../ui/web', static_folder='../
api.debug = False
@api.route('/api/init', methods=["GET", 'POST'])
@api.route('/api/init_last', methods=["GET", 'POST'])
@error9999
def init():
def init_last():
"""
初始化 设置微信数据库路径图片路径解密需要的数据库
是否初始化
:return:
"""
msg_path = request.json.get("msg_path", "").strip().strip("'").strip('"')
micro_path = request.json.get("micro_path", "").strip().strip("'").strip('"')
media_path = request.json.get("media_path", "").strip().strip("'").strip('"')
my_wxid = read_session(g.sf, "test", "last")
if my_wxid:
merge_path = read_session(g.sf, my_wxid, "merge_path")
wx_path = read_session(g.sf, my_wxid, "wx_path")
key = read_session(g.sf, my_wxid, "key")
rdata = {
"merge_path": merge_path,
"wx_path": wx_path,
"key": key,
"my_wxid": my_wxid,
"is_init": True,
}
if merge_path and wx_path:
return ReJson(0, rdata)
return ReJson(0, {"is_init": False, "my_wxid": ""})
@api.route('/api/init_key', methods=["GET", 'POST'])
@error9999
def init_key():
"""
初始化包括key
:return:
"""
wx_path = request.json.get("wx_path", "").strip().strip("'").strip('"')
key = request.json.get("key", "").strip().strip("'").strip('"')
my_wxid = request.json.get("my_wxid", "").strip().strip("'").strip('"')
if not wx_path:
return ReJson(1002)
if not os.path.exists(wx_path):
return ReJson(1001, body=wx_path)
if not key:
return ReJson(1002)
if not my_wxid:
return ReJson(1002)
init_type = request.json.get("init_type", "").strip()
if init_type == "last":
save_msg_path = read_session(g.sf, "msg_path")
save_micro_path = read_session(g.sf, "micro_path")
save_my_wxid = read_session(g.sf, "my_wxid")
if save_msg_path and save_micro_path and os.path.exists(save_msg_path) and os.path.exists(
save_micro_path):
try:
a = get_real_time_msg()
except Exception as e:
pass
return ReJson(0, {"msg_path": save_msg_path, "micro_path": save_micro_path, "is_init": True})
else:
return ReJson(1002, body="上次初始化的路径不存在")
if key: # 如果key不为空表示是解密模式
if not wx_path:
return ReJson(1002)
if not os.path.exists(wx_path):
return ReJson(1001, body=wx_path)
out_path = os.path.join(g.tmp_path, "decrypted", my_wxid) if my_wxid else os.path.join(g.tmp_path, "decrypted")
if os.path.exists(out_path):
shutil.rmtree(out_path)
code, merge_save_path = decrypt_merge(wx_path=wx_path, key=key, outpath=out_path)
time.sleep(1)
if code:
save_session(g.sf, "msg_path", merge_save_path)
save_session(g.sf, "micro_path", merge_save_path)
save_session(g.sf, "media_path", merge_save_path)
save_session(g.sf, "wx_path", wx_path)
save_session(g.sf, "key", key)
save_session(g.sf, "my_wxid", my_wxid)
rdata = {
"msg_path": merge_save_path,
"micro_path": merge_save_path,
"media_path": merge_save_path,
"wx_path": wx_path,
"key": key,
"my_wxid": my_wxid,
"is_init": True,
}
return ReJson(0, rdata)
else:
return ReJson(2001, body=merge_save_path)
else:
if not msg_path or not micro_path:
return ReJson(1002, body="msg_path and micro_path is required")
if not os.path.exists(msg_path) or not os.path.exists(micro_path):
return ReJson(1001)
save_session(g.sf, "msg_path", msg_path)
save_session(g.sf, "micro_path", micro_path)
save_session(g.sf, "media_path", media_path)
save_session(g.sf, "wx_path", wx_path)
save_session(g.sf, "key", "")
save_session(g.sf, "my_wxid", my_wxid)
out_path = os.path.join(g.tmp_path, "decrypted", my_wxid) if my_wxid else os.path.join(g.tmp_path, "decrypted")
if os.path.exists(out_path):
shutil.rmtree(out_path)
code, merge_save_path = decrypt_merge(wx_path=wx_path, key=key, outpath=out_path)
time.sleep(1)
if code:
save_session(g.sf, my_wxid, "merge_path", merge_save_path)
save_session(g.sf, my_wxid, "wx_path", wx_path)
save_session(g.sf, my_wxid, "key", key)
save_session(g.sf, my_wxid, "my_wxid", my_wxid)
save_session(g.sf, "test", "last", my_wxid)
rdata = {
"msg_path": msg_path,
"micro_path": micro_path,
"media_path": media_path,
"merge_path": merge_save_path,
"wx_path": wx_path,
"key": "",
"key": key,
"my_wxid": my_wxid,
"is_init": True,
}
return ReJson(0, rdata)
else:
return ReJson(2001, body=merge_save_path)
@api.route('/api/init_nokey', methods=["GET", 'POST'])
@error9999
def init_nokey():
"""
初始化包括key
:return:
"""
merge_path = request.json.get("merge_path", "").strip().strip("'").strip('"')
wx_path = request.json.get("wx_path", "").strip().strip("'").strip('"')
my_wxid = request.json.get("my_wxid", "").strip().strip("'").strip('"')
if not wx_path:
return ReJson(1002)
if not os.path.exists(wx_path):
return ReJson(1001, body=wx_path)
if not merge_path:
return ReJson(1002)
if not my_wxid:
return ReJson(1002)
key = read_session(g.sf, my_wxid, "key")
save_session(g.sf, my_wxid, "merge_path", merge_path)
save_session(g.sf, my_wxid, "wx_path", wx_path)
save_session(g.sf, my_wxid, "key", key)
save_session(g.sf, my_wxid, "my_wxid", my_wxid)
save_session(g.sf, "test", "last", my_wxid)
rdata = {
"merge_path": merge_path,
"wx_path": wx_path,
"key": "",
"my_wxid": my_wxid,
"is_init": True,
}
return ReJson(0, rdata)
@api.route('/api/version', methods=["GET", 'POST'])
@ -126,87 +143,304 @@ def version():
return ReJson(0, pywxdump.__version__)
@api.route('/api/contact_list', methods=["GET", 'POST'])
# start 以下为聊天联系人相关api
@api.route('/api/recent_user_list', methods=["GET", 'POST'])
@error9999
def contact_list():
def recent_user_list():
"""
获取联系人列表
:return:
"""
# 获取联系人列表
# 从header中读取micro_path
micro_path = request.headers.get("micro_path")
if not micro_path:
micro_path = read_session(g.sf, "micro_path")
my_wxid = read_session(g.sf, "test", "last")
if not my_wxid: return ReJson(1001, body="my_wxid is required")
merge_path = read_session(g.sf, my_wxid, "merge_path")
user_list = get_recent_user_list(merge_path, merge_path, limit=200)
return ReJson(0, user_list)
@api.route('/api/user_list', methods=["GET", 'POST'])
@error9999
def user_list():
"""
获取联系人列表
:return:
"""
if request.method == "GET":
word = request.args.get("word", "")
elif request.method == "POST":
word = request.json.get("word", "")
else:
return ReJson(1003, msg="Unsupported method")
my_wxid = read_session(g.sf, "test", "last")
if not my_wxid: return ReJson(1001, body="my_wxid is required")
merge_path = read_session(g.sf, my_wxid, "merge_path")
user_list = get_user_list(merge_path, merge_path, word)
return ReJson(0, user_list)
@api.route('/api/wxid2user', methods=["GET", 'POST'])
@error9999
def wxid2user():
"""
获取联系人列表
:return:
"""
if request.method == "GET":
word = request.args.get("wxid", "")
elif request.method == "POST":
word = request.json.get("wxid", "")
else:
return ReJson(1003, msg="Unsupported method")
my_wxid = read_session(g.sf, "test", "last")
if not my_wxid: return ReJson(1001, body="my_wxid is required")
merge_path = read_session(g.sf, my_wxid, "merge_path")
user_info = wxid2userinfo(merge_path, merge_path, wxid=word)
return ReJson(0, user_info)
@api.route('/api/mywxid', methods=["GET", 'POST'])
@error9999
def mywxid():
"""
获取联系人列表
:return:
"""
my_wxid = read_session(g.sf, "test", "last")
if not my_wxid: return ReJson(1001, body="my_wxid is required")
return ReJson(0, {"my_wxid": my_wxid})
# end 以上为聊天联系人相关api
# start 以下为聊天记录相关api
@api.route('/api/realtimemsg', methods=["GET", "POST"])
@error9999
def get_real_time_msg():
"""
获取实时消息 使用 merge_real_time_db()函数
:return:
"""
my_wxid = read_session(g.sf, "test", "last")
if not my_wxid: return ReJson(1001, body="my_wxid is required")
merge_path = read_session(g.sf, my_wxid, "merge_path")
key = read_session(g.sf, my_wxid, "key")
wx_path = read_session(g.sf, my_wxid, "wx_path")
if not merge_path or not key or not wx_path or not wx_path:
return ReJson(1002, body="msg_path or media_path or wx_path or key is required")
db_paths = get_core_db(wx_path, ["MediaMSG", "MSG", "MicroMsg"])
if not db_paths[0]:
return ReJson(1001, body="media_paths or msg_paths is required")
db_paths = db_paths[1]
for i in db_paths:
merge_real_time_db(key=key, db_path=i, merge_path=merge_path)
return ReJson(0, "success")
@api.route('/api/msg_count', methods=["GET", 'POST'])
@error9999
def msg_count():
"""
获取联系人的聊天记录数量
:return:
"""
if request.method == "GET":
wxid = request.args.get("wxid")
elif request.method == "POST":
wxid = request.json.get("wxid")
else:
return ReJson(1003, msg="Unsupported method")
my_wxid = read_session(g.sf, "test", "last")
if not my_wxid: return ReJson(1001, body="my_wxid is required")
merge_path = read_session(g.sf, my_wxid, "merge_path")
chat_count = ParsingMSG(merge_path).msg_count(wxid)
return ReJson(0, chat_count)
@api.route('/api/imgsrc/<path:imgsrc>', methods=["GET", 'POST'])
def get_imgsrc(imgsrc):
"""
获取图片
:return:
"""
if not imgsrc:
return ReJson(1002)
# 将?后面的参数连接到imgsrc
imgsrc = imgsrc + "?" + request.query_string.decode("utf-8")
my_wxid = read_session(g.sf, "test", "last")
if not my_wxid: return ReJson(1001, body="my_wxid is required")
img_tmp_path = os.path.join(g.tmp_path, my_wxid, "imgsrc")
if not os.path.exists(img_tmp_path):
os.makedirs(img_tmp_path)
file_name = imgsrc.replace("http://", "").replace("https://", "").replace("/", "_").replace("?", "_")
file_name = file_name + ".jpg"
# 如果文件名过长,则将文件明分为目录和文件名
if len(file_name) > 255:
file_name = file_name[:255] + "/" + file_name[255:]
img_path_all = os.path.join(img_tmp_path, file_name)
if os.path.exists(img_path_all):
return send_file(img_path_all)
else:
download_file(imgsrc, img_path_all)
if os.path.exists(img_path_all):
return send_file(img_path_all)
else:
return ReJson(4004, body=imgsrc)
@api.route('/api/msgs', methods=["GET", 'POST'])
@error9999
def get_msgs():
my_wxid = read_session(g.sf, "test", "last")
if not my_wxid: return ReJson(1001, body="my_wxid is required")
merge_path = read_session(g.sf, my_wxid, "merge_path")
start = request.json.get("start")
limit = request.json.get("limit")
wxid = request.json.get("wxid")
contact_list = analyzer.get_contact_list(micro_path)
# save_session(g.sf, "user_list", contact_list)
if limit:
contact_list = contact_list[int(start):int(start) + int(limit)]
return ReJson(0, contact_list)
if not wxid:
return ReJson(1002, body=f"wxid is required: {wxid}")
if start and isinstance(start, str) and start.isdigit():
start = int(start)
if limit and isinstance(limit, str) and limit.isdigit():
limit = int(limit)
if start is None or limit is None:
return ReJson(1002, body=f"start or limit is required {start} {limit}")
if not isinstance(start, int) and not isinstance(limit, int):
return ReJson(1002, body=f"start or limit is not int {start} {limit}")
parsing_msg = ParsingMSG(merge_path)
msgs, wxid_list = parsing_msg.msg_list(wxid, start, limit)
wxid_list.append(my_wxid)
user_list = wxid2userinfo(merge_path, merge_path, wxid_list)
return ReJson(0, {"msg_list": msgs, "user_list": user_list})
@api.route('/api/chat_count', methods=["GET", 'POST'])
def chat_count():
@api.route('/api/img/<path:img_path>', methods=["GET", 'POST'])
@error9999
def get_img(img_path):
"""
获取每个联系人的聊天记录数量
获取图片
:return:
"""
try:
# 获取联系人列表
# 从header中读取micro_path
msg_path = request.headers.get("msg_path")
if not msg_path:
msg_path = read_session(g.sf, "msg_path")
username = request.json.get("username", "")
contact_list = analyzer.get_chat_count(msg_path, username)
return ReJson(0, contact_list)
except Exception as e:
return ReJson(9999, msg=str(e))
if not img_path:
return ReJson(1002)
my_wxid = read_session(g.sf, "test", "last")
if not my_wxid: return ReJson(1001, body="my_wxid is required")
wx_path = read_session(g.sf, my_wxid, "wx_path")
img_path = img_path.replace("\\\\", "\\")
img_tmp_path = os.path.join(g.tmp_path, my_wxid, "img")
original_img_path = os.path.join(wx_path, img_path)
if os.path.exists(original_img_path):
fomt, md5, out_bytes = read_img_dat(original_img_path)
imgsavepath = os.path.join(img_tmp_path, img_path + "_" + ".".join([md5, fomt]))
if not os.path.exists(os.path.dirname(imgsavepath)):
os.makedirs(os.path.dirname(imgsavepath))
with open(imgsavepath, "wb") as f:
f.write(out_bytes)
return send_file(imgsavepath)
else:
return ReJson(1001, body=original_img_path)
@api.route('/api/contact_count_list', methods=["GET", 'POST'])
def contact_count_list():
"""
获取联系人列表以及聊天记录数量
:return:
"""
try:
# 获取联系人列表
# 从header中读取micro_path
msg_path = request.headers.get("msg_path")
micro_path = request.headers.get("micro_path")
if not msg_path:
msg_path = read_session(g.sf, "msg_path")
if not micro_path:
micro_path = read_session(g.sf, "micro_path")
start = request.json.get("start")
limit = request.json.get("limit")
word = request.json.get("word", "")
@api.route('/api/video/<path:videoPath>', methods=["GET", 'POST'])
def get_video(videoPath):
my_wxid = read_session(g.sf, "test", "last")
if not my_wxid: return ReJson(1001, body="my_wxid is required")
wx_path = read_session(g.sf, my_wxid, "wx_path")
contact_list = analyzer.get_contact_list(micro_path)
chat_count = analyzer.get_chat_count(msg_path)
for contact in contact_list:
contact["chat_count"] = chat_count.get(contact["username"], 0)
# 去重
contact_list = [dict(t) for t in {tuple(d.items()) for d in contact_list}]
# 降序
contact_list = sorted(contact_list, key=lambda x: x["chat_count"], reverse=True)
videoPath = videoPath.replace("\\\\", "\\")
# save_session(g.sf, "user_list", contact_list)
video_tmp_path = os.path.join(g.tmp_path, my_wxid, "video")
original_img_path = os.path.join(wx_path, videoPath)
if not os.path.exists(original_img_path):
return ReJson(5002)
# 复制文件到临时文件夹
video_save_path = os.path.join(video_tmp_path, videoPath)
if not os.path.exists(os.path.dirname(video_save_path)):
os.makedirs(os.path.dirname(video_save_path))
shutil.copy(original_img_path, video_save_path)
return send_file(original_img_path)
if word and word != "" and word != "undefined" and word != "null":
contact_list = [contact for contact in contact_list if
word in contact["account"] or word in contact["describe"] or word in contact[
"nickname"] or word in contact["remark"] or word in contact["username"]]
if limit:
contact_list = contact_list[int(start):int(start) + int(limit)]
return ReJson(0, contact_list)
except Exception as e:
return ReJson(9999, msg=str(e))
@api.route('/api/audio/<path:savePath>', methods=["GET", 'POST'])
def get_audio(savePath):
my_wxid = read_session(g.sf, "test", "last")
if not my_wxid: return ReJson(1001, body="my_wxid is required")
merge_path = read_session(g.sf, my_wxid, "merge_path")
savePath = os.path.join(g.tmp_path, my_wxid, "audio", savePath) # 这个是从url中获取的
if os.path.exists(savePath):
return send_file(savePath)
MsgSvrID = savePath.split("_")[-1].replace(".wav", "")
if not savePath:
return ReJson(1002)
# 判断savePath路径的文件夹是否存在
if not os.path.exists(os.path.dirname(savePath)):
os.makedirs(os.path.dirname(savePath))
parsing_media_msg = ParsingMediaMSG(merge_path)
wave_data = parsing_media_msg.get_audio(MsgSvrID, is_play=False, is_wave=True, save_path=savePath, rate=24000)
if not wave_data:
return ReJson(1001, body="wave_data is required")
if os.path.exists(savePath):
return send_file(savePath)
else:
return ReJson(4004, body=savePath)
@api.route('/api/file_info', methods=["GET", 'POST'])
def get_file_info():
file_path = request.args.get("file_path")
file_path = request.json.get("file_path", file_path)
if not file_path:
return ReJson(1002)
my_wxid = read_session(g.sf, "test", "last")
if not my_wxid: return ReJson(1001, body="my_wxid is required")
wx_path = read_session(g.sf, my_wxid, "wx_path")
all_file_path = os.path.join(wx_path, file_path)
if not os.path.exists(all_file_path):
return ReJson(5002)
file_name = os.path.basename(all_file_path)
file_size = os.path.getsize(all_file_path)
return ReJson(0, {"file_name": file_name, "file_size": str(file_size)})
@api.route('/api/file/<path:filePath>', methods=["GET", 'POST'])
def get_file(filePath):
my_wxid = read_session(g.sf, "test", "last")
if not my_wxid: return ReJson(1001, body="my_wxid is required")
wx_path = read_session(g.sf, my_wxid, "wx_path")
all_file_path = os.path.join(wx_path, filePath)
if not os.path.exists(all_file_path):
return ReJson(5002)
return send_file(all_file_path)
# end 以上为聊天记录相关api
@api.route('/api/msgs_user_list', methods=['GET', 'POST'])
@error9999
@ -249,17 +483,12 @@ def get_msg_list():
start = request.json.get("start")
limit = request.json.get("limit")
wxid = request.json.get("wxid")
my_wxid = read_session(g.sf, "my_wxid")
my_wxid = read_session(g.sf, "test", "last")
msg_list = analyzer.get_msg_list(msg_path, wxid, start_index=start, page_size=limit)
return ReJson(0, {"msg_list": msg_list, 'my_wxid': my_wxid})
def func_get_msgs(start, limit, wxid, msg_path, micro_path):
if not msg_path:
msg_path = read_session(g.sf, "msg_path")
if not micro_path:
micro_path = read_session(g.sf, "micro_path")
msg_list = analyzer.get_msg_list(msg_path, wxid, start_index=start, page_size=limit)
# row_data = {"MsgSvrID": MsgSvrID, "type_name": type_name, "is_sender": IsSender, "talker": talker,
# "room_name": StrTalker, "content": content, "CreateTime": CreateTime}
@ -287,148 +516,6 @@ def func_get_msgs(start, limit, wxid, msg_path, micro_path):
return {"msg_list": msg_list, "user_list": userlist, "my_wxid": my_wxid}
@api.route('/api/msgs', methods=["GET", 'POST'])
@error9999
def get_msgs():
msg_path = request.headers.get("msg_path")
micro_path = request.headers.get("micro_path")
if not msg_path:
msg_path = read_session(g.sf, "msg_path")
if not micro_path:
micro_path = read_session(g.sf, "micro_path")
start = request.json.get("start")
limit = request.json.get("limit")
wxid = request.json.get("wxid")
rdata = func_get_msgs(start, limit, wxid, msg_path, micro_path)
return ReJson(0, rdata)
@api.route('/api/realtimemsg', methods=["GET", "POST"])
@error9999
def get_real_time_msg():
"""
获取实时消息 使用 merge_real_time_db()函数
:return:
"""
save_msg_path = read_session(g.sf, "msg_path")
save_media_path = read_session(g.sf, "media_path")
save_micro_path = read_session(g.sf, "micro_path")
wx_path = read_session(g.sf, "wx_path")
key = read_session(g.sf, "key")
if not save_msg_path or not save_media_path or not wx_path or not key:
return ReJson(1002, body="msg_path or media_path or wx_path or key is required")
media_paths = get_core_db(wx_path, ["MediaMSG"])
msg_paths = get_core_db(wx_path, ["MSG"])
micro_paths = get_core_db(wx_path, ["MicroMsg"])
if not media_paths[0] or not msg_paths[0] or not micro_paths[0]:
return ReJson(1001, body="media_paths or msg_paths is required")
media_paths = media_paths[1]
media_paths.sort(key=lambda x: int(re.findall(r"\d+", x)[-1]))
msg_paths = msg_paths[1]
msg_paths.sort(key=lambda x: int(re.findall(r"\d+", x)[-1]))
micro_paths = micro_paths[1]
micro_paths.sort(key=lambda x: int(re.findall(r"\d+", x)[-1]))
# for i in media_paths:
# merge_real_time_db(key=key, db_path=i, merge_path=save_media_path)
# for i in msg_paths:
# merge_real_time_db(key=key, db_path=i, merge_path=save_msg_path)
# for i in micro_paths:
# merge_real_time_db(key=key, db_path=i, merge_path=save_micro_path)
merge_real_time_db(key=key, db_path=media_paths[-1], merge_path=save_media_path)
merge_real_time_db(key=key, db_path=msg_paths[-1], merge_path=save_msg_path)
merge_real_time_db(key=key, db_path=micro_paths[-1], merge_path=save_micro_path)
return ReJson(0, "success")
@api.route('/api/img', methods=["GET", 'POST'])
@error9999
def get_img():
"""
获取图片
:return:
"""
if request.method == "GET":
img_path = request.args.get("img_path")
elif request.method == "POST":
img_path = request.json.get("img_path")
else:
return ReJson(1003, msg="Unsupported method")
if not img_path:
return ReJson(1002)
img_path = img_path.replace("\\\\", "\\")
wx_path = read_session(g.sf, "wx_path")
img_tmp_path = os.path.join(g.tmp_path, "img")
img_path_all = os.path.join(wx_path, img_path)
if os.path.exists(img_path_all):
fomt, md5, out_bytes = read_img_dat(img_path_all)
imgsavepath = os.path.join(img_tmp_path, img_path + "_" + ".".join([md5, fomt]))
if not os.path.exists(os.path.dirname(imgsavepath)):
os.makedirs(os.path.dirname(imgsavepath))
with open(imgsavepath, "wb") as f:
f.write(out_bytes)
return send_file(imgsavepath)
else:
return ReJson(1001, body=img_path_all)
@api.route('/api/video/<path:videoPath>', methods=["GET", 'POST'])
def get_video(videoPath):
wx_path = read_session(g.sf, "wx_path")
videoPath = videoPath.replace("\\\\", "\\")
all_video_path = os.path.join(wx_path, videoPath)
if not os.path.exists(all_video_path):
return ReJson(5002)
return send_file(all_video_path)
@api.route('/api/file_info', methods=["GET", 'POST'])
def get_file_info():
file_path = request.args.get("file_path")
file_path = request.json.get("file_path", file_path)
if not file_path:
return ReJson(1002)
wx_path = read_session(g.sf, "wx_path")
all_file_path = os.path.join(wx_path, file_path)
if not os.path.exists(all_file_path):
return ReJson(5002)
file_name = os.path.basename(all_file_path)
file_size = os.path.getsize(all_file_path)
return ReJson(0, {"file_name": file_name, "file_size": str(file_size)})
@api.route('/api/file/<path:filePath>', methods=["GET", 'POST'])
def get_file(filePath):
wx_path = read_session(g.sf, "wx_path")
all_file_path = os.path.join(wx_path, filePath)
if not os.path.exists(all_file_path):
return ReJson(5002)
return send_file(all_file_path)
@api.route('/api/audio/<path:savePath>', methods=["GET", 'POST'])
def get_audio(savePath):
# savePath = request.args.get("savePath")
# savePath = request.json.get("savePath", savePath)
savePath = "audio/" + savePath # 这个是从url中获取的
MsgSvrID = savePath.split("_")[-1].replace(".wav", "")
if not savePath:
return ReJson(1002)
media_path = read_session(g.sf, "media_path")
wave_data = read_audio(MsgSvrID, is_wave=True, DB_PATH=media_path)
if not wave_data:
return ReJson(1001)
# 判断savePath路径的文件夹是否存在
savePath = os.path.join(g.tmp_path, savePath)
if not os.path.exists(os.path.dirname(savePath)):
os.makedirs(os.path.dirname(savePath))
with open(savePath, "wb") as f:
f.write(wave_data)
return send_file(savePath)
# 导出聊天记录
@api.route('/api/export', methods=["GET", 'POST'])
@error9999