Skip to content

Conversation

@arksia
Copy link

@arksia arksia commented Dec 1, 2025

修复多个上传组件同时粘贴上传会覆写 dataTransfer 中文件的 uid,导致文件状态更新异常的问题。
image
图中两个文件上传都失败了,但是第一个文件始终处于 uploading 状态

Summary by CodeRabbit

  • Bug Fixes

    • 修复粘贴/拖拽场景中文件实例共享导致的标识冲突:对粘贴/拖拽接收的文件进行重建并保留名称、类型和修改时间,提升并发操作下的元数据一致性与稳定性。
  • Tests

    • 新增并发粘贴/上传场景测试,验证不同上传组件同时操作时各上传项的唯一标识稳定且互不干扰。

✏️ Tip: You can customize this high-level summary in your review settings.

@vercel
Copy link

vercel bot commented Dec 1, 2025

@arksia is attempting to deploy a commit to the React Component Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link

coderabbitai bot commented Dec 1, 2025

Walkthrough

在粘贴/拖放处理路径中,AjaxUploader 在接收的 dataTransfer.files 上为每个文件显式构造新的 File 实例以避免共享 uid 被互相覆盖;新增测试在两个并发 Upload 组件粘贴时验证 uid 独立性并使用 xhr 模拟响应完成流程。

Changes

Cohort / File(s) 更改摘要
文件重建与上传逻辑
src/AjaxUploader.tsx
onDataTransferFiles 中将 dataTransfer.files 的每个文件通过 File 构造函数重建为新的实例(保留 name、type、lastModified),并移除一处空行(无功能影响)。
并发上传测试覆盖
tests/uploader.spec.tsx
新增并发粘贴场景测试:渲染两个 Upload 组件、模拟粘贴产生文件、捕获并比较两侧的 uid(确保分别存在且互不相同),并通过 xhr 响应模拟服务器完成上传(使用 requests[n].respond 同步各组件流程)。

Sequence Diagram(s)

(已跳过:变更为修复并发 uid 覆盖的问题,属于漏洞修复,流程简单且不需序列图。)

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 分钟

Possibly related PRs

Suggested reviewers

  • zombieJ

Poem

🐇 我是小兔在代码田,
轻轻重造每个文件名,
粘贴并行不互侵,uid 各自真,
测试轻敲鼓声清,
快活跳进主分支门 🥕

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed PR标题清晰准确地概括了主要变更:修复多个上传组件同时粘贴导致的uid冲突问题,与代码变更中文件克隆和uid稳定性的实现完全相符。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between dfef270 and d6fe228.

📒 Files selected for processing (1)
  • src/AjaxUploader.tsx
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/AjaxUploader.tsx

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist
Copy link

Summary of Changes

Hello @arksia, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求修复了一个关键错误,该错误影响了当多个组件同时处理粘贴文件时的文件上传功能。核心问题源于共享的文件引用导致 uid 冲突和错误的状态更新。解决方案涉及为每个上传操作创建独立的 File 对象,从而隔离它们的状态并确保正确的跟踪。此更改还附带了一个新的、专门的测试用例,以验证在同时粘贴场景下修复的有效性。

Highlights

  • 修复 UID 冲突: 解决了当多个上传组件同时粘贴上传文件时,dataTransfer 中的文件 uid 可能被覆盖,导致文件状态更新异常的问题。
  • 创建新的文件对象: 通过对 dataTransfer.files 进行映射,创建新的 File 对象实例,确保每个组件处理独立的文件实例,从而避免 uid 冲突。
  • 增强测试覆盖: 新增了一个测试用例,专门验证在多个上传组件同时处理粘贴事件时,文件 uid 不会被覆盖。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

你好,感谢你的贡献!这个修复通过为每个上传组件克隆文件对象,巧妙地解决了多个上传组件同时处理粘贴事件时可能出现的 uid 冲突问题。这是一个很棒的解决方案。我还添加了一些建议来改进代码和新添加的测试用例,使其更加健壮和简洁。请查看我的评论。

Comment on lines 98 to 103
let files: File[] = [...(dataTransfer.files || [])].map((file: File) => {
return new File([file], file.name, {
type: file.type,
lastModified: file.lastModified,
});
});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

为了代码更简洁,这里的 map 函数可以简化为单行表达式,使用隐式返回。

