@@ -1532,6 +1532,69 @@ async def search_knowledge_files(
15321532 return json .dumps ({"error" : str (e )})
15331533
15341534
1535+ async def view_file (
1536+ file_id : str ,
1537+ __request__ : Request = None ,
1538+ __user__ : dict = None ,
1539+ __model_knowledge__ : Optional [list [dict ]] = None ,
1540+ ) -> str :
1541+ """
1542+ Get the full content of a file by its ID.
1543+
1544+ :param file_id: The ID of the file to retrieve
1545+ :return: JSON with the file's id, filename, and full text content
1546+ """
1547+ if __request__ is None :
1548+ return json .dumps ({"error" : "Request context not available" })
1549+
1550+ if not __user__ :
1551+ return json .dumps ({"error" : "User context not available" })
1552+
1553+ try :
1554+ from open_webui .models .files import Files
1555+ from open_webui .routers .files import has_access_to_file
1556+
1557+ user_id = __user__ .get ("id" )
1558+ user_role = __user__ .get ("role" , "user" )
1559+
1560+ file = Files .get_file_by_id (file_id )
1561+ if not file :
1562+ return json .dumps ({"error" : "File not found" })
1563+
1564+ if (
1565+ file .user_id != user_id
1566+ and user_role != "admin"
1567+ and not any (
1568+ item .get ("type" ) == "file" and item .get ("id" ) == file_id
1569+ for item in (__model_knowledge__ or [])
1570+ )
1571+ and not has_access_to_file (
1572+ file_id = file_id ,
1573+ access_type = "read" ,
1574+ user = UserModel (** __user__ ),
1575+ )
1576+ ):
1577+ return json .dumps ({"error" : "File not found" })
1578+
1579+ content = ""
1580+ if file .data :
1581+ content = file .data .get ("content" , "" )
1582+
1583+ return json .dumps (
1584+ {
1585+ "id" : file .id ,
1586+ "filename" : file .filename ,
1587+ "content" : content ,
1588+ "updated_at" : file .updated_at ,
1589+ "created_at" : file .created_at ,
1590+ },
1591+ ensure_ascii = False ,
1592+ )
1593+ except Exception as e :
1594+ log .exception (f"view_file error: { e } " )
1595+ return json .dumps ({"error" : str (e )})
1596+
1597+
15351598async def view_knowledge_file (
15361599 file_id : str ,
15371600 __request__ : Request = None ,
0 commit comments