You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

file2document_service.py 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. e, obj = cls.get_by_id(obj["id"])
  41. if not e:
  42. raise RuntimeError("Database error (File retrieval)!")
  43. return obj
  44. @classmethod
  45. @DB.connection_context()
  46. def delete_by_file_id(cls, file_id):
  47. return cls.model.delete().where(cls.model.file_id == file_id).execute()
  48. @classmethod
  49. @DB.connection_context()
  50. def delete_by_document_id(cls, doc_id):
  51. return cls.model.delete().where(cls.model.document_id == doc_id).execute()
  52. @classmethod
  53. @DB.connection_context()
  54. def update_by_file_id(cls, file_id, obj):
  55. obj["update_time"] = current_timestamp()
  56. obj["update_date"] = datetime_format(datetime.now())
  57. # num = cls.model.update(obj).where(cls.model.id == file_id).execute()
  58. e, obj = cls.get_by_id(cls.model.id)
  59. return obj
  60. @classmethod
  61. @DB.connection_context()
  62. def get_storage_address(cls, doc_id=None, file_id=None):
  63. if doc_id:
  64. f2d = cls.get_by_document_id(doc_id)
  65. else:
  66. f2d = cls.get_by_file_id(file_id)
  67. if f2d:
  68. file = File.get_by_id(f2d[0].file_id)
  69. if not file.source_type or file.source_type == FileSource.LOCAL:
  70. return file.parent_id, file.location
  71. doc_id = f2d[0].document_id
  72. assert doc_id, "please specify doc_id"
  73. e, doc = DocumentService.get_by_id(doc_id)
  74. return doc.kb_id, doc.location