diff --git a/app/DataBase/hard_link.py b/app/DataBase/hard_link.py
index cb3a756..fdc4014 100644
--- a/app/DataBase/hard_link.py
+++ b/app/DataBase/hard_link.py
@@ -6,24 +6,30 @@ import xml.etree.ElementTree as ET
from app.log import log
-lock = threading.Lock()
-
-db_path = "./app/Database/Msg/HardLinkImage.db"
+image_db_lock = threading.Lock()
+video_db_lock = threading.Lock()
+image_db_path = "./app/Database/Msg/HardLinkImage.db"
+video_db_path = "./app/Database/Msg/HardLinkVideo.db"
root_path = 'FileStorage/MsgAttach/'
+video_root_path = 'FileStorage/Video/'
@log
-def get_md5_from_xml(content):
+def get_md5_from_xml(content, type_='img'):
try:
# 解析XML
root = ET.fromstring(content)
- # 提取md5的值
- md5_value = root.find(".//img").get("md5")
+ if type_ == 'img':
+ # 提取md5的值
+ md5_value = root.find(".//img").get("md5")
+ elif type_ == 'video':
+ md5_value = root.find(".//videomsg").get("md5")
# print(md5_value)
return md5_value
except ET.ParseError:
return None
+
def singleton(cls):
_instance = {}
@@ -38,20 +44,29 @@ def singleton(cls):
@singleton
class HardLink:
def __init__(self):
- self.DB = None
- self.cursor = None
+ self.imageDB = None
+ self.videoDB = None
+ self.image_cursor = None
+ self.video_cursor = None
self.open_flag = False
self.init_database()
def init_database(self):
if not self.open_flag:
- if os.path.exists(db_path):
- self.DB = sqlite3.connect(db_path, check_same_thread=False)
+ if os.path.exists(image_db_path):
+ self.imageDB = sqlite3.connect(image_db_path, check_same_thread=False)
# '''创建游标'''
- self.cursor = self.DB.cursor()
+ self.image_cursor = self.imageDB.cursor()
self.open_flag = True
- if lock.locked():
- lock.release()
+ if image_db_lock.locked():
+ image_db_lock.release()
+ if os.path.exists(video_db_path):
+ self.videoDB = sqlite3.connect(video_db_path, check_same_thread=False)
+ # '''创建游标'''
+ self.video_cursor = self.videoDB.cursor()
+ self.open_flag = True
+ if video_db_lock.locked():
+ video_db_lock.release()
def get_image_by_md5(self, md5: bytes):
if not md5:
@@ -66,23 +81,46 @@ class HardLink:
where MD5 = ?;
'''
try:
- lock.acquire(True)
+ image_db_lock.acquire(True)
try:
- self.cursor.execute(sql, [md5])
+ self.image_cursor.execute(sql, [md5])
except AttributeError:
self.init_database()
- finally:
- self.cursor.execute(sql, [md5])
- result = self.cursor.fetchone()
+ self.image_cursor.execute(sql, [md5])
+ result = self.image_cursor.fetchone()
return result
finally:
- lock.release()
+ image_db_lock.release()
+
+ def get_video_by_md5(self, md5: bytes):
+ if not md5:
+ return None
+ if not self.open_flag:
+ return None
+ sql = '''
+ select Md5Hash,MD5,FileName,HardLinkVideoID2.Dir as DirName2
+ from HardLinkVideoAttribute
+ join HardLinkVideoID as HardLinkVideoID2 on HardLinkVideoAttribute.DirID2 = HardLinkVideoID2.DirID
+ where MD5 = ?;
+ '''
+ try:
+ video_db_lock.acquire(True)
+ try:
+ self.video_cursor.execute(sql, [md5])
+ except AttributeError:
+ self.init_database()
+ self.video_cursor.execute(sql, [md5])
+ result = self.video_cursor.fetchone()
+ return result
+ finally:
+ video_db_lock.release()
def get_image(self, content, thumb=False):
md5 = get_md5_from_xml(content)
if not md5:
return None
result = self.get_image_by_md5(binascii.unhexlify(md5))
+
if result:
dir1 = result[3]
dir2 = result[4]
@@ -91,14 +129,29 @@ class HardLink:
dat_image = os.path.join(root_path, dir1, dir0, dir2, data_image)
return dat_image
+ def get_video(self, content, thumb=False):
+ md5 = get_md5_from_xml(content, type_='video')
+ if not md5:
+ return None
+ result = self.get_video_by_md5(binascii.unhexlify(md5))
+ if result:
+ dir2 = result[3]
+ data_image = result[2].split('.')[0] + '.jpg' if thumb else result[2]
+ # dir0 = 'Thumb' if thumb else 'Image'
+ dat_image = os.path.join(video_root_path, dir2, data_image)
+ return dat_image
+
def close(self):
if self.open_flag:
try:
- lock.acquire(True)
+ image_db_lock.acquire(True)
+ video_db_lock.acquire(True)
self.open_flag = False
- self.DB.close()
+ self.imageDB.close()
+ self.videoDB.close()
finally:
- lock.release()
+ image_db_lock.release()
+ video_db_lock.release()
def __del__(self):
self.close()
@@ -108,11 +161,19 @@ class HardLink:
if __name__ == '__main__':
msg_root_path = './Msg/'
- db_path = "./Msg/HardLinkImage.db"
+ image_db_path = "./Msg/HardLinkImage.db"
+ video_db_path = "./Msg/HardLinkVideo.db"
hard_link_db = HardLink()
hard_link_db.init_database()
- content = '''\n\t
\n\t\n\t\n\n'''
- print(hard_link_db.get_image(content))
- print(hard_link_db.get_image(content, thumb=False))
- result = get_md5_from_xml(content)
- print(result)
+ # content = '''\n\t
\n\t\n\t\n\n'''
+ # print(hard_link_db.get_image(content))
+ # print(hard_link_db.get_image(content, thumb=False))
+ # result = get_md5_from_xml(content)
+ # print(result)
+ content = '''
+
+
+
+'''
+ print(hard_link_db.get_video(content))
+ print(hard_link_db.get_video(content, thumb=True))