feat(OcrProvider): 统一文件格式
This commit is contained in:
+1
-1
@@ -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'
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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)) {
|
||||
|
||||
@@ -212,6 +212,9 @@ class FileStorage {
|
||||
}
|
||||
|
||||
public deleteFile = async (_: Electron.IpcMainInvokeEvent, id: string): Promise<void> => {
|
||||
if (!fs.existsSync(path.join(this.storageDir, id))) {
|
||||
return
|
||||
}
|
||||
await fs.promises.unlink(path.join(this.storageDir, id))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user