Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
refactor(ocr): get rid of unused routes and services
Elian Doran72dff8838496
diff --git a/apps/server/src/routes/api/ocr.ts b/apps/server/src/routes/api/ocr.tsindex 314fc811fa..65618b7db8 100644--- a/apps/server/src/routes/api/ocr.ts+++ b/apps/server/src/routes/api/ocr.ts@@ -125,41 +125,6 @@ async function processAttachmentOCR(req: Request<{ attachmentId: string }>) {return { success: true, result };}-/**- * @swagger- * /api/ocr/search:- * get:- * summary: Search for text in OCR results- * operationId: ocr-search- * parameters:- * - name: q- * in: query- * required: true- * schema:- * type: string- * description: Search query text- * responses:- * '200':- * description: Search results- * '400':- * description: Bad request - missing search query- * '500':- * description: Internal server error- * security:- * - session: []- * tags: ["ocr"]- */-async function searchOCR(req: Request) {- const { q: searchText } = req.query;-- if (!searchText || typeof searchText !== 'string') {- return [400, { success: false, message: 'Search query is required' }];- }-- const results = ocrService.searchOCRResults(searchText);- return { success: true, results };-}-/*** @swagger* /api/ocr/batch-process:@@ -204,56 +169,6 @@ async function getBatchProgress() {return ocrService.getBatchProgress();}-/**- * @swagger- * /api/ocr/stats:- * get:- * summary: Get OCR processing statistics- * operationId: ocr-get-stats- * responses:- * '200':- * description: OCR statistics- * '500':- * description: Internal server error- * security:- * - session: []- * tags: ["ocr"]- */-async function getOCRStats() {- return { success: true, stats: ocrService.getOCRStats() };-}--/**- * @swagger- * /api/ocr/delete/{blobId}:- * delete:- * summary: Delete OCR results for a specific blob- * operationId: ocr-delete-results- * parameters:- * - name: blobId- * in: path- * required: true- * schema:- * type: string- * description: ID of the blob- * responses:- * '200':- * description: OCR results deleted successfully- * '400':- * description: Bad request - invalid parameters- * '500':- * description: Internal server error- * security:- * - session: []- * tags: ["ocr"]- */-async function deleteOCRResults(req: Request<{ blobId: string }>) {- const { blobId } = req.params;-- ocrService.deleteOCRResult(blobId);- return { success: true, message: `OCR results deleted for blob ${blobId}` };-}-/*** @swagger* /api/ocr/notes/{noteId}/text:@@ -319,11 +234,8 @@ async function getAttachmentOCRText(req: Request<{ attachmentId: string }>) {export default {processNoteOCR,processAttachmentOCR,- searchOCR,batchProcessOCR,getBatchProgress,- getOCRStats,- deleteOCRResults,getNoteOCRText,getAttachmentOCRText};diff --git a/apps/server/src/routes/routes.ts b/apps/server/src/routes/routes.tsindex b05c124f17..775a36e833 100644--- a/apps/server/src/routes/routes.ts+++ b/apps/server/src/routes/routes.ts@@ -380,11 +380,8 @@ function register(app: express.Application) {// OCR APIasyncApiRoute(PST, "/api/ocr/process-note/:noteId", ocrRoute.processNoteOCR);asyncApiRoute(PST, "/api/ocr/process-attachment/:attachmentId", ocrRoute.processAttachmentOCR);- asyncApiRoute(GET, "/api/ocr/search", ocrRoute.searchOCR);asyncApiRoute(PST, "/api/ocr/batch-process", ocrRoute.batchProcessOCR);asyncApiRoute(GET, "/api/ocr/batch-progress", ocrRoute.getBatchProgress);- asyncApiRoute(GET, "/api/ocr/stats", ocrRoute.getOCRStats);- asyncApiRoute(DEL, "/api/ocr/delete/:blobId", ocrRoute.deleteOCRResults);asyncApiRoute(GET, "/api/ocr/notes/:noteId/text", ocrRoute.getNoteOCRText);asyncApiRoute(GET, "/api/ocr/attachments/:attachmentId/text", ocrRoute.getAttachmentOCRText);diff --git a/apps/server/src/services/ocr/ocr_service.ts b/apps/server/src/services/ocr/ocr_service.tsindex 731ceb71ab..12ab5d034a 100644--- a/apps/server/src/services/ocr/ocr_service.ts+++ b/apps/server/src/services/ocr/ocr_service.ts@@ -27,17 +27,11 @@ export interface OCRProcessingOptions {enablePDFTextExtraction?: boolean;}-interface OCRBlobRow {- blobId: string;- textRepresentation: string;-}-/*** OCR Service for extracting text from images and other OCR-able objects* Uses Tesseract.js for text recognition*/class OCRService {- private isProcessing = false;private processors: Map<string, FileProcessor> = new Map();constructor() {@@ -116,27 +110,17 @@ class OCRService {* Extract text from file buffer using appropriate processor*/async extractTextFromFile(fileBuffer: Buffer, mimeType: string, options: OCRProcessingOptions = {}): Promise<OCRResult> {- try {- log.info(`Starting OCR text extraction for MIME type: ${mimeType} with language: ${options.language || "eng"}`);- this.isProcessing = true;+ log.info(`Starting OCR text extraction for MIME type: ${mimeType} with language: ${options.language || "eng"}`);- // Find appropriate processor- const processor = this.getProcessorForMimeType(mimeType);- if (!processor) {- throw new Error(`No processor found for MIME type: ${mimeType}`);- }-- const result = await processor.extractText(fileBuffer, options);+ const processor = this.getProcessorForMimeType(mimeType);+ if (!processor) {+ throw new Error(`No processor found for MIME type: ${mimeType}`);+ }- log.info(`OCR extraction completed. Confidence: ${Math.round(result.confidence * 100)}%, Text length: ${result.text.length}`);- return result;+ const result = await processor.extractText(fileBuffer, options);- } catch (error) {- log.error(`OCR text extraction failed: ${error}`);- throw error;- } finally {- this.isProcessing = false;- }+ log.info(`OCR extraction completed. Confidence: ${Math.round(result.confidence * 100)}%, Text length: ${result.text.length}`);+ return result;}/**@@ -268,102 +252,6 @@ class OCRService {return !!row?.textRepresentation;}- /**- * Search for text in OCR results- */- searchOCRResults(searchText: string): Array<{ blobId: string; text: string }> {- try {- const query = `- SELECT blobId, textRepresentation- FROM blobs- WHERE textRepresentation LIKE ?- AND textRepresentation IS NOT NULL- `;- const params = [`%${searchText}%`];-- const rows = sql.getRows<OCRBlobRow>(query, params);-- return rows.map(row => ({- blobId: row.blobId,- text: row.textRepresentation- }));- } catch (error) {- log.error(`Failed to search OCR results: ${error}`);- return [];- }- }-- /**- * Delete OCR results for a blob- */- deleteOCRResult(blobId: string): void {- try {- sql.execute(`UPDATE blobs SET textRepresentation = NULL WHERE blobId = ?`, [blobId]);-- this.putBlobEntityChange(blobId);-- log.info(`Deleted OCR result for blob ${blobId}`);- } catch (error) {- log.error(`Failed to delete OCR result for blob ${blobId}: ${error}`);- throw error;- }- }-- /**- * Get OCR statistics- */- getOCRStats(): { totalProcessed: number; imageNotes: number; imageAttachments: number } {- try {- const stats = sql.getRow<{- total_processed: number;- }>(`- SELECT COUNT(*) as total_processed- FROM blobs- WHERE textRepresentation IS NOT NULL AND textRepresentation != ''- `);-- // Count image notes with OCR- const noteStats = sql.getRow<{- count: number;- }>(`- SELECT COUNT(*) as count- FROM notes n- JOIN blobs b ON n.blobId = b.blobId- WHERE n.type = 'image'- AND n.isDeleted = 0- AND b.textRepresentation IS NOT NULL AND b.textRepresentation != ''- `);-- // Count image attachments with OCR- const attachmentStats = sql.getRow<{- count: number;- }>(`- SELECT COUNT(*) as count- FROM attachments a- JOIN blobs b ON a.blobId = b.blobId- WHERE a.role = 'image'- AND a.isDeleted = 0- AND b.textRepresentation IS NOT NULL AND b.textRepresentation != ''- `);-- return {- totalProcessed: stats?.total_processed || 0,- imageNotes: noteStats?.count || 0,- imageAttachments: attachmentStats?.count || 0- };- } catch (error) {- log.error(`Failed to get OCR stats: ${error}`);- return { totalProcessed: 0, imageNotes: 0, imageAttachments: 0 };- }- }-- /**- * Check if currently processing- */- isCurrentlyProcessing(): boolean {- return this.isProcessing;- }-// Batch processing stateprivate batchProcessingState: {inProgress: boolean;