files.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. import logging
  2. import os
  3. import uuid
  4. import json
  5. from fnmatch import fnmatch
  6. from pathlib import Path
  7. from typing import Optional
  8. from urllib.parse import quote
  9. import asyncio
  10. from fastapi import (
  11. BackgroundTasks,
  12. APIRouter,
  13. Depends,
  14. File,
  15. Form,
  16. HTTPException,
  17. Request,
  18. UploadFile,
  19. status,
  20. Query,
  21. )
  22. from fastapi.responses import FileResponse, StreamingResponse
  23. from open_webui.constants import ERROR_MESSAGES
  24. from open_webui.env import SRC_LOG_LEVELS
  25. from open_webui.retrieval.vector.factory import VECTOR_DB_CLIENT
  26. from open_webui.models.users import Users
  27. from open_webui.models.files import (
  28. FileForm,
  29. FileModel,
  30. FileModelResponse,
  31. Files,
  32. )
  33. from open_webui.models.knowledge import Knowledges
  34. from open_webui.routers.knowledge import get_knowledge, get_knowledge_list
  35. from open_webui.routers.retrieval import ProcessFileForm, process_file
  36. from open_webui.routers.audio import transcribe
  37. from open_webui.storage.provider import Storage
  38. from open_webui.utils.auth import get_admin_user, get_verified_user
  39. from pydantic import BaseModel
  40. log = logging.getLogger(__name__)
  41. log.setLevel(SRC_LOG_LEVELS["MODELS"])
  42. router = APIRouter()
  43. ############################
  44. # Check if the current user has access to a file through any knowledge bases the user may be in.
  45. ############################
  46. def has_access_to_file(
  47. file_id: Optional[str], access_type: str, user=Depends(get_verified_user)
  48. ) -> bool:
  49. file = Files.get_file_by_id(file_id)
  50. log.debug(f"Checking if user has {access_type} access to file")
  51. if not file:
  52. raise HTTPException(
  53. status_code=status.HTTP_404_NOT_FOUND,
  54. detail=ERROR_MESSAGES.NOT_FOUND,
  55. )
  56. has_access = False
  57. knowledge_base_id = file.meta.get("collection_name") if file.meta else None
  58. if knowledge_base_id:
  59. knowledge_bases = Knowledges.get_knowledge_bases_by_user_id(
  60. user.id, access_type
  61. )
  62. for knowledge_base in knowledge_bases:
  63. if knowledge_base.id == knowledge_base_id:
  64. has_access = True
  65. break
  66. return has_access
  67. ############################
  68. # Upload File
  69. ############################
  70. def process_uploaded_file(request, file, file_path, file_item, file_metadata, user):
  71. try:
  72. if file.content_type:
  73. stt_supported_content_types = getattr(
  74. request.app.state.config, "STT_SUPPORTED_CONTENT_TYPES", []
  75. )
  76. if any(
  77. fnmatch(file.content_type, content_type)
  78. for content_type in (
  79. stt_supported_content_types
  80. if stt_supported_content_types
  81. and any(t.strip() for t in stt_supported_content_types)
  82. else ["audio/*", "video/webm"]
  83. )
  84. ):
  85. file_path = Storage.get_file(file_path)
  86. result = transcribe(request, file_path, file_metadata)
  87. process_file(
  88. request,
  89. ProcessFileForm(
  90. file_id=file_item.id, content=result.get("text", "")
  91. ),
  92. user=user,
  93. )
  94. elif (not file.content_type.startswith(("image/", "video/"))) or (
  95. request.app.state.config.CONTENT_EXTRACTION_ENGINE == "external"
  96. ):
  97. process_file(request, ProcessFileForm(file_id=file_item.id), user=user)
  98. else:
  99. log.info(
  100. f"File type {file.content_type} is not provided, but trying to process anyway"
  101. )
  102. process_file(request, ProcessFileForm(file_id=file_item.id), user=user)
  103. Files.update_file_data_by_id(
  104. file_item.id,
  105. {"status": "completed"},
  106. )
  107. except Exception as e:
  108. log.error(f"Error processing file: {file_item.id}")
  109. Files.update_file_data_by_id(
  110. file_item.id,
  111. {
  112. "status": "failed",
  113. "error": str(e.detail) if hasattr(e, "detail") else str(e),
  114. },
  115. )
  116. @router.post("/", response_model=FileModelResponse)
  117. def upload_file(
  118. request: Request,
  119. background_tasks: BackgroundTasks,
  120. file: UploadFile = File(...),
  121. metadata: Optional[dict | str] = Form(None),
  122. process: bool = Query(True),
  123. process_in_background: bool = Query(True),
  124. user=Depends(get_verified_user),
  125. ):
  126. return upload_file_handler(
  127. request,
  128. file=file,
  129. metadata=metadata,
  130. process=process,
  131. process_in_background=process_in_background,
  132. user=user,
  133. background_tasks=background_tasks,
  134. )
  135. def upload_file_handler(
  136. request: Request,
  137. file: UploadFile = File(...),
  138. metadata: Optional[dict | str] = Form(None),
  139. process: bool = Query(True),
  140. process_in_background: bool = Query(True),
  141. user=Depends(get_verified_user),
  142. background_tasks: Optional[BackgroundTasks] = None,
  143. ):
  144. log.info(f"file.content_type: {file.content_type}")
  145. if isinstance(metadata, str):
  146. try:
  147. metadata = json.loads(metadata)
  148. except json.JSONDecodeError:
  149. raise HTTPException(
  150. status_code=status.HTTP_400_BAD_REQUEST,
  151. detail=ERROR_MESSAGES.DEFAULT("Invalid metadata format"),
  152. )
  153. file_metadata = metadata if metadata else {}
  154. try:
  155. unsanitized_filename = file.filename
  156. filename = os.path.basename(unsanitized_filename)
  157. file_extension = os.path.splitext(filename)[1]
  158. # Remove the leading dot from the file extension
  159. file_extension = file_extension[1:] if file_extension else ""
  160. if process and request.app.state.config.ALLOWED_FILE_EXTENSIONS:
  161. request.app.state.config.ALLOWED_FILE_EXTENSIONS = [
  162. ext for ext in request.app.state.config.ALLOWED_FILE_EXTENSIONS if ext
  163. ]
  164. if file_extension not in request.app.state.config.ALLOWED_FILE_EXTENSIONS:
  165. raise HTTPException(
  166. status_code=status.HTTP_400_BAD_REQUEST,
  167. detail=ERROR_MESSAGES.DEFAULT(
  168. f"File type {file_extension} is not allowed"
  169. ),
  170. )
  171. # replace filename with uuid
  172. id = str(uuid.uuid4())
  173. name = filename
  174. filename = f"{id}_{filename}"
  175. contents, file_path = Storage.upload_file(
  176. file.file,
  177. filename,
  178. {
  179. "OpenWebUI-User-Email": user.email,
  180. "OpenWebUI-User-Id": user.id,
  181. "OpenWebUI-User-Name": user.name,
  182. "OpenWebUI-File-Id": id,
  183. },
  184. )
  185. file_item = Files.insert_new_file(
  186. user.id,
  187. FileForm(
  188. **{
  189. "id": id,
  190. "filename": name,
  191. "path": file_path,
  192. "data": {
  193. **({"status": "pending"} if process else {}),
  194. },
  195. "meta": {
  196. "name": name,
  197. "content_type": file.content_type,
  198. "size": len(contents),
  199. "data": file_metadata,
  200. },
  201. }
  202. ),
  203. )
  204. if process:
  205. if background_tasks and process_in_background:
  206. background_tasks.add_task(
  207. process_uploaded_file,
  208. request,
  209. file,
  210. file_path,
  211. file_item,
  212. file_metadata,
  213. user,
  214. )
  215. return {"status": True, **file_item.model_dump()}
  216. else:
  217. process_uploaded_file(
  218. request,
  219. file,
  220. file_path,
  221. file_item,
  222. file_metadata,
  223. user,
  224. )
  225. return {"status": True, **file_item.model_dump()}
  226. else:
  227. if file_item:
  228. return file_item
  229. else:
  230. raise HTTPException(
  231. status_code=status.HTTP_400_BAD_REQUEST,
  232. detail=ERROR_MESSAGES.DEFAULT("Error uploading file"),
  233. )
  234. except Exception as e:
  235. log.exception(e)
  236. raise HTTPException(
  237. status_code=status.HTTP_400_BAD_REQUEST,
  238. detail=ERROR_MESSAGES.DEFAULT("Error uploading file"),
  239. )
  240. ############################
  241. # List Files
  242. ############################
  243. @router.get("/", response_model=list[FileModelResponse])
  244. async def list_files(user=Depends(get_verified_user), content: bool = Query(True)):
  245. if user.role == "admin":
  246. files = Files.get_files()
  247. else:
  248. files = Files.get_files_by_user_id(user.id)
  249. if not content:
  250. for file in files:
  251. if "content" in file.data:
  252. del file.data["content"]
  253. return files
  254. ############################
  255. # Search Files
  256. ############################
  257. @router.get("/search", response_model=list[FileModelResponse])
  258. async def search_files(
  259. filename: str = Query(
  260. ...,
  261. description="Filename pattern to search for. Supports wildcards such as '*.txt'",
  262. ),
  263. content: bool = Query(True),
  264. user=Depends(get_verified_user),
  265. ):
  266. """
  267. Search for files by filename with support for wildcard patterns.
  268. """
  269. # Get files according to user role
  270. if user.role == "admin":
  271. files = Files.get_files()
  272. else:
  273. files = Files.get_files_by_user_id(user.id)
  274. # Get matching files
  275. matching_files = [
  276. file for file in files if fnmatch(file.filename.lower(), filename.lower())
  277. ]
  278. if not matching_files:
  279. raise HTTPException(
  280. status_code=status.HTTP_404_NOT_FOUND,
  281. detail="No files found matching the pattern.",
  282. )
  283. if not content:
  284. for file in matching_files:
  285. if "content" in file.data:
  286. del file.data["content"]
  287. return matching_files
  288. ############################
  289. # Delete All Files
  290. ############################
  291. @router.delete("/all")
  292. async def delete_all_files(user=Depends(get_admin_user)):
  293. result = Files.delete_all_files()
  294. if result:
  295. try:
  296. Storage.delete_all_files()
  297. VECTOR_DB_CLIENT.reset()
  298. except Exception as e:
  299. log.exception(e)
  300. log.error("Error deleting files")
  301. raise HTTPException(
  302. status_code=status.HTTP_400_BAD_REQUEST,
  303. detail=ERROR_MESSAGES.DEFAULT("Error deleting files"),
  304. )
  305. return {"message": "All files deleted successfully"}
  306. else:
  307. raise HTTPException(
  308. status_code=status.HTTP_400_BAD_REQUEST,
  309. detail=ERROR_MESSAGES.DEFAULT("Error deleting files"),
  310. )
  311. ############################
  312. # Get File By Id
  313. ############################
  314. @router.get("/{id}", response_model=Optional[FileModel])
  315. async def get_file_by_id(id: str, user=Depends(get_verified_user)):
  316. file = Files.get_file_by_id(id)
  317. if not file:
  318. raise HTTPException(
  319. status_code=status.HTTP_404_NOT_FOUND,
  320. detail=ERROR_MESSAGES.NOT_FOUND,
  321. )
  322. if (
  323. file.user_id == user.id
  324. or user.role == "admin"
  325. or has_access_to_file(id, "read", user)
  326. ):
  327. return file
  328. else:
  329. raise HTTPException(
  330. status_code=status.HTTP_404_NOT_FOUND,
  331. detail=ERROR_MESSAGES.NOT_FOUND,
  332. )
  333. @router.get("/{id}/process/status")
  334. async def get_file_process_status(
  335. id: str, stream: bool = Query(False), user=Depends(get_verified_user)
  336. ):
  337. file = Files.get_file_by_id(id)
  338. if not file:
  339. raise HTTPException(
  340. status_code=status.HTTP_404_NOT_FOUND,
  341. detail=ERROR_MESSAGES.NOT_FOUND,
  342. )
  343. if (
  344. file.user_id == user.id
  345. or user.role == "admin"
  346. or has_access_to_file(id, "read", user)
  347. ):
  348. if stream:
  349. MAX_FILE_PROCESSING_DURATION = 3600 * 2
  350. async def event_stream(file_item):
  351. for _ in range(MAX_FILE_PROCESSING_DURATION):
  352. file_item = Files.get_file_by_id(file_item.id)
  353. if file_item:
  354. data = file_item.model_dump().get("data", {})
  355. status = data.get("status")
  356. if status:
  357. event = {"status": status}
  358. if status == "failed":
  359. event["error"] = data.get("error")
  360. yield f"data: {json.dumps(event)}\n\n"
  361. if status in ("completed", "failed"):
  362. break
  363. else:
  364. # Legacy
  365. break
  366. await asyncio.sleep(0.5)
  367. return StreamingResponse(
  368. event_stream(file),
  369. media_type="text/event-stream",
  370. )
  371. else:
  372. return {"status": file.data.get("status", "pending")}
  373. else:
  374. raise HTTPException(
  375. status_code=status.HTTP_404_NOT_FOUND,
  376. detail=ERROR_MESSAGES.NOT_FOUND,
  377. )
  378. ############################
  379. # Get File Data Content By Id
  380. ############################
  381. @router.get("/{id}/data/content")
  382. async def get_file_data_content_by_id(id: str, user=Depends(get_verified_user)):
  383. file = Files.get_file_by_id(id)
  384. if not file:
  385. raise HTTPException(
  386. status_code=status.HTTP_404_NOT_FOUND,
  387. detail=ERROR_MESSAGES.NOT_FOUND,
  388. )
  389. if (
  390. file.user_id == user.id
  391. or user.role == "admin"
  392. or has_access_to_file(id, "read", user)
  393. ):
  394. return {"content": file.data.get("content", "")}
  395. else:
  396. raise HTTPException(
  397. status_code=status.HTTP_404_NOT_FOUND,
  398. detail=ERROR_MESSAGES.NOT_FOUND,
  399. )
  400. ############################
  401. # Update File Data Content By Id
  402. ############################
  403. class ContentForm(BaseModel):
  404. content: str
  405. @router.post("/{id}/data/content/update")
  406. async def update_file_data_content_by_id(
  407. request: Request, id: str, form_data: ContentForm, user=Depends(get_verified_user)
  408. ):
  409. file = Files.get_file_by_id(id)
  410. if not file:
  411. raise HTTPException(
  412. status_code=status.HTTP_404_NOT_FOUND,
  413. detail=ERROR_MESSAGES.NOT_FOUND,
  414. )
  415. if (
  416. file.user_id == user.id
  417. or user.role == "admin"
  418. or has_access_to_file(id, "write", user)
  419. ):
  420. try:
  421. process_file(
  422. request,
  423. ProcessFileForm(file_id=id, content=form_data.content),
  424. user=user,
  425. )
  426. file = Files.get_file_by_id(id=id)
  427. except Exception as e:
  428. log.exception(e)
  429. log.error(f"Error processing file: {file.id}")
  430. return {"content": file.data.get("content", "")}
  431. else:
  432. raise HTTPException(
  433. status_code=status.HTTP_404_NOT_FOUND,
  434. detail=ERROR_MESSAGES.NOT_FOUND,
  435. )
  436. ############################
  437. # Get File Content By Id
  438. ############################
  439. @router.get("/{id}/content")
  440. async def get_file_content_by_id(
  441. id: str, user=Depends(get_verified_user), attachment: bool = Query(False)
  442. ):
  443. file = Files.get_file_by_id(id)
  444. if not file:
  445. raise HTTPException(
  446. status_code=status.HTTP_404_NOT_FOUND,
  447. detail=ERROR_MESSAGES.NOT_FOUND,
  448. )
  449. if (
  450. file.user_id == user.id
  451. or user.role == "admin"
  452. or has_access_to_file(id, "read", user)
  453. ):
  454. try:
  455. file_path = Storage.get_file(file.path)
  456. file_path = Path(file_path)
  457. # Check if the file already exists in the cache
  458. if file_path.is_file():
  459. # Handle Unicode filenames
  460. filename = file.meta.get("name", file.filename)
  461. encoded_filename = quote(filename) # RFC5987 encoding
  462. content_type = file.meta.get("content_type")
  463. filename = file.meta.get("name", file.filename)
  464. encoded_filename = quote(filename)
  465. headers = {}
  466. if attachment:
  467. headers["Content-Disposition"] = (
  468. f"attachment; filename*=UTF-8''{encoded_filename}"
  469. )
  470. else:
  471. if content_type == "application/pdf" or filename.lower().endswith(
  472. ".pdf"
  473. ):
  474. headers["Content-Disposition"] = (
  475. f"inline; filename*=UTF-8''{encoded_filename}"
  476. )
  477. content_type = "application/pdf"
  478. elif content_type != "text/plain":
  479. headers["Content-Disposition"] = (
  480. f"attachment; filename*=UTF-8''{encoded_filename}"
  481. )
  482. return FileResponse(file_path, headers=headers, media_type=content_type)
  483. else:
  484. raise HTTPException(
  485. status_code=status.HTTP_404_NOT_FOUND,
  486. detail=ERROR_MESSAGES.NOT_FOUND,
  487. )
  488. except Exception as e:
  489. log.exception(e)
  490. log.error("Error getting file content")
  491. raise HTTPException(
  492. status_code=status.HTTP_400_BAD_REQUEST,
  493. detail=ERROR_MESSAGES.DEFAULT("Error getting file content"),
  494. )
  495. else:
  496. raise HTTPException(
  497. status_code=status.HTTP_404_NOT_FOUND,
  498. detail=ERROR_MESSAGES.NOT_FOUND,
  499. )
  500. @router.get("/{id}/content/html")
  501. async def get_html_file_content_by_id(id: str, user=Depends(get_verified_user)):
  502. file = Files.get_file_by_id(id)
  503. if not file:
  504. raise HTTPException(
  505. status_code=status.HTTP_404_NOT_FOUND,
  506. detail=ERROR_MESSAGES.NOT_FOUND,
  507. )
  508. file_user = Users.get_user_by_id(file.user_id)
  509. if not file_user.role == "admin":
  510. raise HTTPException(
  511. status_code=status.HTTP_404_NOT_FOUND,
  512. detail=ERROR_MESSAGES.NOT_FOUND,
  513. )
  514. if (
  515. file.user_id == user.id
  516. or user.role == "admin"
  517. or has_access_to_file(id, "read", user)
  518. ):
  519. try:
  520. file_path = Storage.get_file(file.path)
  521. file_path = Path(file_path)
  522. # Check if the file already exists in the cache
  523. if file_path.is_file():
  524. log.info(f"file_path: {file_path}")
  525. return FileResponse(file_path)
  526. else:
  527. raise HTTPException(
  528. status_code=status.HTTP_404_NOT_FOUND,
  529. detail=ERROR_MESSAGES.NOT_FOUND,
  530. )
  531. except Exception as e:
  532. log.exception(e)
  533. log.error("Error getting file content")
  534. raise HTTPException(
  535. status_code=status.HTTP_400_BAD_REQUEST,
  536. detail=ERROR_MESSAGES.DEFAULT("Error getting file content"),
  537. )
  538. else:
  539. raise HTTPException(
  540. status_code=status.HTTP_404_NOT_FOUND,
  541. detail=ERROR_MESSAGES.NOT_FOUND,
  542. )
  543. @router.get("/{id}/content/{file_name}")
  544. async def get_file_content_by_id(id: str, user=Depends(get_verified_user)):
  545. file = Files.get_file_by_id(id)
  546. if not file:
  547. raise HTTPException(
  548. status_code=status.HTTP_404_NOT_FOUND,
  549. detail=ERROR_MESSAGES.NOT_FOUND,
  550. )
  551. if (
  552. file.user_id == user.id
  553. or user.role == "admin"
  554. or has_access_to_file(id, "read", user)
  555. ):
  556. file_path = file.path
  557. # Handle Unicode filenames
  558. filename = file.meta.get("name", file.filename)
  559. encoded_filename = quote(filename) # RFC5987 encoding
  560. headers = {
  561. "Content-Disposition": f"attachment; filename*=UTF-8''{encoded_filename}"
  562. }
  563. if file_path:
  564. file_path = Storage.get_file(file_path)
  565. file_path = Path(file_path)
  566. # Check if the file already exists in the cache
  567. if file_path.is_file():
  568. return FileResponse(file_path, headers=headers)
  569. else:
  570. raise HTTPException(
  571. status_code=status.HTTP_404_NOT_FOUND,
  572. detail=ERROR_MESSAGES.NOT_FOUND,
  573. )
  574. else:
  575. # File path doesn’t exist, return the content as .txt if possible
  576. file_content = file.content.get("content", "")
  577. file_name = file.filename
  578. # Create a generator that encodes the file content
  579. def generator():
  580. yield file_content.encode("utf-8")
  581. return StreamingResponse(
  582. generator(),
  583. media_type="text/plain",
  584. headers=headers,
  585. )
  586. else:
  587. raise HTTPException(
  588. status_code=status.HTTP_404_NOT_FOUND,
  589. detail=ERROR_MESSAGES.NOT_FOUND,
  590. )
  591. ############################
  592. # Delete File By Id
  593. ############################
  594. @router.delete("/{id}")
  595. async def delete_file_by_id(id: str, user=Depends(get_verified_user)):
  596. file = Files.get_file_by_id(id)
  597. if not file:
  598. raise HTTPException(
  599. status_code=status.HTTP_404_NOT_FOUND,
  600. detail=ERROR_MESSAGES.NOT_FOUND,
  601. )
  602. if (
  603. file.user_id == user.id
  604. or user.role == "admin"
  605. or has_access_to_file(id, "write", user)
  606. ):
  607. result = Files.delete_file_by_id(id)
  608. if result:
  609. try:
  610. Storage.delete_file(file.path)
  611. VECTOR_DB_CLIENT.delete(collection_name=f"file-{id}")
  612. except Exception as e:
  613. log.exception(e)
  614. log.error("Error deleting files")
  615. raise HTTPException(
  616. status_code=status.HTTP_400_BAD_REQUEST,
  617. detail=ERROR_MESSAGES.DEFAULT("Error deleting files"),
  618. )
  619. return {"message": "File deleted successfully"}
  620. else:
  621. raise HTTPException(
  622. status_code=status.HTTP_400_BAD_REQUEST,
  623. detail=ERROR_MESSAGES.DEFAULT("Error deleting file"),
  624. )
  625. else:
  626. raise HTTPException(
  627. status_code=status.HTTP_404_NOT_FOUND,
  628. detail=ERROR_MESSAGES.NOT_FOUND,
  629. )