files.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. import logging
  2. import os
  3. import shutil
  4. import uuid
  5. from pathlib import Path
  6. from typing import Optional
  7. from pydantic import BaseModel
  8. from open_webui.apps.webui.models.files import FileForm, FileModel, Files
  9. from open_webui.apps.retrieval.main import process_file, ProcessFileForm
  10. from open_webui.config import UPLOAD_DIR
  11. from open_webui.constants import ERROR_MESSAGES
  12. from open_webui.env import SRC_LOG_LEVELS
  13. from fastapi import APIRouter, Depends, File, HTTPException, UploadFile, status
  14. from fastapi.responses import FileResponse, StreamingResponse
  15. from open_webui.utils.utils import get_admin_user, get_verified_user
  16. log = logging.getLogger(__name__)
  17. log.setLevel(SRC_LOG_LEVELS["MODELS"])
  18. router = APIRouter()
  19. ############################
  20. # Upload File
  21. ############################
  22. @router.post("/")
  23. def upload_file(file: UploadFile = File(...), user=Depends(get_verified_user)):
  24. log.info(f"file.content_type: {file.content_type}")
  25. try:
  26. unsanitized_filename = file.filename
  27. filename = os.path.basename(unsanitized_filename)
  28. # replace filename with uuid
  29. id = str(uuid.uuid4())
  30. name = filename
  31. filename = f"{id}_{filename}"
  32. file_path = f"{UPLOAD_DIR}/{filename}"
  33. contents = file.file.read()
  34. with open(file_path, "wb") as f:
  35. f.write(contents)
  36. f.close()
  37. file = Files.insert_new_file(
  38. user.id,
  39. FileForm(
  40. **{
  41. "id": id,
  42. "filename": filename,
  43. "meta": {
  44. "name": name,
  45. "content_type": file.content_type,
  46. "size": len(contents),
  47. "path": file_path,
  48. },
  49. }
  50. ),
  51. )
  52. try:
  53. process_file(ProcessFileForm(file_id=id))
  54. file = Files.get_file_by_id(id=id)
  55. except Exception as e:
  56. log.exception(e)
  57. log.error(f"Error processing file: {file.id}")
  58. if file:
  59. return file
  60. else:
  61. raise HTTPException(
  62. status_code=status.HTTP_400_BAD_REQUEST,
  63. detail=ERROR_MESSAGES.DEFAULT("Error uploading file"),
  64. )
  65. except Exception as e:
  66. log.exception(e)
  67. raise HTTPException(
  68. status_code=status.HTTP_400_BAD_REQUEST,
  69. detail=ERROR_MESSAGES.DEFAULT(e),
  70. )
  71. ############################
  72. # List Files
  73. ############################
  74. @router.get("/", response_model=list[FileModel])
  75. async def list_files(user=Depends(get_verified_user)):
  76. if user.role == "admin":
  77. files = Files.get_files()
  78. else:
  79. files = Files.get_files_by_user_id(user.id)
  80. return files
  81. ############################
  82. # Delete All Files
  83. ############################
  84. @router.delete("/all")
  85. async def delete_all_files(user=Depends(get_admin_user)):
  86. result = Files.delete_all_files()
  87. if result:
  88. folder = f"{UPLOAD_DIR}"
  89. try:
  90. # Check if the directory exists
  91. if os.path.exists(folder):
  92. # Iterate over all the files and directories in the specified directory
  93. for filename in os.listdir(folder):
  94. file_path = os.path.join(folder, filename)
  95. try:
  96. if os.path.isfile(file_path) or os.path.islink(file_path):
  97. os.unlink(file_path) # Remove the file or link
  98. elif os.path.isdir(file_path):
  99. shutil.rmtree(file_path) # Remove the directory
  100. except Exception as e:
  101. print(f"Failed to delete {file_path}. Reason: {e}")
  102. else:
  103. print(f"The directory {folder} does not exist")
  104. except Exception as e:
  105. print(f"Failed to process the directory {folder}. Reason: {e}")
  106. return {"message": "All files deleted successfully"}
  107. else:
  108. raise HTTPException(
  109. status_code=status.HTTP_400_BAD_REQUEST,
  110. detail=ERROR_MESSAGES.DEFAULT("Error deleting files"),
  111. )
  112. ############################
  113. # Get File By Id
  114. ############################
  115. @router.get("/{id}", response_model=Optional[FileModel])
  116. async def get_file_by_id(id: str, user=Depends(get_verified_user)):
  117. file = Files.get_file_by_id(id)
  118. if file and (file.user_id == user.id or user.role == "admin"):
  119. return file
  120. else:
  121. raise HTTPException(
  122. status_code=status.HTTP_404_NOT_FOUND,
  123. detail=ERROR_MESSAGES.NOT_FOUND,
  124. )
  125. ############################
  126. # Get File Data Content By Id
  127. ############################
  128. @router.get("/{id}/data/content")
  129. async def get_file_data_content_by_id(id: str, user=Depends(get_verified_user)):
  130. file = Files.get_file_by_id(id)
  131. if file and (file.user_id == user.id or user.role == "admin"):
  132. return {"content": file.data.get("content", "")}
  133. else:
  134. raise HTTPException(
  135. status_code=status.HTTP_404_NOT_FOUND,
  136. detail=ERROR_MESSAGES.NOT_FOUND,
  137. )
  138. ############################
  139. # Update File Data Content By Id
  140. ############################
  141. class ContentForm(BaseModel):
  142. content: str
  143. @router.post("/{id}/data/content/update")
  144. async def update_file_data_content_by_id(
  145. id: str, form_data: ContentForm, user=Depends(get_verified_user)
  146. ):
  147. file = Files.get_file_by_id(id)
  148. if file and (file.user_id == user.id or user.role == "admin"):
  149. try:
  150. process_file(ProcessFileForm(file_id=id, content=form_data.content))
  151. file = Files.get_file_by_id(id=id)
  152. except Exception as e:
  153. log.exception(e)
  154. log.error(f"Error processing file: {file.id}")
  155. return {"content": file.data.get("content", "")}
  156. else:
  157. raise HTTPException(
  158. status_code=status.HTTP_404_NOT_FOUND,
  159. detail=ERROR_MESSAGES.NOT_FOUND,
  160. )
  161. ############################
  162. # Get File Content By Id
  163. ############################
  164. @router.get("/{id}/content", response_model=Optional[FileModel])
  165. async def get_file_content_by_id(id: str, user=Depends(get_verified_user)):
  166. file = Files.get_file_by_id(id)
  167. if file and (file.user_id == user.id or user.role == "admin"):
  168. file_path = Path(file.meta["path"])
  169. # Check if the file already exists in the cache
  170. if file_path.is_file():
  171. print(f"file_path: {file_path}")
  172. return FileResponse(file_path)
  173. else:
  174. raise HTTPException(
  175. status_code=status.HTTP_404_NOT_FOUND,
  176. detail=ERROR_MESSAGES.NOT_FOUND,
  177. )
  178. else:
  179. raise HTTPException(
  180. status_code=status.HTTP_404_NOT_FOUND,
  181. detail=ERROR_MESSAGES.NOT_FOUND,
  182. )
  183. @router.get("/{id}/content/{file_name}", response_model=Optional[FileModel])
  184. async def get_file_content_by_id(id: str, user=Depends(get_verified_user)):
  185. file = Files.get_file_by_id(id)
  186. if file and (file.user_id == user.id or user.role == "admin"):
  187. file_path = file.meta.get("path")
  188. if file_path:
  189. file_path = Path(file_path)
  190. # Check if the file already exists in the cache
  191. if file_path.is_file():
  192. print(f"file_path: {file_path}")
  193. return FileResponse(file_path)
  194. else:
  195. raise HTTPException(
  196. status_code=status.HTTP_404_NOT_FOUND,
  197. detail=ERROR_MESSAGES.NOT_FOUND,
  198. )
  199. else:
  200. # File path doesn’t exist, return the content as .txt if possible
  201. file_content = file.content.get("content", "")
  202. file_name = file.filename
  203. # Create a generator that encodes the file content
  204. def generator():
  205. yield file_content.encode("utf-8")
  206. return StreamingResponse(
  207. generator(),
  208. media_type="text/plain",
  209. headers={"Content-Disposition": f"attachment; filename={file_name}"},
  210. )
  211. else:
  212. raise HTTPException(
  213. status_code=status.HTTP_404_NOT_FOUND,
  214. detail=ERROR_MESSAGES.NOT_FOUND,
  215. )
  216. ############################
  217. # Delete File By Id
  218. ############################
  219. @router.delete("/{id}")
  220. async def delete_file_by_id(id: str, user=Depends(get_verified_user)):
  221. file = Files.get_file_by_id(id)
  222. if file and (file.user_id == user.id or user.role == "admin"):
  223. result = Files.delete_file_by_id(id)
  224. if result:
  225. return {"message": "File deleted successfully"}
  226. else:
  227. raise HTTPException(
  228. status_code=status.HTTP_400_BAD_REQUEST,
  229. detail=ERROR_MESSAGES.DEFAULT("Error deleting file"),
  230. )
  231. else:
  232. raise HTTPException(
  233. status_code=status.HTTP_404_NOT_FOUND,
  234. detail=ERROR_MESSAGES.NOT_FOUND,
  235. )