Skip to content

Conversation

@LusiyAvA
Copy link

@LusiyAvA LusiyAvA commented Jan 6, 2026

Description / 描述

This PR implements the frontend logic for chunked file uploads in form mode, corresponding to the backend changes.
此 PR 实现了 Form 模式下分片上传的前端逻辑,以配合后端的变更。

Key changes include:
主要更改包括:

  1. File Slicing: Implemented logic to slice files based on backend configuration before uploading.
    文件切片:实现了上传前根据后台配置对文件进行切片的逻辑。
  2. Integrity Check: Added crc-32 calculation for each chunk to ensure data integrity during transmission.
    完整性校验:为每个分片添加了 crc-32 计算,以确保传输过程中的数据完整性。
  3. Upload Loop: Updated the upload component to handle the sequential upload of chunks and error retries.
    上传循环:更新了上传组件以处理分片的顺序上传和错误重试。

Motivation and Context / 背景

Standard form uploads are prone to failure with large files or unstable network conditions. Introducing chunked uploads improves reliability and user experience.
普通的表单上传在处理大文件或网络不稳定时容易失败。引入分片上传可以显著提高可靠性和用户体验。

Relates to OpenListTeam/OpenList#1928

How Has This Been Tested? / 测试

  • Manually tested uploading large files (>500MB) in form mode.
    手动测试了 Form 模式下上传大于 500MB 的大文件。
  • Verified that crc-32 is correctly calculating checksums for chunks.
    验证了 crc-32 能正确计算分片的校验和。
  • Verified successful assembly of files with the backend (PR #1928).
    验证了配合后端 (PR #1928) 能成功合并文件。

Checklist / 检查清单

  • I have read the CONTRIBUTING document.
    我已阅读 CONTRIBUTING 文档。
  • I have formatted my code with go fmt or prettier.
    我已使用 go fmtprettier 格式化提交的代码。
  • I have added appropriate labels to this PR (or mentioned needed labels in the description if lacking permissions).
    (Maintainers please add feat and frontend labels / 请维护者添加 featfrontend 标签)
  • I have requested review from relevant code authors using the "Request review" feature when applicable.
    我已在适当情况下使用"Request review"功能请求相关代码作者进行审查。
  • I have updated the repository accordingly (If it’s needed).
    我已相应更新了相关仓库(若适用)。

Fill in the help English field in /src/lang/en/settings.json.
}
}

export const FormUpload: Upload = async (
Copy link
Member

Choose a reason for hiding this comment

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

不应覆盖原有form上传逻辑,分片上传和原form应作为两种上传方式供用户选择

}

// Keep old signature for compatibility if needed, but we essentially replace it
export const calculateHash = async (file: File) => {
Copy link
Member

Choose a reason for hiding this comment

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

calculateHash是客户端秒传功能使用的,不应直接覆盖

Comment on lines +22 to +34
async function generateUploadId(path: string, file: File): Promise<string> {
const sample = file.slice(0, Math.min(1024 * 1024, file.size))
const buffer = await sample.arrayBuffer()
const hashBuffer = await crypto.subtle.digest("SHA-256", buffer)
const hashHex = Array.from(new Uint8Array(hashBuffer))
.slice(0, 8)
.map((b) => b.toString(16).padStart(2, "0"))
.join("")
// Use encodeURIComponent to handle Unicode characters before btoa
const rawId = `${path}|${file.size}|${hashHex}`
const encodedId = btoa(encodeURIComponent(rawId))
return encodedId.replace(/[+/=]/g, "_")
}
Copy link
Member

Choose a reason for hiding this comment

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

二选一:

  1. 应在服务端生成upload_id
  2. 服务端应校验upload_id与首个分片的哈希值是否一致,同时在首个分片上传成功前拒绝其它分片

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