GYH
commited on
Commit
·
9d69040
1
Parent(s):
bf38604
Add /api/document/rm function (#887)
Browse files### What problem does this PR solve?
Delete files from a knowledge base.
#717
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
- api/apps/api_app.py +62 -2
api/apps/api_app.py
CHANGED
@@ -20,8 +20,8 @@ from datetime import datetime, timedelta
|
|
20 |
from flask import request, Response
|
21 |
from flask_login import login_required, current_user
|
22 |
|
23 |
-
from api.db import FileType, ParserType
|
24 |
-
from api.db.db_models import APIToken, API4Conversation, Task
|
25 |
from api.db.services import duplicate_name
|
26 |
from api.db.services.api_service import APITokenService, API4ConversationService
|
27 |
from api.db.services.dialog_service import DialogService, chat
|
@@ -428,3 +428,63 @@ def list_kb_docs():
|
|
428 |
|
429 |
except Exception as e:
|
430 |
return server_error_response(e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
from flask import request, Response
|
21 |
from flask_login import login_required, current_user
|
22 |
|
23 |
+
from api.db import FileType, ParserType, FileSource
|
24 |
+
from api.db.db_models import APIToken, API4Conversation, Task, File
|
25 |
from api.db.services import duplicate_name
|
26 |
from api.db.services.api_service import APITokenService, API4ConversationService
|
27 |
from api.db.services.dialog_service import DialogService, chat
|
|
|
428 |
|
429 |
except Exception as e:
|
430 |
return server_error_response(e)
|
431 |
+
|
432 |
+
|
433 |
+
@manager.route('/document/rm', methods=['POST'])
|
434 |
+
# @login_required
|
435 |
+
def document_rm():
|
436 |
+
token = request.headers.get('Authorization').split()[1]
|
437 |
+
objs = APIToken.query(token=token)
|
438 |
+
if not objs:
|
439 |
+
return get_json_result(
|
440 |
+
data=False, retmsg='Token is not valid!"', retcode=RetCode.AUTHENTICATION_ERROR)
|
441 |
+
|
442 |
+
tenant_id = objs[0].tenant_id
|
443 |
+
req = request.json
|
444 |
+
doc_ids = []
|
445 |
+
try:
|
446 |
+
doc_ids = [DocumentService.get_doc_id_by_doc_name(doc_name) for doc_name in req.get("doc_names", [])]
|
447 |
+
for doc_id in req.get("doc_ids", []):
|
448 |
+
if doc_id not in doc_ids:
|
449 |
+
doc_ids.append(doc_id)
|
450 |
+
|
451 |
+
if not doc_ids:
|
452 |
+
return get_json_result(
|
453 |
+
data=False, retmsg="Can't find doc_names or doc_ids"
|
454 |
+
)
|
455 |
+
|
456 |
+
except Exception as e:
|
457 |
+
return server_error_response(e)
|
458 |
+
|
459 |
+
root_folder = FileService.get_root_folder(tenant_id)
|
460 |
+
pf_id = root_folder["id"]
|
461 |
+
FileService.init_knowledgebase_docs(pf_id, tenant_id)
|
462 |
+
|
463 |
+
errors = ""
|
464 |
+
for doc_id in doc_ids:
|
465 |
+
try:
|
466 |
+
e, doc = DocumentService.get_by_id(doc_id)
|
467 |
+
if not e:
|
468 |
+
return get_data_error_result(retmsg="Document not found!")
|
469 |
+
tenant_id = DocumentService.get_tenant_id(doc_id)
|
470 |
+
if not tenant_id:
|
471 |
+
return get_data_error_result(retmsg="Tenant not found!")
|
472 |
+
|
473 |
+
b, n = File2DocumentService.get_minio_address(doc_id=doc_id)
|
474 |
+
|
475 |
+
if not DocumentService.remove_document(doc, tenant_id):
|
476 |
+
return get_data_error_result(
|
477 |
+
retmsg="Database error (Document removal)!")
|
478 |
+
|
479 |
+
f2d = File2DocumentService.get_by_document_id(doc_id)
|
480 |
+
FileService.filter_delete([File.source_type == FileSource.KNOWLEDGEBASE, File.id == f2d[0].file_id])
|
481 |
+
File2DocumentService.delete_by_document_id(doc_id)
|
482 |
+
|
483 |
+
MINIO.rm(b, n)
|
484 |
+
except Exception as e:
|
485 |
+
errors += str(e)
|
486 |
+
|
487 |
+
if errors:
|
488 |
+
return get_json_result(data=False, retmsg=errors, retcode=RetCode.SERVER_ERROR)
|
489 |
+
|
490 |
+
return get_json_result(data=True)
|