Skip to content

[pull] main from fscarmen2:main#4

Open
pull[bot] wants to merge 13 commits into
asdf243dsfa:mainfrom
fscarmen2:main
Open

[pull] main from fscarmen2:main#4
pull[bot] wants to merge 13 commits into
asdf243dsfa:mainfrom
fscarmen2:main

Conversation

@pull

@pull pull Bot commented Dec 17, 2024

Copy link
Copy Markdown

See Commits and Changes for more details.


Created by pull[bot] (v2.0.0-alpha.4)

Can you help keep this open source service alive? 💖 Please sponsor : )

使用示例:
如果设置 DIR = 'dirA'
访问 https://mydomain.workers.dev/dirB/dirC/file.jpg
实际会访问存储中的 dirA/dirB/dirC/file.jpg
@fscarmen2 fscarmen2 force-pushed the main branch 2 times, most recently from 5c8f651 to f12e195 Compare February 1, 2025 09:04
notagshen and others added 3 commits April 21, 2025 21:50
Problem:
const r2Path = `${r2Config.bucket}/${DIR}/${storagePath}`;

当 DIR 为空时,生成的路径变成 bucket//subfolder/file.ext,这在 S3 兼容的 API 中被视为不同于 bucket/subfolder/file.ext 的路径,因此找不到文件。



Solution:
const r2Path = DIR ? `${r2Config.bucket}/${DIR}/${storagePath}` : `${r2Config.bucket}/${storagePath}`;

修改路径构建逻辑,增加处理dir为空的逻辑,如果为空则直接拼接 bucket 和 storagePath。
…dflare Worker scripts

Changes in github_gitlab_r2_b2.js:
- Add encodePathSegments() and buildEncodedPath() to encode path segments individually
- Fix double-slash bug when DIR is empty (main//filename → main/filename)
- Wrap decodeURIComponent in try/catch to prevent crashes on malformed URLs
- Apply correct encoding to all GitHub raw URLs and GitHub API (where mode) URLs
- Split CHECK_PASSWORD into CUSTOM_PASSWORD + CHECK_PASSWORD to avoid "always truthy" lint warning
- Add @type {string} annotation to DIR to suppress "types have no overlap" lint warning

Changes in github_only.js:
- Decode requestPath with try/catch before extracting FILE, enabling CJK filename support
- Add encodePathSegments() helper and rewrite URL builder to filter empty DIR, avoiding double slashes

Changes in gitlab_only.js:
- Encode each path segment individually in filePath construction instead of joining then encoding, preserving multi-level directory slashes
- Fix getFileSizeFromGitLab to split file.path by "/" and encode each segment, preventing "/" from being encoded as "%2F"

---

修复:在三个 Cloudflare Worker 脚本中支持中日韩文件名及 URL 编码问题

github_gitlab_r2_b2.js 的改动:
- 新增 encodePathSegments() 和 buildEncodedPath(),对路径逐段编码,支持中文/日文/韩文文件名
- 修复双斜杠问题:DIR 为空时不再产生 main//文件名
- decodeURIComponent 加 try/catch 保护,防止畸形 URL 导致 Worker 崩溃
- 所有 GitHub raw URL 及 GitHub API(where 模式)均应用了新编码逻辑
- 将 CHECK_PASSWORD 拆分为 CUSTOM_PASSWORD + CHECK_PASSWORD,消除"always truthy"静态检查警告
- 为 DIR 添加 @type {string} 注解,消除"types have no overlap"警告

github_only.js 的改动:
- 对 url.pathname 做带 try/catch 保护的解码后再提取 FILE,支持中文文件名
- 新增 encodePathSegments() 并重写 URL 构建逻辑,过滤空 DIR,避免 raw URL 出现双斜杠

gitlab_only.js 的改动:
- filePath 构建改为对 pathParts 逐段编码再拼接,修复整体编码时 / 被错误编码为 %2F 的问题
- 修复 getFileSizeFromGitLab:file.path 按 / 分割后逐段编码,正确保留多级目录的斜杠结构
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

⤵️ pull merge-conflict Resolve conflicts manually

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants