From bcc1046cdf0f430874880a53aa6e728f92e6abce Mon Sep 17 00:00:00 2001 From: Chen Tao <70054568+eeee0717@users.noreply.github.com> Date: Thu, 10 Jul 2025 22:06:09 +0800 Subject: [PATCH] feat: add upload file (#8035) --- packages/shared/IpcChannel.ts | 1 + src/main/ipc.ts | 1 + src/main/services/FileStorage.ts | 13 +++++++++++++ src/preload/index.ts | 3 ++- src/renderer/src/hooks/useKnowledge.ts | 3 ++- .../src/pages/knowledge/items/KnowledgeFiles.tsx | 8 +++++--- 6 files changed, 24 insertions(+), 5 deletions(-) diff --git a/packages/shared/IpcChannel.ts b/packages/shared/IpcChannel.ts index 78208133d5..b970a6507a 100644 --- a/packages/shared/IpcChannel.ts +++ b/packages/shared/IpcChannel.ts @@ -147,6 +147,7 @@ export enum IpcChannel { File_Base64File = 'file:base64File', File_GetPdfInfo = 'file:getPdfInfo', Fs_Read = 'fs:read', + File_OpenWithRelativePath = 'file:openWithRelativePath', // file service FileService_Upload = 'file-service:upload', diff --git a/src/main/ipc.ts b/src/main/ipc.ts index be160c8d2a..7ccbc54ba4 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -399,6 +399,7 @@ export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) { ipcMain.handle(IpcChannel.File_Download, fileManager.downloadFile) ipcMain.handle(IpcChannel.File_Copy, fileManager.copyFile) ipcMain.handle(IpcChannel.File_BinaryImage, fileManager.binaryImage) + ipcMain.handle(IpcChannel.File_OpenWithRelativePath, fileManager.openFileWithRelativePath) // file service ipcMain.handle(IpcChannel.FileService_Upload, async (_, provider: Provider, file: FileMetadata) => { diff --git a/src/main/services/FileStorage.ts b/src/main/services/FileStorage.ts index baa94f535a..87d465e5f4 100644 --- a/src/main/services/FileStorage.ts +++ b/src/main/services/FileStorage.ts @@ -417,6 +417,19 @@ class FileStorage { shell.openPath(path).catch((err) => logger.error('[IPC - Error] Failed to open file:', err)) } + /** + * 通过相对路径打开文件,跨设备时使用 + * @param file + */ + public openFileWithRelativePath = async (_: Electron.IpcMainInvokeEvent, file: FileMetadata): Promise => { + const filePath = path.join(this.storageDir, file.name) + if (fs.existsSync(filePath)) { + shell.openPath(filePath).catch((err) => logger.error('[IPC - Error] Failed to open file:', err)) + } else { + logger.warn('[IPC - Warning] File does not exist:', filePath) + } + } + public save = async ( _: Electron.IpcMainInvokeEvent, fileName: string, diff --git a/src/preload/index.ts b/src/preload/index.ts index 5221761dfb..8fc104b68e 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -146,7 +146,8 @@ const api = { copy: (fileId: string, destPath: string) => ipcRenderer.invoke(IpcChannel.File_Copy, fileId, destPath), base64File: (fileId: string) => ipcRenderer.invoke(IpcChannel.File_Base64File, fileId), pdfInfo: (fileId: string) => ipcRenderer.invoke(IpcChannel.File_GetPdfInfo, fileId), - getPathForFile: (file: File) => webUtils.getPathForFile(file) + getPathForFile: (file: File) => webUtils.getPathForFile(file), + openFileWithRelativePath: (file: FileMetadata) => ipcRenderer.invoke(IpcChannel.File_OpenWithRelativePath, file) }, fs: { read: (pathOrUrl: string, encoding?: BufferEncoding) => ipcRenderer.invoke(IpcChannel.Fs_Read, pathOrUrl, encoding) diff --git a/src/renderer/src/hooks/useKnowledge.ts b/src/renderer/src/hooks/useKnowledge.ts index 9d893da5b4..8008930bb3 100644 --- a/src/renderer/src/hooks/useKnowledge.ts +++ b/src/renderer/src/hooks/useKnowledge.ts @@ -145,7 +145,8 @@ export const useKnowledge = (baseId: string) => { } } if (item.type === 'file' && typeof item.content === 'object') { - await window.api.file.deleteDir(item.content.id) + // name: eg. text.pdf + await window.api.file.delete(item.content.name) } } // 刷新项目 diff --git a/src/renderer/src/pages/knowledge/items/KnowledgeFiles.tsx b/src/renderer/src/pages/knowledge/items/KnowledgeFiles.tsx index 19021b161e..8ac29817be 100644 --- a/src/renderer/src/pages/knowledge/items/KnowledgeFiles.tsx +++ b/src/renderer/src/pages/knowledge/items/KnowledgeFiles.tsx @@ -3,6 +3,7 @@ import Ellipsis from '@renderer/components/Ellipsis' import { useKnowledge } from '@renderer/hooks/useKnowledge' import FileItem from '@renderer/pages/files/FileItem' import StatusIcon from '@renderer/pages/knowledge/components/StatusIcon' +import FileManager from '@renderer/services/FileManager' import { getProviderName } from '@renderer/services/ProviderService' import { FileMetadata, FileType, FileTypes, KnowledgeBase, KnowledgeItem } from '@renderer/types' import { formatFileSize, uuid } from '@renderer/utils' @@ -111,8 +112,9 @@ const KnowledgeFiles: FC = ({ selectedBase, progressMap, } }) .filter(({ ext }) => fileTypes.includes(ext)) - // const uploadedFiles = await FileManager.uploadFiles(_files) - addFiles(_files) + const uploadedFiles = await FileManager.uploadFiles(_files) + console.log('uploadedFiles', uploadedFiles) + addFiles(uploadedFiles) } } @@ -172,7 +174,7 @@ const KnowledgeFiles: FC = ({ selectedBase, progressMap, key={item.id} fileInfo={{ name: ( - window.api.file.openPath(file.path)}> + window.api.file.openFileWithRelativePath(file)}> {file.origin_name}