feat(remote): 实现文件下载真实进度显示
- 下载改用流式读取,计算真实进度百分比 - 通过 IPC 事件实时推送进度到前端 - 支持 Content-Length 计算下载进度
This commit is contained in:
@@ -119,13 +119,31 @@ export const downloadFileFromRemote = async (
|
||||
password?: string,
|
||||
onProgress?: (progress: number) => void
|
||||
): Promise<void> => {
|
||||
onProgress?.(50)
|
||||
const result = await window.electronAPI.remoteDownloadFile(serverHost, port, fileName, remotePath, localPath, password)
|
||||
onProgress?.(100)
|
||||
if (!result.success) {
|
||||
if (result.canceled) {
|
||||
return
|
||||
const transferId = Date.now().toString()
|
||||
|
||||
const cleanup = window.electronAPI?.onDownloadProgress?.((data) => {
|
||||
if (data.id === transferId) {
|
||||
onProgress?.(data.progress)
|
||||
}
|
||||
throw new Error(result.error || 'Download failed')
|
||||
})
|
||||
|
||||
try {
|
||||
const result = await window.electronAPI.remoteDownloadFile(
|
||||
transferId,
|
||||
serverHost,
|
||||
port,
|
||||
fileName,
|
||||
remotePath,
|
||||
localPath,
|
||||
password
|
||||
)
|
||||
if (!result.success) {
|
||||
if (result.canceled) {
|
||||
return
|
||||
}
|
||||
throw new Error(result.error || 'Download failed')
|
||||
}
|
||||
} finally {
|
||||
cleanup?.()
|
||||
}
|
||||
}
|
||||
|
||||
3
src/types/electron.d.ts
vendored
3
src/types/electron.d.ts
vendored
@@ -14,6 +14,7 @@ export interface ElectronAPI {
|
||||
onRemoteClipboardSyncToRemote: (callback: () => void) => () => void
|
||||
onRemoteClipboardSyncFromRemote: (callback: () => void) => () => void
|
||||
onRemoteClipboardAutoSync: (callback: (text: string) => void) => () => void
|
||||
onDownloadProgress: (callback: (data: { progress: number; id: string }) => void) => () => void
|
||||
clipboardReadText: () => Promise<{ success: boolean; text?: string; error?: string }>
|
||||
clipboardWriteText: (text: string) => Promise<{ success: boolean; error?: string }>
|
||||
remoteFetchDrives: (serverHost: string, port: number, password?: string) => Promise<{
|
||||
@@ -30,7 +31,7 @@ export interface ElectronAPI {
|
||||
success: boolean
|
||||
error?: string
|
||||
}>
|
||||
remoteDownloadFile: (serverHost: string, port: number, fileName: string, remotePath: string, localPath: string, password?: string) => Promise<{
|
||||
remoteDownloadFile: (id: string, serverHost: string, port: number, fileName: string, remotePath: string, localPath: string, password?: string) => Promise<{
|
||||
success: boolean
|
||||
filePath?: string
|
||||
error?: string
|
||||
|
||||
Reference in New Issue
Block a user