diff --git a/recv/recv.py b/recv/recv.py index 91bfc4a..28abeb9 100644 --- a/recv/recv.py +++ b/recv/recv.py @@ -1,6 +1,5 @@ import base64 - -from Crypto.Hash import SHA256 +import os.path from config import config from entity.Letter import Letter @@ -55,9 +54,23 @@ def handleLetter(letter: Letter): # 默认下载目录 download_dir = DownloadPathTool.get_download_directory() - - with open(f"{download_dir}/{letter.fileName}", "wb") as f: + filename = letter.fileName + base_name, ext = os.path.splitext(filename) # 分离文件名和扩展名 + newName = None + count = 0 + while True: + if count: + newName = f"{base_name}({count}){ext}" + else: + newName = filename + path = f"{download_dir}/{newName}" + if os.path.exists(path): + print(f"文件已存在,自动避免覆盖写入") + count += 1 + else: + break + with open(path, "wb") as f: f.write(data) print(f"确认收到来自 {Segwit.encodeSegwit(letter.senderPubKey.encode("utf-8"))} 的文件") - print(f"签名验证有效,已将文件 {letter.fileName} 保存至 {download_dir} 下") + print(f"签名验证有效,已将文件 {newName} 保存至 {download_dir} 下") return