files.py 9.2 KB

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