Skip to content

Commit

Permalink
修复大文件无法下载的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
H-Sofie committed Jan 31, 2025
1 parent 0021e46 commit c493934
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions update_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'}
Expand All @@ -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 中。
Expand Down Expand Up @@ -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:
# 是子目录
Expand All @@ -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)
Expand Down

0 comments on commit c493934

Please sign in to comment.