From c4939347a90b06a36a2b6e2fe09b0d3be8c9083f Mon Sep 17 00:00:00 2001 From: H-Sofie <2088711818@qq.com> Date: Fri, 31 Jan 2025 10:43:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=A4=A7=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E4=B8=8B=E8=BD=BD=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- update_dir.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/update_dir.py b/update_dir.py index 8dc6dde7d..9ff1b26a4 100644 --- a/update_dir.py +++ b/update_dir.py @@ -9,9 +9,13 @@ repo_full = os.environ.get("GITHUB_REPOSITORY", "defaultOwner/defaultRepo") branch = os.environ.get("GITHUB_REF_NAME", "main") -# 文本文件走 blob 链接(在线查看),二进制文件走 cdn 链接(便于直接下载) +# 文本文件走 blob 链接(在线查看),其他文件走 cdn 链接(便于直接下载),大文件反代加速 BLOB_URL_PREFIX = f"https://github.com/{repo_full}/blob/{branch}/" BIN_URL_PREFIX = f"https://cdn.jsdmirror.com/gh/{repo_full}/" +LARGE_URL_PREFIX = f"https://raw.github.site/{repo_full}/refs/heads/{branch}/" + +# 大文件阈值 15MB +LARGE_THRESHOLD = 15 * 1024 * 1024 # 顶层排除目录 EXCLUDE_TOP_DIRS = {'.git', 'docs', '.vscode', '.circleci', 'site', 'image'} @@ -22,6 +26,7 @@ # 生成 blob 链接 BLOB_EXTS = {'md', 'txt', 'c', 'cpp', 'py'} + def process_directory(base_dir: str, rel_path: str): """ 将 base_dir(绝对路径) 对应的目录内容,递归生成到 docs/rel_path 目录下的 index.md 中。 @@ -56,11 +61,18 @@ def process_directory(base_dir: str, rel_path: str): # 若是 README,则已处理过;否则纳入链接 if item not in README_CANDIDATES: ext = item.split(".")[-1].lower() if "." in item else "" - # 判断用 blob 还是 cdn 加速 + file_size = os.path.getsize(full_item_path) + + # blob 类文件链接 if ext in BLOB_EXTS: file_url = BLOB_URL_PREFIX + quote(f"{rel_path}/{item}") else: - file_url = BIN_URL_PREFIX + quote(f"{rel_path}/{item}") + # 大文件链接 + if file_size > LARGE_THRESHOLD: + file_url = LARGE_URL_PREFIX + quote(f"{rel_path}/{item}") + else: + file_url = BIN_URL_PREFIX + quote(f"{rel_path}/{item}") + file_links.append((item, file_url)) else: # 是子目录 @@ -80,9 +92,8 @@ def process_directory(base_dir: str, rel_path: str): # 3) 列出当前目录内的文件链接 for fname, url in file_links: wf.write(f"- [{fname}]({url})\n") - wf.write("\n") - # 4) 递归处理子目录 + # 递归处理子目录 for subdir in subdirs: sub_rel_path = os.path.join(rel_path, subdir) full_subdir_path = os.path.join(base_dir, subdir)