files.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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, JSON
  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. hash = Column(String)
  18. filename = Column(Text)
  19. data = Column(JSON)
  20. meta = Column(JSONField)
  21. created_at = Column(BigInteger)
  22. updated_at = Column(BigInteger)
  23. class FileModel(BaseModel):
  24. model_config = ConfigDict(from_attributes=True)
  25. id: str
  26. user_id: str
  27. hash: str
  28. filename: str
  29. data: dict
  30. meta: dict
  31. created_at: int # timestamp in epoch
  32. updated_at: int # timestamp in epoch
  33. ####################
  34. # Forms
  35. ####################
  36. class FileModelResponse(BaseModel):
  37. id: str
  38. user_id: str
  39. hash: str
  40. filename: str
  41. data: dict
  42. meta: dict
  43. created_at: int # timestamp in epoch
  44. updated_at: int # timestamp in epoch
  45. class FileForm(BaseModel):
  46. id: str
  47. filename: str
  48. meta: dict = {}
  49. class FilesTable:
  50. def insert_new_file(self, user_id: str, form_data: FileForm) -> Optional[FileModel]:
  51. with get_db() as db:
  52. file = FileModel(
  53. **{
  54. **form_data.model_dump(),
  55. "user_id": user_id,
  56. "created_at": int(time.time()),
  57. }
  58. )
  59. try:
  60. result = File(**file.model_dump())
  61. db.add(result)
  62. db.commit()
  63. db.refresh(result)
  64. if result:
  65. return FileModel.model_validate(result)
  66. else:
  67. return None
  68. except Exception as e:
  69. print(f"Error creating tool: {e}")
  70. return None
  71. def get_file_by_id(self, id: str) -> Optional[FileModel]:
  72. with get_db() as db:
  73. try:
  74. file = db.get(File, id)
  75. return FileModel.model_validate(file)
  76. except Exception:
  77. return None
  78. def get_files(self) -> list[FileModel]:
  79. with get_db() as db:
  80. return [FileModel.model_validate(file) for file in db.query(File).all()]
  81. def get_files_by_user_id(self, user_id: str) -> list[FileModel]:
  82. with get_db() as db:
  83. return [
  84. FileModel.model_validate(file)
  85. for file in db.query(File).filter_by(user_id=user_id).all()
  86. ]
  87. def update_files_metadata_by_id(self, id: str, meta: dict) -> Optional[FileModel]:
  88. with get_db() as db:
  89. try:
  90. file = db.query(File).filter_by(id=id).first()
  91. file.meta = {**file.meta, **meta}
  92. db.commit()
  93. return FileModel.model_validate(file)
  94. except Exception:
  95. return None
  96. def delete_file_by_id(self, id: str) -> bool:
  97. with get_db() as db:
  98. try:
  99. db.query(File).filter_by(id=id).delete()
  100. db.commit()
  101. return True
  102. except Exception:
  103. return False
  104. def delete_all_files(self) -> bool:
  105. with get_db() as db:
  106. try:
  107. db.query(File).delete()
  108. db.commit()
  109. return True
  110. except Exception:
  111. return False
  112. Files = FilesTable()