Skip to content

armgit / txtai

Public Imported from GitHub
0

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

fix(workflow): write a URL without a filename to a file, not to the directory (#1400)

PerryLink8e7a33c54c0c
diff --git a/src/python/txtai/workflow/task/retrieve.py b/src/python/txtai/workflow/task/retrieve.py
index 622fab36..37fc1513 100644
--- a/src/python/txtai/workflow/task/retrieve.py
+++ b/src/python/txtai/workflow/task/retrieve.py
@@ -52,7 +52,8 @@ class RetrieveTask(Task):
if url:
if self.flatten:
# Flatten directory structure (default)
- path = os.path.join(self.directory, os.path.basename(path))
+ # A URL with no filename falls back to its hostname, then to "download"
+ path = os.path.join(self.directory, os.path.basename(path) or urlparse(element).hostname or "download")
directory = None
else:
# Derive output path
diff --git a/test/python/testworkflow.py b/test/python/testworkflow.py
index ce193a57..df35c610 100644
--- a/test/python/testworkflow.py
+++ b/test/python/testworkflow.py
@@ -23,6 +23,7 @@ from croniter import croniter
from txtai.api import API
from txtai.app import Application
from txtai.embeddings import Documents, Embeddings
+from txtai.pipeline.data.urlretrieve import URLRetrieve
from txtai.pipeline import Nop, Segmentation, Summary, Translation, Textractor
from txtai.workflow import (
Workflow,
@@ -485,6 +486,21 @@ class TestWorkflow(unittest.TestCase):
results = list(workflow(["file://" + Utils.PATH + "/books.jpg"]))
self.assertTrue(results[0].endswith("books.jpg") and "txtai" in results[0])
+ # A URL with no filename must still write to a file, not to the output directory
+ with patch.object(URLRetrieve, "__call__", return_value=b"data"):
+ with tempfile.TemporaryDirectory() as directory:
+ path = RetrieveTask(directory=directory).prepare("https://example.com/")
+ self.assertTrue(os.path.isfile(path))
+ with open(path, "rb") as handle:
+ self.assertEqual(handle.read(), b"data")
+
+ # Distinct bare host URLs must not resolve to the same file
+ first = RetrieveTask(directory=directory).prepare("https://a.example.com/")
+ second = RetrieveTask(directory=directory).prepare("https://b.example.com/")
+ self.assertNotEqual(first, second)
+ self.assertTrue(first.endswith("a.example.com"))
+ self.assertTrue(second.endswith("b.example.com"))
+
def testRetrieveLocalFilenames(self):
"""
Test that local filenames are preserved instead of parsed as URLs