Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

file2document_service.py 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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.db_models import DB
  18. from api.db.db_models import File, Document, File2Document
  19. from api.db.services.common_service import CommonService
  20. from api.db.services.document_service import DocumentService
  21. from api.db.services.file_service import FileService
  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_minio_address(cls, doc_id=None, file_id=None):
  63. if doc_id:
  64. ids = File2DocumentService.get_by_document_id(doc_id)
  65. else:
  66. ids = File2DocumentService.get_by_file_id(file_id)
  67. if ids:
  68. e, file = FileService.get_by_id(ids[0].file_id)
  69. return file.parent_id, file.location
  70. else:
  71. assert doc_id, "please specify doc_id"
  72. e, doc = DocumentService.get_by_id(doc_id)
  73. return doc.kb_id, doc.location