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_app.py 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 api.db.services.file2document_service import File2DocumentService
  17. from api.db.services.file_service import FileService
  18. from flask import request
  19. from flask_login import login_required, current_user
  20. from api.db.services.knowledgebase_service import KnowledgebaseService
  21. from api.utils.api_utils import server_error_response, get_data_error_result, validate_request
  22. from api.utils import get_uuid
  23. from api.db import FileType
  24. from api.db.services.document_service import DocumentService
  25. from api import settings
  26. from api.utils.api_utils import get_json_result
  27. @manager.route('/convert', methods=['POST']) # noqa: F821
  28. @login_required
  29. @validate_request("file_ids", "kb_ids")
  30. def convert():
  31. req = request.json
  32. kb_ids = req["kb_ids"]
  33. file_ids = req["file_ids"]
  34. file2documents = []
  35. try:
  36. for file_id in file_ids:
  37. e, file = FileService.get_by_id(file_id)
  38. file_ids_list = [file_id]
  39. if file.type == FileType.FOLDER.value:
  40. file_ids_list = FileService.get_all_innermost_file_ids(file_id, [])
  41. for id in file_ids_list:
  42. informs = File2DocumentService.get_by_file_id(id)
  43. # delete
  44. for inform in informs:
  45. doc_id = inform.document_id
  46. e, doc = DocumentService.get_by_id(doc_id)
  47. if not e:
  48. return get_data_error_result(message="Document not found!")
  49. tenant_id = DocumentService.get_tenant_id(doc_id)
  50. if not tenant_id:
  51. return get_data_error_result(message="Tenant not found!")
  52. if not DocumentService.remove_document(doc, tenant_id):
  53. return get_data_error_result(
  54. message="Database error (Document removal)!")
  55. File2DocumentService.delete_by_file_id(id)
  56. # insert
  57. for kb_id in kb_ids:
  58. e, kb = KnowledgebaseService.get_by_id(kb_id)
  59. if not e:
  60. return get_data_error_result(
  61. message="Can't find this knowledgebase!")
  62. e, file = FileService.get_by_id(id)
  63. if not e:
  64. return get_data_error_result(
  65. message="Can't find this file!")
  66. doc = DocumentService.insert({
  67. "id": get_uuid(),
  68. "kb_id": kb.id,
  69. "parser_id": FileService.get_parser(file.type, file.name, kb.parser_id),
  70. "parser_config": kb.parser_config,
  71. "created_by": current_user.id,
  72. "type": file.type,
  73. "name": file.name,
  74. "location": file.location,
  75. "size": file.size
  76. })
  77. file2document = File2DocumentService.insert({
  78. "id": get_uuid(),
  79. "file_id": id,
  80. "document_id": doc.id,
  81. })
  82. file2documents.append(file2document.to_json())
  83. return get_json_result(data=file2documents)
  84. except Exception as e:
  85. return server_error_response(e)
  86. @manager.route('/rm', methods=['POST']) # noqa: F821
  87. @login_required
  88. @validate_request("file_ids")
  89. def rm():
  90. req = request.json
  91. file_ids = req["file_ids"]
  92. if not file_ids:
  93. return get_json_result(
  94. data=False, message='Lack of "Files ID"', code=settings.RetCode.ARGUMENT_ERROR)
  95. try:
  96. for file_id in file_ids:
  97. informs = File2DocumentService.get_by_file_id(file_id)
  98. if not informs:
  99. return get_data_error_result(message="Inform not found!")
  100. for inform in informs:
  101. if not inform:
  102. return get_data_error_result(message="Inform not found!")
  103. File2DocumentService.delete_by_file_id(file_id)
  104. doc_id = inform.document_id
  105. e, doc = DocumentService.get_by_id(doc_id)
  106. if not e:
  107. return get_data_error_result(message="Document not found!")
  108. tenant_id = DocumentService.get_tenant_id(doc_id)
  109. if not tenant_id:
  110. return get_data_error_result(message="Tenant not found!")
  111. if not DocumentService.remove_document(doc, tenant_id):
  112. return get_data_error_result(
  113. message="Database error (Document removal)!")
  114. return get_json_result(data=True)
  115. except Exception as e:
  116. return server_error_response(e)