Skip to content

Commit 7c31862

Browse files
authored
Fix file reference links (#33)
1 parent 40566dd commit 7c31862

2 files changed

Lines changed: 38 additions & 8 deletions

File tree

backend/danswer/connectors/file/connector.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from sqlalchemy.orm import Session
1010

1111
from danswer.configs.app_configs import INDEX_BATCH_SIZE
12+
from danswer.configs.app_configs import WEB_DOMAIN
1213
from danswer.configs.constants import DocumentSource
1314
from danswer.connectors.cross_connector_utils.miscellaneous_utils import time_str_to_utc
1415
from danswer.connectors.interfaces import GenerateDocumentsOutput
@@ -130,12 +131,18 @@ def _process_file(
130131
else None
131132
)
132133

134+
# Generate a link for file uploads if not already provided
135+
# Files uploaded via the connector are stored as uuid/filename
136+
file_link = all_metadata.get("link")
137+
if not file_link and "/" in file_name:
138+
# The file_name format is "uuid/filename"
139+
# The endpoint expects /file/{uuid}/{filename}
140+
file_link = f"{WEB_DOMAIN}/api/chat/file/{file_name}"
141+
133142
return [
134143
Document(
135144
id=f"FILE_CONNECTOR__{file_name}", # add a prefix to avoid conflicts with other connectors
136-
sections=[
137-
Section(link=all_metadata.get("link"), text=file_content_raw.strip())
138-
],
145+
sections=[Section(link=file_link, text=file_content_raw.strip())],
139146
source=DocumentSource.FILE,
140147
semantic_identifier=file_display_name,
141148
title=title,

backend/danswer/server/query_and_chat/chat_backend.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -573,14 +573,37 @@ def upload_files_for_chat(
573573
}
574574

575575

576-
@router.get("/file/{file_id}")
576+
@router.get("/file/{file_id:path}")
577577
def fetch_chat_file(
578578
file_id: str,
579579
db_session: Session = Depends(get_session),
580580
_: User | None = Depends(current_user),
581581
) -> Response:
582582
file_store = get_default_file_store(db_session)
583-
file_io = file_store.read_file(file_id, mode="b")
584-
# NOTE: specifying "image/jpeg" here, but it still works for pngs
585-
# TODO: do this properly
586-
return Response(content=file_io.read(), media_type="image/jpeg")
583+
584+
try:
585+
file_io = file_store.read_file(file_id, mode="b")
586+
587+
# Determine content type based on file extension if it's a path
588+
content_type = "application/octet-stream"
589+
if "/" in file_id:
590+
# It's a path like uuid/filename, extract filename
591+
filename = file_id.split("/")[-1].lower()
592+
if filename.endswith(".pdf"):
593+
content_type = "application/pdf"
594+
elif filename.endswith((".jpg", ".jpeg")):
595+
content_type = "image/jpeg"
596+
elif filename.endswith(".png"):
597+
content_type = "image/png"
598+
elif filename.endswith(".txt"):
599+
content_type = "text/plain"
600+
elif filename.endswith((".doc", ".docx")):
601+
content_type = "application/msword"
602+
else:
603+
# For simple IDs (chat uploads)
604+
content_type = "image/jpeg"
605+
606+
return Response(content=file_io.read(), media_type=content_type)
607+
except Exception as e:
608+
logger.error(f"Error fetching file {file_id}: {str(e)}")
609+
raise HTTPException(status_code=404, detail="File not found")

0 commit comments

Comments
 (0)