Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

file2document_service.py 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #
  2. # Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. from datetime import datetime
  17. from api.db import FileSource
  18. from api.db.db_models import DB
  19. from api.db.db_models import File, File2Document
  20. from api.db.services.common_service import CommonService
  21. from api.db.services.document_service import DocumentService
  22. from api.utils import current_timestamp, datetime_format
  23. class File2DocumentService(CommonService):
  24. model = File2Document
  25. @classmethod
  26. @DB.connection_context()
  27. def get_by_file_id(cls, file_id):
  28. objs = cls.model.select().where(cls.model.file_id == file_id)
  29. return objs
  30. @classmethod
  31. @DB.connection_context()
  32. def get_by_document_id(cls, document_id):
  33. objs = cls.model.select().where(cls.model.document_id == document_id)
  34. return objs
  35. @classmethod
  36. @DB.connection_context()
  37. def insert(cls, obj):
  38. if not cls.save(**obj):
  39. raise RuntimeError("Database error (File)!")
  40. return File2Document(**obj)
  41. @classmethod
  42. @DB.connection_context()
  43. def delete_by_file_id(cls, file_id):
  44. return cls.model.delete().where(cls.model.file_id == file_id).execute()
  45. @classmethod
  46. @DB.connection_context()
  47. def delete_by_document_id(cls, doc_id):
  48. return cls.model.delete().where(cls.model.document_id == doc_id).execute()
  49. @classmethod
  50. @DB.connection_context()
  51. def update_by_file_id(cls, file_id, obj):
  52. obj["update_time"] = current_timestamp()
  53. obj["update_date"] = datetime_format(datetime.now())
  54. cls.model.update(obj).where(cls.model.id == file_id).execute()
  55. return File2Document(**obj)
  56. @classmethod
  57. @DB.connection_context()
  58. def get_storage_address(cls, doc_id=None, file_id=None):
  59. if doc_id:
  60. f2d = cls.get_by_document_id(doc_id)
  61. else:
  62. f2d = cls.get_by_file_id(file_id)
  63. if f2d:
  64. file = File.get_by_id(f2d[0].file_id)
  65. if not file.source_type or file.source_type == FileSource.LOCAL:
  66. return file.parent_id, file.location
  67. doc_id = f2d[0].document_id
  68. assert doc_id, "please specify doc_id"
  69. e, doc = DocumentService.get_by_id(doc_id)
  70. return doc.kb_id, doc.location