Suggested change
let files: File[] = [...(dataTransfer.files || [])].map((file: File) => {
return new File([file], file.name, {
type: file.type,
lastModified: file.lastModified,
});
});
let files: File[] = [...(dataTransfer.files || [])].map(file => (
new File([file], file.name, {
type: file.type,
lastModified: file.lastModified,
})
));

Comment on lines 640 to 641
await sleep(100);
requests[0].respond(200, {}, `["","${files[0].name}"]`);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

这个测试用例很好地覆盖了并发粘贴的场景。不过,它可以更健壮一些。目前它只完成了一个上传请求,并且没有验证两个组件生成的 uid 是唯一的。

我建议更新测试以:

  1. 断言创建了两个独立的请求。
  2. 断言 uid 是不同的。
  3. 完成两个上传请求,以确保两个组件的生命周期回调都得到测试。
      await sleep(100);

      expect(requests).toHaveLength(2);
      expect(uid1).toBeDefined();
      expect(uid2).toBeDefined();
      expect(uid1).not.toEqual(uid2);

      // 完成两个上传
      requests[0].respond(200, {}, `["","${files[0].name}"]`);
      requests[1].respond(200, {}, `["","${files[0].name}"]`);

@codecov
Copy link

codecov bot commented Dec 2, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.62%. Comparing base (c0188c8) to head (d6fe228).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #686      +/-   ##
==========================================
+ Coverage   89.58%   89.62%   +0.03%     
==========================================
  Files           6        6              
  Lines         317      318       +1     
  Branches       90       94       +4     
==========================================
+ Hits          284      285       +1     
  Misses         33       33              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@afc163
Copy link
Member

afc163 commented Dec 2, 2025

看下 gemini 的 review 建议

@arksia
Copy link
Author

arksia commented Dec 2, 2025

看下 gemini 的 review 建议

已根据 review 调整补充

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a concurrency bug where multiple upload components with pastable: true would interfere with each other when receiving the same paste/drop event simultaneously. The issue occurred because all components were mutating the same shared File objects from the DataTransfer API, causing UID conflicts and incorrect upload state tracking.

Key Changes:

  • Creates new File instances from DataTransfer files to prevent shared object mutation
  • Preserves essential file metadata (name, type, lastModified)
  • Adds comprehensive test coverage for the concurrent paste scenario
  • Updates existing tests to use consistent mocking patterns

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/AjaxUploader.tsx Implements fix by creating new File instances from dataTransfer.files using File constructor, preventing shared object mutations across multiple upload components
tests/uploader.spec.tsx Adds test validating UID uniqueness when multiple components receive the same paste event; updates existing tests to use response mocking consistently

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

expect(uidsInOnSuccess[0]).toBeDefined();
expect(uidsInOnSuccess[1]).toBeDefined();
expect(uidsInOnSuccess[0]).not.toEqual(uidsInOnSuccess[1]);

Copy link

Copilot AI Jan 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing whitespace detected. Consider removing it for consistency with the codebase style.

Suggested change

Copilot uses AI. Check for mistakes.

delete this.reqs[uid];
},
};
Copy link

Copilot AI Jan 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whitespace-only change appears to be unintentional. Consider keeping the blank line after the object definition for better readability.

Suggested change
};
};

Copilot uses AI. Check for mistakes.
value: i => files[i],
});

Copy link

Copilot AI Jan 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whitespace-only change appears to be unintentional. Consider keeping the original blank line for consistency with the rest of the codebase.

Suggested change

Copilot uses AI. Check for mistakes.
Comment on lines 98 to 103
let files: File[] = [...(dataTransfer.files || [])].map(file => (
new File([file], file.name, {
type: file.type,
lastModified: file.lastModified,
})
));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// Clone File objects to avoid shared uid mutation between multiple Upload components
const files: File[] = Array.from(dataTransfer.files ?? [], file =>
  new File([file], file.name, {
    type: file.type,
    lastModified: file.lastModified,
  })
);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改为 Array.form 克隆文件数组,但还是得用 let,因为后面有对 files 赋值

if (directory) {
  files = await traverseFileTree(Array.prototype.slice.call(items), this.filterFile);
  this.uploadFiles(files);
} else {
  let acceptFiles = [...files].filter(file => this.filterFile(file, true));
  if (multiple === false) {
     acceptFiles = files.slice(0, 1);
  }
  this.uploadFiles(acceptFiles);
}

@arksia arksia requested a review from afc163 January 11, 2026 10:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants