From 637fe0b0c8342b076f323526c52b1151f16cbb6e Mon Sep 17 00:00:00 2001 From: Timmoc Date: Tue, 26 Nov 2024 10:29:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=8E=B0=E5=9C=A8=E4=B8=8D?= =?UTF-8?q?=E4=BC=9A=E8=A6=86=E7=9B=96=E7=8E=B0=E6=9C=89=E7=9A=84=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- recv/recv.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) 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