From 78f3123ddd05ece619e2c1569c9305ffccbbaa87 Mon Sep 17 00:00:00 2001 From: eeee0717 Date: Wed, 26 Mar 2025 15:53:12 +0800 Subject: [PATCH] =?UTF-8?q?feat(OcrProvider):=20=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/ipc.ts | 2 +- src/main/ocr/BaseOcrProvider.ts | 30 ++++++++++++++++++++++++++++++ src/main/ocr/MacSysOcrProvider.ts | 7 ++++--- src/main/ocr/MistralOcrProvider.ts | 3 +-- src/main/services/FileStorage.ts | 3 +++ 5 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/main/ipc.ts b/src/main/ipc.ts index 2b0b738f9..c08551503 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -17,8 +17,8 @@ import FileService from './services/FileService' import FileStorage from './services/FileStorage' import KnowledgeService from './services/KnowledgeService' import MCPService from './services/MCPService' -import ObsidianVaultService from './services/ObsidianVaultService' import * as NutstoreService from './services/NutstoreService' +import ObsidianVaultService from './services/ObsidianVaultService' import { ProxyConfig, proxyManager } from './services/ProxyManager' import { registerShortcuts, unregisterAllShortcuts } from './services/ShortcutService' import { TrayService } from './services/TrayService' diff --git a/src/main/ocr/BaseOcrProvider.ts b/src/main/ocr/BaseOcrProvider.ts index c7992b141..1aba42ebe 100644 --- a/src/main/ocr/BaseOcrProvider.ts +++ b/src/main/ocr/BaseOcrProvider.ts @@ -1,11 +1,15 @@ import fs from 'node:fs' +import path from 'node:path' import { windowService } from '@main/services/WindowService' import { FileSource, LocalFileSource, OcrProvider } from '@types' +import { app } from 'electron' import Logger from 'electron-log' import pdfParse from 'pdf-parse' export default abstract class BaseOcrProvider { protected provider: OcrProvider + private storageDir = path.join(app.getPath('userData'), 'Data', 'Files') + constructor(provider: OcrProvider) { if (!provider) { throw new Error('Ocr provider is not set') @@ -56,4 +60,30 @@ export default abstract class BaseOcrProvider { progress: progress }) } + + /** + * 将文件移动到附件目录 + * @param fileId 文件id + * @param filePaths 需要移动的文件路径数组 + * @returns 移动后的文件路径数组 + */ + public moveToAttachmentsDir(fileId: string, filePaths: string[]): string[] { + const attachmentsPath = path.join(this.storageDir, fileId) + if (!fs.existsSync(attachmentsPath)) { + fs.mkdirSync(attachmentsPath, { recursive: true }) + } + + const movedPaths: string[] = [] + + for (const filePath of filePaths) { + if (fs.existsSync(filePath)) { + const fileName = path.basename(filePath) + const destPath = path.join(attachmentsPath, fileName) + fs.copyFileSync(filePath, destPath) + fs.unlinkSync(filePath) // 删除原文件,实现"移动" + movedPaths.push(destPath) + } + } + return movedPaths + } } diff --git a/src/main/ocr/MacSysOcrProvider.ts b/src/main/ocr/MacSysOcrProvider.ts index be8eb7b7b..cbb15f6ad 100644 --- a/src/main/ocr/MacSysOcrProvider.ts +++ b/src/main/ocr/MacSysOcrProvider.ts @@ -69,14 +69,15 @@ export default class MacSysOcrProvider extends BaseOcrProvider { }) writeStream.on('error', reject) }) - + const movedPaths = this.moveToAttachmentsDir(file.id, [txtFilePath]) + console.log('movedPaths', movedPaths) return { processedFile: { ...file, name: txtFileName, - path: txtFilePath, + path: movedPaths[0], ext: '.txt', - size: fs.statSync(txtFilePath).size + size: fs.statSync(movedPaths[0]).size } } } catch (error) { diff --git a/src/main/ocr/MistralOcrProvider.ts b/src/main/ocr/MistralOcrProvider.ts index 307c9ef79..4eed7ee3e 100644 --- a/src/main/ocr/MistralOcrProvider.ts +++ b/src/main/ocr/MistralOcrProvider.ts @@ -9,7 +9,6 @@ import { OCRResponse } from '@mistralai/mistralai/models/components/ocrresponse' import { FileSource, FileTypes, isLocalFile, LocalFileSource, OcrProvider } from '@types' import Logger from 'electron-log' import path from 'path' -import { v4 as uuidv4 } from 'uuid' import BaseOcrProvider from './BaseOcrProvider' @@ -109,7 +108,7 @@ export default class MistralOcrProvider extends BaseOcrProvider { private convertFile(result: OCRResponse, file: FileSource): LocalFileSource { // Create a unique directory for this conversion to store images - const conversionId = uuidv4() + const conversionId = file.id let outputPath = '' let outputFileName = '' if (isLocalFile(file)) { diff --git a/src/main/services/FileStorage.ts b/src/main/services/FileStorage.ts index e70544019..6e040f701 100644 --- a/src/main/services/FileStorage.ts +++ b/src/main/services/FileStorage.ts @@ -212,6 +212,9 @@ class FileStorage { } public deleteFile = async (_: Electron.IpcMainInvokeEvent, id: string): Promise => { + if (!fs.existsSync(path.join(this.storageDir, id))) { + return + } await fs.promises.unlink(path.join(this.storageDir, id)) }