Skip to content

perf(worker): 附件内容 hash 去重(#387)#393

Merged
leeguooooo merged 1 commit into
mainfrom
feat/387-attachment-hash
Jul 12, 2026
Merged

perf(worker): 附件内容 hash 去重(#387)#393
leeguooooo merged 1 commit into
mainfrom
feat/387-attachment-hash

Conversation

@leeguooooo

Copy link
Copy Markdown
Owner

附件 object 之前用随机 uuid 命名,同文件重复上传在 R2 存多份。改成按内容 sha256 命名:key = <slug>/<sha256>/<filename>。同频道同内容+同名重复上传落同一 key,head 命中就跳过 put——省存储+带宽,hash 即完整性锚。

key 前缀仍锚定 slug(跨频道隔离不动);旧 uuid-key 存量附件照常解析,向后兼容。

worker 全量 + 附件 round-trip 全过(新增去重断言)、tsc 干净。

Closes #387

之前上传 object 用随机 uuid 命名,同一文件重复上传就在 R2 里存多份。改成按内容
sha256 命名:object key = <slug>/<sha256>/<filename>。同频道内同内容+同名重复上传落
到同一 key,先 head 检查命中就跳过 put——省 R2 存储 + 上行带宽;hash 即完整性锚。

key 前缀仍锚定 slug 做跨频道隔离(安全边界不动)。旧 uuid-key 的存量附件照常解析
(key 存在引用里,下载不受影响),向后兼容。

验证:worker 全量 + 附件 round-trip 测试全过(新增去重断言:同内容同名→同 key、
不同内容→不同 key、去重命中后仍可下载)、tsc 干净。

Closes #387

Claude-Session: https://claude.ai/code/session_01PgxkZeqJmDge3dYe9W2tPZ
@github-actions

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis ✅

387 - Fully compliant

Compliant requirements:

  • 附件内容使用 sha256 命名,以实现去重
  • 同频道内同内容+同名的附件上传到同一个 key
  • 确保跨频道隔离
  • 新增去重断言测试
⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ No major issues detected

@github-actions

Copy link
Copy Markdown

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
Possible issue
添加异常处理。

在检查对象是否已存在时,确保处理可能的异常情况,以避免潜在的错误。

worker/src/index.ts [5563-5567]

-if (existing === null) {
-  await c.env.ATTACHMENTS.put(key, bytes, {
-    httpMetadata: { contentType },
-    customMetadata: { filename, uploaded_by: identity.name, channel: slug },
-  });
+try {
+  if (existing === null) {
+    await c.env.ATTACHMENTS.put(key, bytes, {
+      httpMetadata: { contentType },
+      customMetadata: { filename, uploaded_by: identity.name, channel: slug },
+    });
+  }
+} catch (error) {
+  console.error(`Failed to put attachment: ${error}`);
+  return c.json(errorBody("internal_error", "failed to store attachment"), 500);
 }
Suggestion importance[1-10]: 5

__

Why: 在检查对象是否已存在并进行上传时,添加异常处理可以提高代码的健壮性,但这是一个相对较小的改进,主要影响错误处理和日志记录。

Low

@leeguooooo leeguooooo merged commit 06859d6 into main Jul 12, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

附件 hash 优化

1 participant