files.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import logging
  2. import time
  3. from typing import Optional
  4. from open_webui.apps.webui.internal.db import Base, JSONField, get_db
  5. from open_webui.env import SRC_LOG_LEVELS
  6. from pydantic import BaseModel, ConfigDict
  7. from sqlalchemy import BigInteger, Column, String, Text
  8. log = logging.getLogger(__name__)
  9. log.setLevel(SRC_LOG_LEVELS["MODELS"])
  10. ####################
  11. # Files DB Schema
  12. ####################
  13. class File(Base):
  14. __tablename__ = "file"
  15. id = Column(String, primary_key=True)
  16. user_id = Column(String)
  17. filename = Column(Text)
  18. meta = Column(JSONField)
  19. created_at = Column(BigInteger)
  20. class FileModel(BaseModel):
  21. id: str
  22. user_id: str
  23. filename: str
  24. meta: dict
  25. created_at: int # timestamp in epoch
  26. model_config = ConfigDict(from_attributes=True)
  27. ####################
  28. # Forms
  29. ####################
  30. class FileModelResponse(BaseModel):
  31. id: str
  32. user_id: str
  33. filename: str
  34. meta: dict
  35. created_at: int # timestamp in epoch
  36. class FileForm(BaseModel):
  37. id: str
  38. filename: str
  39. meta: dict = {}
  40. class FilesTable:
  41. def insert_new_file(self, user_id: str, form_data: FileForm) -> Optional[FileModel]:
  42. with get_db() as db:
  43. file = FileModel(
  44. **{
  45. **form_data.model_dump(),
  46. "user_id": user_id,
  47. "created_at": int(time.time()),
  48. }
  49. )
  50. try:
  51. result = File(**file.model_dump())
  52. db.add(result)
  53. db.commit()
  54. db.refresh(result)
  55. if result:
  56. return FileModel.model_validate(result)
  57. else:
  58. return None
  59. except Exception as e:
  60. print(f"Error creating tool: {e}")
  61. return None
  62. def get_file_by_id(self, id: str) -> Optional[FileModel]:
  63. with get_db() as db:
  64. try:
  65. file = db.get(File, id)
  66. return FileModel.model_validate(file)
  67. except Exception:
  68. return None
  69. def get_files(self) -> list[FileModel]:
  70. with get_db() as db:
  71. return [FileModel.model_validate(file) for file in db.query(File).all()]
  72. def get_files_by_user_id(self, user_id: str) -> list[FileModel]:
  73. with get_db() as db:
  74. return [
  75. FileModel.model_validate(file)
  76. for file in db.query(File).filter_by(user_id=user_id).all()
  77. ]
  78. def update_files_metadata_by_id(self, id: str, meta: dict) -> Optional[FileModel]:
  79. with get_db() as db:
  80. try:
  81. file = db.query(File).filter_by(id=id).first()
  82. file.meta = {**file.meta, **meta}
  83. db.commit()
  84. return FileModel.model_validate(file)
  85. except Exception:
  86. return None
  87. def delete_file_by_id(self, id: str) -> bool:
  88. with get_db() as db:
  89. try:
  90. db.query(File).filter_by(id=id).delete()
  91. db.commit()
  92. return True
  93. except Exception:
  94. return False
  95. def delete_all_files(self) -> bool:
  96. with get_db() as db:
  97. try:
  98. db.query(File).delete()
  99. db.commit()
  100. return True
  101. except Exception:
  102. return False
  103. Files = FilesTable()