Skip to content

Commit

Permalink
支持按照字节数计算路径长度以改进文件系统兼容性 (#8, #119)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuukiy committed Sep 10, 2023
1 parent dd910d9 commit bb57163
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
6 changes: 3 additions & 3 deletions JavSP.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,9 @@ def generate_names(movie: Movie):
d['num'] = info.dvdid or info.cid
d['title'] = info.title or cfg.NamingRule.null_for_title
d['rawtitle'] = info.ori_title or d['title']
if info.actress and len(info.actress) > cfg.NamingRule.max_acctress_count:
if info.actress and len(info.actress) > cfg.NamingRule.max_actress_count:
logging.debug('女优人数过多,按配置保留了其中的前n个: ' + ','.join(info.actress))
actress = info.actress[:cfg.NamingRule.max_acctress_count] + ['…']
actress = info.actress[:cfg.NamingRule.max_actress_count] + ['…']
else:
actress = info.actress
d['actress'] = ','.join(actress) if actress else cfg.NamingRule.null_for_actress
Expand Down Expand Up @@ -296,7 +296,7 @@ def generate_names(movie: Movie):
copyd['title'] = copyd['title'][:remaining]
copyd['rawtitle'] = copyd['rawtitle'][:remaining]
if (copyd['title'] == '' and '$title' in templates) or (copyd['rawtitle'] == '' and '$rawtitle' in templates):
logger.error("命名规则导致标题被截断至空,请增大'max_path_len'或减小'max_acctress_count'配置项后重试")
logger.error("命名规则导致标题被截断至空,请增大'max_path_len'或减小'max_actress_count'配置项后重试")
logger.debug((d, templates, cfg.NamingRule.max_path_len))
return
save_dir = os.path.normpath(cfg.NamingRule.save_dir.substitute(copyd)).strip()
Expand Down
8 changes: 5 additions & 3 deletions core/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@ output_folder = #整理完成
save_dir = $actress/[$num] $title
# 影片、封面、nfo信息文件等的文件名将基于下面的规则来创建
filename = $num
# 允许的最长文件路径(以字符计数,不区分中英日字符。路径过长时将据此自动截短标题)
max_path_len = 240
# 允许的最长文件路径(路径过长时将据此自动截短标题)
max_path_len = 250
# 是否以字节数来计算文件路径长度(auto/yes/no, auto将自动根据输出路径的文件系统是本地还是远程来判断)
calc_path_len_by_byte = auto
# 路径中的$actress字段最多包含多少名女优?
max_acctress_count = 10
max_actress_count = 10
# nfo文件中的影片标题(即媒体管理工具中显示的标题)
nfo_title = $num $title
# 下面这些项用来设置对应变量为空时的替代信息
Expand Down
9 changes: 8 additions & 1 deletion core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def norm_int(cfg: Config):
cfg.Network.retry = cfg.getint('Network', 'retry')
cfg.Network.timeout = cfg.getint('Network', 'timeout')
cfg.NamingRule.max_path_len = min(cfg.getint('NamingRule', 'max_path_len'), 256)
cfg.NamingRule.max_acctress_count = max(cfg.getint('NamingRule', 'max_acctress_count'), 1)
cfg.NamingRule.max_actress_count = max(cfg.getint('NamingRule', 'max_actress_count'), 1)


def norm_tuples(cfg: Config):
Expand Down Expand Up @@ -257,6 +257,13 @@ def norm_boolean(cfg: Config):
('Other', 'auto_update')
]:
cfg._sections[sec][key] = cfg.getboolean(sec, key)
# 特殊转换
sec, key = 'NamingRule', 'calc_path_len_by_byte'
try:
cfg._sections[sec][key] = cfg.getboolean(sec, key)
except ValueError:
# 当配置为auto时会转换失败,此时保留auto配置以待后面根据文件系统来作更新
cfg._sections[sec][key] = cfg._sections[sec][key].lower()


def norm_ignore_pattern(cfg: Config):
Expand Down
18 changes: 17 additions & 1 deletion core/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import re
import sys
import ctypes
import logging
from sys import platform
from typing import List
Expand Down Expand Up @@ -147,11 +148,26 @@ def replace_illegal_chars(name):
return name


def is_remote_drive(path: str):
"""判断一个路径是否为远程映射到本地"""
#TODO: 当前仅支持Windows平台
DRIVE_REMOTE = 0x4
drive = os.path.splitdrive(os.path.abspath(path))[0] + os.sep
result = ctypes.windll.kernel32.GetDriveTypeW(drive)
return result == DRIVE_REMOTE


def get_remaining_path_len(path):
"""计算当前系统支持的最大路径长度与给定路径长度的差值"""
#TODO: 支持不同的操作系统
fullpath = os.path.abspath(path)
remaining = cfg.NamingRule.max_path_len - len(fullpath)
# Windows: If the length exceeds ~256 characters, you will be able to see the path/files via Windows/File Explorer, but may not be able to delete/move/rename these paths/files
if cfg.NamingRule.calc_path_len_by_byte == 'auto':
is_remote = is_remote_drive(path)
logger.debug(f"目标路径{['是', '不是'][is_remote]}远程文件系统")
cfg.NamingRule.calc_path_len_by_byte = is_remote
length = len(fullpath.encode('utf-8')) if cfg.NamingRule.calc_path_len_by_byte else len(fullpath)
remaining = cfg.NamingRule.max_path_len - length
return remaining


Expand Down

0 comments on commit bb57163

Please sign in to comment.