Skip to content

Build your personal knowledge base with Trilium Notes

chore(ocr): remove last extraction date

Elian Doran60a26219282b
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 8f11a032ab..a11334dd5c 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -2090,7 +2090,6 @@
"no_text_available": "No OCR text available",
"no_text_explanation": "This note has not been processed for OCR text extraction or no text was found.",
"failed_to_load": "Failed to load OCR text",
- "extracted_on": "Extracted on: {{date}}",
"process_now": "Process OCR",
"processing": "Processing...",
"processing_started": "OCR processing has been started. Please wait a moment and refresh.",
diff --git a/apps/client/src/widgets/type_widgets/ReadOnlyTextRepresentation.tsx b/apps/client/src/widgets/type_widgets/ReadOnlyTextRepresentation.tsx
index 5ad2f83f88..9eaec33f4c 100644
--- a/apps/client/src/widgets/type_widgets/ReadOnlyTextRepresentation.tsx
+++ b/apps/client/src/widgets/type_widgets/ReadOnlyTextRepresentation.tsx
@@ -6,12 +6,11 @@ import { useEffect, useState } from "preact/hooks";
import { t } from "../../services/i18n";
import server from "../../services/server";
import toast from "../../services/toast";
-import { formatDateTime } from "../../services/utils";
import { TypeWidgetProps } from "./type_widget";
type State =
| { kind: "loading" }
- | { kind: "loaded"; text: string; extractedAt: string | null }
+ | { kind: "loaded"; text: string }
| { kind: "empty" }
| { kind: "error"; message: string };
@@ -35,7 +34,7 @@ export default function ReadOnlyTextRepresentation({ note }: TypeWidgetProps) {
return;
}
- setState({ kind: "loaded", text: response.text, extractedAt: response.extractedAt });
+ setState({ kind: "loaded", text: response.text });
} catch (error: any) {
console.error("Error loading text representation:", error);
setState({ kind: "error", message: error.message || t("ocr.failed_to_load") });
@@ -78,11 +77,6 @@ export default function ReadOnlyTextRepresentation({ note }: TypeWidgetProps) {
<div className="text-representation-content">
{state.text}
</div>
- {state.extractedAt && (
- <div className="text-representation-meta">
- {t("ocr.extracted_on", { date: formatDateTime(new Date(state.extractedAt)) })}
- </div>
- )}
</>
)}
diff --git a/apps/server/src/assets/db/schema.sql b/apps/server/src/assets/db/schema.sql
index 106353e159..27e38bf205 100644
--- a/apps/server/src/assets/db/schema.sql
+++ b/apps/server/src/assets/db/schema.sql
@@ -108,7 +108,6 @@ CREATE TABLE IF NOT EXISTS "blobs" (
`blobId` TEXT NOT NULL,
`content` TEXT NULL DEFAULT NULL,
`textRepresentation` TEXT DEFAULT NULL,
- `textExtractionLastProcessed` TEXT DEFAULT NULL,
`dateModified` TEXT NOT NULL,
`utcDateModified` TEXT NOT NULL,
PRIMARY KEY(`blobId`)
diff --git a/apps/server/src/migrations/migrations.ts b/apps/server/src/migrations/migrations.ts
index a938395384..f3987b0568 100644
--- a/apps/server/src/migrations/migrations.ts
+++ b/apps/server/src/migrations/migrations.ts
@@ -6,23 +6,16 @@
// Migrations should be kept in descending order, so the latest migration is first.
const MIGRATIONS: (SqlMigration | JsMigration)[] = [
- // Add text representation column and last processed timestamp to blobs table
+ // Add text representation column to blobs table
{
version: 236,
sql: /*sql*/`\
-- Add text representation column to blobs table
ALTER TABLE blobs ADD COLUMN textRepresentation TEXT DEFAULT NULL;
- -- Add OCR last processed timestamp to blobs table
- ALTER TABLE blobs ADD COLUMN textExtractionLastProcessed TEXT DEFAULT NULL;
-
-- Create index for text representation searches
CREATE INDEX IF NOT EXISTS idx_blobs_textRepresentation
ON blobs (textRepresentation);
-
- -- Create index for OCR last processed timestamp
- CREATE INDEX IF NOT EXISTS idx_blobs_textExtractionLastProcessed
- ON blobs (textExtractionLastProcessed);
`
},
// Add missing database indices for query performance
diff --git a/apps/server/src/routes/api/ocr.ts b/apps/server/src/routes/api/ocr.ts
index 96188f4b65..2b2b7a4658 100644
--- a/apps/server/src/routes/api/ocr.ts
+++ b/apps/server/src/routes/api/ocr.ts
@@ -283,29 +283,25 @@ async function getNoteOCRText(req: Request<{ noteId: string }>) {
}
let ocrText: string | null = null;
- let extractedAt: string | null = null;
if (note.blobId) {
const result = sql.getRow<{
textRepresentation: string | null;
- textExtractionLastProcessed: string | null;
}>(`
- SELECT textRepresentation, textExtractionLastProcessed
+ SELECT textRepresentation
FROM blobs
WHERE blobId = ?
`, [note.blobId]);
if (result) {
ocrText = result.textRepresentation;
- extractedAt = result.textExtractionLastProcessed;
}
}
return {
success: true,
text: ocrText || '',
- hasOcr: !!ocrText,
- extractedAt
+ hasOcr: !!ocrText
} satisfies TextRepresentationResponse;
}
diff --git a/apps/server/src/services/ocr/ocr_service.ts b/apps/server/src/services/ocr/ocr_service.ts
index 3e39ed6e9c..5284002367 100644
--- a/apps/server/src/services/ocr/ocr_service.ts
+++ b/apps/server/src/services/ocr/ocr_service.ts
@@ -31,7 +31,6 @@ export interface OCRProcessingOptions {
interface OCRBlobRow {
blobId: string;
textRepresentation: string;
- textExtractionLastProcessed?: string;
}
/**
@@ -189,11 +188,13 @@ class OCRService {
return null;
}
- // Check if OCR already exists and is up-to-date
- const existingOCR = this.getStoredOCRResult(note.blobId);
- if (existingOCR && !options.forceReprocess && note.blobId && !this.needsReprocessing(note.blobId)) {
- log.info(`OCR already exists and is up-to-date for note ${noteId}, returning cached result`);
- return existingOCR;
+ // Check if OCR already exists
+ if (!options.forceReprocess) {
+ const existingOCR = this.getStoredOCRResult(note.blobId);
+ if (existingOCR) {
+ log.info(`OCR already exists for note ${noteId}, returning cached result`);
+ return existingOCR;
+ }
}
try {
@@ -243,11 +244,13 @@ class OCRService {
return null;
}
- // Check if OCR already exists and is up-to-date
- const existingOCR = this.getStoredOCRResult(attachment.blobId);
- if (existingOCR && !options.forceReprocess && attachment.blobId && !this.needsReprocessing(attachment.blobId)) {
- log.info(`OCR already exists and is up-to-date for attachment ${attachmentId}, returning cached result`);
- return existingOCR;
+ // Check if OCR already exists
+ if (!options.forceReprocess) {
+ const existingOCR = this.getStoredOCRResult(attachment.blobId);
+ if (existingOCR) {
+ log.info(`OCR already exists for attachment ${attachmentId}, returning cached result`);
+ return existingOCR;
+ }
}
try {
@@ -280,11 +283,9 @@ class OCRService {
try {
sql.execute(`
- UPDATE blobs SET
- textRepresentation = ?,
- textExtractionLastProcessed = ?
+ UPDATE blobs SET textRepresentation = ?
WHERE blobId = ?
- `, [ocrResult.text, new Date().toISOString(), blobId]);
+ `, [ocrResult.text, blobId]);
this.putBlobEntityChange(blobId);
@@ -361,10 +362,7 @@ class OCRService {
*/
deleteOCRResult(blobId: string): void {
try {
- sql.execute(`
- UPDATE blobs SET textRepresentation = NULL, textExtractionLastProcessed = NULL
- WHERE blobId = ?
- `, [blobId]);
+ sql.execute(`UPDATE blobs SET textRepresentation = NULL WHERE blobId = ?`, [blobId]);
this.putBlobEntityChange(blobId);
@@ -618,68 +616,7 @@ class OCRService {
}
/**
- * Check if blob needs OCR re-processing due to content changes
- */
- needsReprocessing(blobId: string): boolean {
- if (!blobId) {
- return false;
- }
-
- try {
- const blobInfo = sql.getRow<{
- utcDateModified: string;
- textExtractionLastProcessed: string | null;
- }>(`
- SELECT utcDateModified, textExtractionLastProcessed
- FROM blobs
- WHERE blobId = ?
- `, [blobId]);
-
- if (!blobInfo) {
- return false;
- }
-
- // If OCR was never processed, it needs processing
- if (!blobInfo.textExtractionLastProcessed) {
- return true;
- }
-
- // If blob was modified after last OCR processing, it needs re-processing
- const blobModified = new Date(blobInfo.utcDateModified);
- const lastOcrProcessed = new Date(blobInfo.textExtractionLastProcessed);
-
- return blobModified > lastOcrProcessed;
- } catch (error) {
- log.error(`Failed to check if blob ${blobId} needs reprocessing: ${error}`);
- return false;
- }
- }
-
- /**
- * Invalidate OCR results for a blob (clear textRepresentation and textExtractionLastProcessed)
- */
- invalidateOCRResult(blobId: string): void {
- if (!blobId) {
- return;
- }
-
- try {
- sql.execute(`
- UPDATE blobs SET textRepresentation = NULL, textExtractionLastProcessed = NULL
- WHERE blobId = ?
- `, [blobId]);
-
- this.putBlobEntityChange(blobId);
-
- log.info(`Invalidated OCR result for blob ${blobId}`);
- } catch (error) {
- log.error(`Failed to invalidate OCR result for blob ${blobId}: ${error}`);
- throw error;
- }
- }
-
- /**
- * Get blobs that need OCR processing (modified after last OCR or never processed)
+ * Get blobs that need OCR processing (those without text representation)
*/
getBlobsNeedingOCR(): Array<{ blobId: string; mimeType: string; entityType: 'note' | 'attachment'; entityId: string }> {
try {
@@ -717,10 +654,7 @@ class OCRService {
)
AND n.isDeleted = 0
AND n.blobId IS NOT NULL
- AND (
- b.textExtractionLastProcessed IS NULL
- OR b.utcDateModified > b.textExtractionLastProcessed
- )
+ AND b.textRepresentation IS NULL
`);
// Get attachments with blobs that need OCR (both image and file attachments with supported MIME types)
@@ -757,10 +691,7 @@ class OCRService {
)
AND a.isDeleted = 0
AND a.blobId IS NOT NULL
- AND (
- b.textExtractionLastProcessed IS NULL
- OR b.utcDateModified > b.textExtractionLastProcessed
- )
+ AND b.textRepresentation IS NULL
`);
// Combine results
diff --git a/packages/commons/src/lib/server_api.ts b/packages/commons/src/lib/server_api.ts
index 293bfa47bf..48abb76404 100644
--- a/packages/commons/src/lib/server_api.ts
+++ b/packages/commons/src/lib/server_api.ts
@@ -292,7 +292,6 @@ export interface TextRepresentationResponse {
success: boolean;
text: string;
hasOcr: boolean;
- extractedAt: string | null;
message?: string;
}