The minimalistic document archiving platform.
feat(extractors): add image extractor
Corentin Thomassetf97e5f863e86
diff --git a/.gitignore b/.gitignoreindex ed3cd98b..c05e8edb 100644--- a/.gitignore+++ b/.gitignore@@ -30,3 +30,4 @@ logscoveragecache.zed+*.traineddata\ No newline at end of filediff --git a/fixtures/006.expected b/fixtures/006.expectednew file mode 100644index 00000000..0baa277a--- /dev/null+++ b/fixtures/006.expected@@ -0,0 +1,6 @@+Lorem ipsum++Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sapien ante conubia vestibulum+ultrices quisque nam nascetur consectetur. Viverra amet lacinia massa donec gravida primis+leo tellus. Montes nulla sit cras odio penatibus cum aenean metus. Per per eros fusce et+platea et feugiat ullamcorper.diff --git a/fixtures/006.png b/fixtures/006.pngnew file mode 100644index 00000000..929cebb1Binary files /dev/null and b/fixtures/006.png differdiff --git a/fixtures/007.expected b/fixtures/007.expectednew file mode 100644index 00000000..0fd0daf5--- /dev/null+++ b/fixtures/007.expected@@ -0,0 +1,41 @@+at his touch of a certain icy pang along my blood. “Come, sir,” said I.+“You forget that I have not yet the pleasure of your acquaintance. Be+seated, if you please.” And I showed him an example, and sat down+myself in my customary seat and with as fair an imitation of my or-+dinary manner to a patient, as the lateness of the hour, the nature of+my preoccupations, and the horror I had of my visitor, would suffer+me to muster.++“I beg your pardon, Dr. Lanyon,” he replied civilly enough. “What+you say is very well founded; and my impatience has shown its heels+to my politeness. I come here at the instance of your colleague, Dr.+Henry Jekyll, on a piece of business of some moment; and I under-+stood...” He paused and put his hand to his throat, and I could see,+in spite of his collected manner, that he was wrestling against the+approaches of the hysteria—“T understood, a drawer...”++But here I took pity on my visitor's suspense, and some perhaps+on my own growing curiosity.++“There it is, sir,” said I, pointing to the drawer, where it lay on the+floor behind a table and still covered with the sheet.++He sprang to it, and then paused, and laid his hand upon his+heart: I could hear his teeth grate with the convulsive action of his+jaws; and his face was so ghastly to see that I grew alarmed both for+his life and reason.++“Compose yourself,” said I.++He turned a dreadful smile to me, and as if with the decision of+despair, plucked away the sheet. At sight of the contents, he uttered+one loud sob of such immense relief that I sat petrified. And the+next moment, in a voice that was already fairly well under control,+“Have you a graduated glass?” he asked.++I rose from my place with something of an effort and gave him+what he asked.++He thanked me with a smiling nod, measured out a few min-+ims of the red tincture and added one of the powders. The mix-+ture, which was at first of a reddish hue, began, in proportion as thediff --git a/fixtures/007.jpg b/fixtures/007.jpgnew file mode 100644index 00000000..32743710Binary files /dev/null and b/fixtures/007.jpg differdiff --git a/fixtures/008.expected b/fixtures/008.expectednew file mode 100644index 00000000..0baa277a--- /dev/null+++ b/fixtures/008.expected@@ -0,0 +1,6 @@+Lorem ipsum++Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sapien ante conubia vestibulum+ultrices quisque nam nascetur consectetur. Viverra amet lacinia massa donec gravida primis+leo tellus. Montes nulla sit cras odio penatibus cum aenean metus. Per per eros fusce et+platea et feugiat ullamcorper.diff --git a/fixtures/008.gif b/fixtures/008.gifnew file mode 100644index 00000000..fc9a566bBinary files /dev/null and b/fixtures/008.gif differdiff --git a/package.json b/package.jsonindex 7e354122..7f566c5d 100644--- a/package.json+++ b/package.json@@ -46,6 +46,7 @@"release": "bumpp --commit --tag --push"},"dependencies": {+ "tesseract.js": "^6.0.0","unpdf": "^0.12.1"},"devDependencies": {diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 9760802e..51edc59d 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -8,6 +8,9 @@ importers:.:dependencies:+ tesseract.js:+ specifier: ^6.0.0+ version: 6.0.0unpdf:specifier: ^0.12.1version: 0.12.1@@ -829,6 +832,9 @@ packages:resolution: {integrity: sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==}+ resolution: {integrity: sha512-vHdS19CnY3hwiNdkaqk93DvjVLfbEcI8mys4UjuWrlX1haDmroo8o4xCzh4wD6DGV6HxRCyauwhHRqMTfERtjw==}+resolution: {integrity: sha512-JZOSA7Mo9sNGB8+UjSgzdLtokWAky1zbztM3WRLCbZ70/3cTANmQmOdR7y2g+J0e2WXywy1yS468tY+IruqEww==}@@ -1503,6 +1509,9 @@ packages:resolution: {integrity: sha512-AXcZb6vzzrFAUE61HnN4mpLqd/cSIwNQjtNWR0euPm6y0iqx3G4gOXaIDdtdDwZmhwe82LA6+zinmW4UBWVePQ==}engines: {node: '>=16.17.0'}+ resolution: {integrity: sha512-8Sb3veuYCyrZL+VBt9LJfZjLUPWVvqn8tG28VqYNFCo43KHcKuq+b4EiXGeuaLAQWL2YmyDgMp2aSpH9JHsEQg==}+resolution: {integrity: sha512-hsBTNUqQTDwkWtcdYI2i06Y/nUBEsNEDJKjWdigLvegy8kDuJAS8uRlpkkcQpyEXL0Z/pjDy5HBmMjRCJ2gq+g==}engines: {node: '>= 4'}@@ -1563,6 +1572,9 @@ packages:resolution: {integrity: sha512-LnQR4bZ9IADDRSkvpqMGvt/tEJWclzklNgSw48V5EAaAeDd6qGvN8ei6k5p0tvxSR171VmGyHuTiAOfxAbr8kA==}engines: {node: ^12.20.0 || ^14.13.1 || >=16.0.0}+ resolution: {integrity: sha512-ITvGim8FhRiYe4IQ5uHSkj7pVaPDrCTkNd3yq3cV7iZAcJdHTUMPMEHcqSOy9xZ9qFenQCvi+2wjH9a1nXqHww==}+resolution: {integrity: sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==}@@ -1986,6 +1998,10 @@ packages:resolution: {integrity: sha512-1FlR+gjXK7X+AsAHso35MnyN5KqGwJRi/31ft6x0M194ht7S+rWAvd7PHss9xSKMzE0asv1pyIHaJYq+BbacAQ==}engines: {node: '>=12'}+ resolution: {integrity: sha512-8AV/sCtuzUeTo8gQK5qDZzARrulB3egtLzFgteqB2tcT4Mw7B8Kt7JcDHmltjz6FOAHsvTevk70gZEbhM4ZS9Q==}+ hasBin: true+resolution: {integrity: sha512-6IpQ7mKUxRcZNLIObR0hz7lxsapSSIYNZJwXPGeF0mTVqGKFIXj1DQcMoT22S3ROcLyY/rz0PWaWZ9ayWmad9g==}engines: {node: '>= 0.8.0'}@@ -2311,6 +2327,9 @@ packages:resolution: {integrity: sha512-J8rn6v4DBb2nnFqkqwy6/NnTYMcgLA+sLr0iIO41qpv0n+ngb7ksag2tMRl0inb1bbO/esUwzW1vbJi7K0sI0g==}engines: {node: ^12.0.0 || ^14.0.0 || >=16.0.0}+ resolution: {integrity: sha512-kY1AZVr2Ra+t+piVaJ4gxaFaReZVH40AKNo7UCX6W+dEwBo/2oZJzqfuN1qLq1oL45o56cPaTXELwrTh8Fpggg==}+resolution: {integrity: sha512-sZuz1dYW/ZsfG17WSAG7eS85r5a0dDsvg+7BiiYR5o6lKCAtUrEwdmRmaGF6rwVj3LcmAeYkOWKEPlbPzN3Y3A==}engines: {node: ^12.0.0 || ^14.0.0 || >=16.0.0}@@ -2513,6 +2532,12 @@ packages:resolution: {integrity: sha512-DZ4yORTwrbTj/7MZYq2w+/ZFdI6OZ/f9SFHR+71gIVUZhOQPHzVCLpvRnPgyaMpfWxxk/4ONva3GQSyNIKRv6A==}engines: {node: '>=10'}+ resolution: {integrity: sha512-1Qncm/9oKM7xgrQXZXNB+NRh19qiXGhxlrR8EwFbK5SaUbPZnS5OMtP/ghtqfd23hsr1ZvZbZjeuAGcMxd/ooA==}++ resolution: {integrity: sha512-tqYCod1HwJzkeZw1l6XWx+ly2hhisGcBtak9MArhYwDAxL0NgeVhLJcUjqPxZMQtpgtVUzWcpZPryi+hnaQGVw==}+resolution: {integrity: sha512-pFYqmTw68LXVjeWJMST4+borgQP2AyMNbg1BpZh9LbyhUeNkeaPF9gzfPGUAnSMV3qPYdWUwDIjjCLiSDOl7vg==}engines: {node: '>=18'}@@ -2705,6 +2730,9 @@ packages:peerDependencies:eslint: '>=6.0.0'+ resolution: {integrity: sha512-zksaLKM2fVlnB5jQQDqKXXwYHLQUVH9es+5TOOHwGOVJOCeRBCiPjwSg+3tN2AdTCzjgli4jijCH290kXb/zWQ==}+resolution: {integrity: sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==}@@ -2774,6 +2802,9 @@ packages:resolution: {integrity: sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==}engines: {node: '>=10'}+ resolution: {integrity: sha512-+J9RrgTKOmlxFSDHo0pI1xM6BLVUv+o0ZT9ANtCxGkjIVCCUdx9alUF8Gm+dGLKbkkkidWIHFDZHDMpfITt4+w==}+resolution: {integrity: sha512-bXE4cR/kVZhKZX/RjPEflHaKVhUVl85noU3v6b8apfQEc1x4A+zBxjZ4lN8LqGd6WZ3dl98pY4o717VFmoPp+A==}@@ -3559,6 +3590,8 @@ snapshots:+ [email protected]: {}+@@ -4385,6 +4418,8 @@ snapshots:+ [email protected]: {}+@@ -4433,6 +4468,8 @@ snapshots:+ [email protected]: {}+@@ -4945,7 +4982,6 @@ snapshots:dependencies:whatwg-url: 5.0.0- optional: true@@ -5002,6 +5038,8 @@ snapshots:dependencies:mimic-fn: 4.0.0+ [email protected]: {}+dependencies:deep-is: 0.1.4@@ -5305,6 +5343,8 @@ snapshots:dependencies:'@eslint-community/regexpp': 4.12.1+ [email protected]: {}+dependencies:'@eslint-community/regexpp': 4.12.1@@ -5521,6 +5561,22 @@ snapshots:mkdirp: 1.0.4yallist: 4.0.0+ [email protected]: {}++ dependencies:+ bmp-js: 0.1.0+ idb-keyval: 6.2.1+ is-url: 1.2.4+ node-fetch: 2.7.0+ opencollective-postinstall: 2.0.3+ regenerator-runtime: 0.13.11+ tesseract.js-core: 6.0.0+ wasm-feature-detect: 1.8.0+ zlibjs: 0.3.1+ transitivePeerDependencies:+ - encoding+dependencies:'@istanbuljs/schema': 0.1.3@@ -5550,8 +5606,7 @@ snapshots:dependencies:eslint-visitor-keys: 3.4.3- optional: true+ [email protected]: {}dependencies:@@ -5747,14 +5802,14 @@ snapshots:transitivePeerDependencies:- supports-color- optional: true+ [email protected]: {}++ [email protected]: {}dependencies:tr46: 0.0.3webidl-conversions: 3.0.1- optional: truedependencies:@@ -5817,4 +5872,6 @@ snapshots:+ [email protected]: {}+diff --git a/src/extractors.registry.ts b/src/extractors.registry.tsindex 7e99dcc6..ea5d7ef0 100644--- a/src/extractors.registry.ts+++ b/src/extractors.registry.ts@@ -1,10 +1,12 @@import type { ExtractorDefinition } from './extractors.models';+import { imageExtractorDefinition } from './extractors/img.extractor';import { pdfExtractorDefinition } from './extractors/pdf.extractor';import { txtExtractorDefinition } from './extractors/txt.extractor';export const extractorDefinitions: ExtractorDefinition[] = [pdfExtractorDefinition,txtExtractorDefinition,+ imageExtractorDefinition,];export function getExtractor({diff --git a/src/extractors/img.extractor.ts b/src/extractors/img.extractor.tsnew file mode 100644index 00000000..0638b18a--- /dev/null+++ b/src/extractors/img.extractor.ts@@ -0,0 +1,23 @@+import { Buffer } from 'node:buffer';+import { createWorker } from 'tesseract.js';+import { defineTextExtractor } from '../extractors.models';++export const imageExtractorDefinition = defineTextExtractor({+ name: 'image',+ mimeTypes: [+ 'image/png',+ 'image/jpeg',+ 'image/webp',+ 'image/gif',+ ],+ extract: async ({ arrayBuffer }) => {+ const buffer = Buffer.from(arrayBuffer);++ const worker = await createWorker();++ const { data: { text } } = await worker.recognize(buffer);+ await worker.terminate();++ return { content: text };+ },+});