files.py 9.3 KB

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