From de58647402ee03c8af28be94048260dc2613710f Mon Sep 17 00:00:00 2001 From: lededev Date: Sat, 9 Apr 2022 01:35:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A8=A1=E5=BC=8F2=E6=94=AF=E6=8C=81=E7=A1=AC?= =?UTF-8?q?=E9=93=BE=E6=8E=A5=EF=BC=9B=E6=94=B9=E8=BF=9B=E5=AD=97=E5=B9=95?= =?UTF-8?q?=E5=A4=8D=E5=88=B6=E9=80=82=E5=BA=94=E6=9B=B4=E5=A4=9A=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=90=8D=E7=BB=84=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core.py | 64 +++++++++++++++++++++++++++++++-------------------------- 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/core.py b/core.py index 4aa3c78..d512d01 100644 --- a/core.py +++ b/core.py @@ -494,7 +494,6 @@ def add_to_pic(pic_path, img_pic, size, count, mode): def paste_file_to_folder(filepath, path, number, leak_word, c_word, hack_word): # 文件路径,番号,后缀,要移动至的位置 filepath_obj = pathlib.Path(filepath) houzhui = filepath_obj.suffix - file_parent_origin_path = str(filepath_obj.parent) try: targetpath = os.path.join(path, f"{number}{leak_word}{c_word}{hack_word}{houzhui}") # 任何情况下都不要覆盖,以免遭遇数据源或者引擎错误导致所有文件得到同一个number,逐一 @@ -520,25 +519,19 @@ def paste_file_to_folder(filepath, path, number, leak_word, c_word, hack_word): filerelpath = os.path.relpath(filepath, path) os.symlink(filerelpath, targetpath) except: - os.symlink(filepath_obj.resolve(), targetpath) - sub_res = config.getInstance().sub_rule() + os.symlink(str(filepath_obj.resolve()), targetpath) - for subname in sub_res: - sub_filepath = str(filepath_obj.with_suffix(subname)) - if os.path.isfile(sub_filepath.replace(subname,".chs" + subname)): - sub_filepath = sub_filepath.replace(subname,".chs" + subname) - subname = ".chs" + subname - elif os.path.isfile(sub_filepath.replace(subname,".cht" + subname)): - sub_filepath = sub_filepath.replace(subname, ".cht" + subname) - subname = ".cht" + subname - if os.path.isfile(sub_filepath): + sub_res = [subext.lower() for subext in config.getInstance().sub_rule()] + for subfile in filepath_obj.parent.glob('**/*'): + if subfile.is_file() and subfile.suffix.lower() in sub_res: + sub_targetpath = Path(path) / f"{number}{leak_word}{c_word}{hack_word}{''.join(subfile.suffixes)}" if link_mode not in (1, 2): - shutil.move(sub_filepath, os.path.join(path, f"{number}{leak_word}{c_word}{hack_word}{subname}")) - print('[+]Sub moved!') + shutil.move(str(subfile), str(sub_targetpath)) + print(f"[+]Sub Moved! '{sub_targetpath.name}'") else: - shutil.copyfile(sub_filepath, os.path.join(path, f"{number}{leak_word}{c_word}{hack_word}{subname}")) - print('[+]Sub Copied!') - return True + shutil.copyfile(str(subfile), str(sub_targetpath)) + print(f"[+]Sub Copied! '{sub_targetpath.name}'") + return except FileExistsError as fee: print(f'[-]FileExistsError: {fee}') @@ -557,24 +550,37 @@ def paste_file_to_folder_mode2(filepath, path, multi_part, number, part, leak_wo number += part # 这时number会被附加上CD1后缀 filepath_obj = pathlib.Path(filepath) houzhui = filepath_obj.suffix - file_parent_origin_path = str(filepath_obj.parent) targetpath = os.path.join(path, f"{number}{part}{leak_word}{c_word}{hack_word}{houzhui}") if os.path.exists(targetpath): raise FileExistsError('File Exists on destination path, we will never overwriting.') try: - if config.getInstance().link_mode(): - os.symlink(filepath, targetpath) - else: + link_mode = config.getInstance().link_mode() + create_softlink = False + if link_mode not in (1, 2): shutil.move(filepath, targetpath) + elif link_mode == 2: + try: + os.link(filepath, targetpath, follow_symlinks=False) + except: + create_softlink = True + if link_mode == 1 or create_softlink: + try: + filerelpath = os.path.relpath(filepath, path) + os.symlink(filerelpath, targetpath) + except: + os.symlink(str(filepath_obj.resolve()), targetpath) - sub_res = config.getInstance().sub_rule() - for subname in sub_res: - sub_filepath = str(filepath_obj.with_suffix(subname)) - if os.path.isfile(sub_filepath): # 字幕移动 - shutil.move(sub_filepath, os.path.join(path, f"{number}{part}{leak_word}{c_word}{hack_word}{subname}")) - print('[+]Sub moved!') - print('[!]Success') - return True + sub_res = [subext.lower() for subext in config.getInstance().sub_rule()] + for subfile in filepath_obj.parent.glob('**/*'): + if subfile.is_file() and subfile.suffix.lower() in sub_res: + sub_targetpath = Path(path) / f"{number}{leak_word}{c_word}{hack_word}{''.join(subfile.suffixes)}" + if link_mode not in (1, 2): + shutil.move(str(subfile), str(sub_targetpath)) + print(f"[+]Sub Moved! '{sub_targetpath.name}'") + else: + shutil.copyfile(str(subfile), str(sub_targetpath)) + print(f"[+]Sub Copied! '{sub_targetpath.name}'") + return except FileExistsError as fee: print(f'[-]FileExistsError: {fee}') return