Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add ability to fetch content from URL
  • Loading branch information
Sudhamsha committed Apr 17, 2025
commit fac1425a465b38b23d8917a0b8e375759aef0a73
40 changes: 29 additions & 11 deletions cognee/api/v1/add/routers/get_add_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,38 +30,56 @@ async def add(
"""
from cognee.api.v1.add import add as cognee_add
try:
if file:
# Handle file upload
return await cognee_add(
file,
datasetId,
user=user,
)
logger.info(f"Received data for datasetId={datasetId}")
if file and file.filename:
logger.info(f"Received file upload: filename={file.filename}, content_type={file.content_type}, datasetId={datasetId}")
try:
text = (await file.read()).decode("utf-8")
logger.info(f"Passing uploaded file as text to cognee_add")
return await cognee_add(
text,
datasetId,
user=user
)
except Exception as e:
logger.info(f"Could not decode file as text, falling back to binary. Error: {e}")
file.file.seek(0)
return await cognee_add(
file.file,
datasetId,
user=user
)
elif url:
logger.info(f"Received url={url} for datasetId={datasetId}")
if url.startswith("http"):
if "github" in url:
# Perform git clone if the URL is from GitHub
repo_name = url.split("/")[-1].replace(".git", "")
subprocess.run(["git", "clone", url, f".data/{repo_name}"], check=True)
logger.info(f"Cloned GitHub repo to .data/{repo_name}")
return await cognee_add(
"data://.data/",
f"{repo_name}",
user=user,
)
else:
# Fetch and store the data from other types of URL
response = requests.get(url)
response.raise_for_status()
if not response.content:
logger.error(f"No content fetched from URL: {url}")
return JSONResponse(status_code=400, content={"error": "No content fetched from URL"})
logger.info(f"Fetched content from URL: {response.text}")
return await cognee_add(
response.content,
response.text,
datasetId,
user=user,
user=user
)
else:
logger.error(f"Invalid URL format: {url}")
return JSONResponse(status_code=400, content={"error": "Invalid URL format"})
else:
return JSONResponse(status_code=400, content={"error": "No file or URL provided"})
except Exception as error:
logger.error(f"Error processing file or URL: {error}")
return JSONResponse(status_code=409, content={"error": str(error)})

return router