Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Avoid temporary files for failed Textractor downloads (#1468)

HuaTNA7e913ad2c9ee
diff --git a/src/python/txtai/pipeline/data/textractor.py b/src/python/txtai/pipeline/data/textractor.py
index 4cfc3132..747c743c 100644
--- a/src/python/txtai/pipeline/data/textractor.py
+++ b/src/python/txtai/pipeline/data/textractor.py
@@ -87,10 +87,13 @@ class Textractor(Segmentation):
temporary file path
"""
+ # Retrieve data before creating a persistent temporary file
+ data = self.safeopen.retrieve(url)
+
with tempfile.NamedTemporaryFile(mode="wb", delete=False) as output:
path = output.name
- # Retrieve and write data to temporary file
- output.write(self.safeopen.retrieve(url))
+ # Write data to temporary file
+ output.write(data)
return path
diff --git a/test/python/testpipeline/testdata/testtextractor.py b/test/python/testpipeline/testdata/testtextractor.py
index c095a4c2..e75d68f4 100644
--- a/test/python/testpipeline/testdata/testtextractor.py
+++ b/test/python/testpipeline/testdata/testtextractor.py
@@ -2,8 +2,13 @@
Textractor module tests
"""
+import os
+import tempfile
import unittest
+from unittest.mock import patch
+from urllib.error import URLError
+
from txtai.pipeline import Textractor
# pylint: disable=C0411
@@ -62,6 +67,22 @@ class TestTextractor(unittest.TestCase):
# Check number of sections is as expected
self.assertEqual(len(sections), 2)
+ def testDownloadError(self):
+ """
+ Test failed remote retrieval leaves no temporary file
+ """
+
+ textractor = Textractor()
+ error = URLError("download failed")
+
+ with tempfile.TemporaryDirectory() as directory:
+ with patch("tempfile.tempdir", directory), patch.object(textractor.safeopen, "retrieve", side_effect=error):
+ with self.assertRaises(URLError) as raised:
+ textractor("https://example.com/missing.pdf")
+
+ self.assertIs(raised.exception, error)
+ self.assertEqual(os.listdir(directory), [])
+
def testDocling(self):
"""
Test docling backend