23
23
from flask import request
24
24
from flask_login import login_required , current_user
25
25
26
- from api .db .db_models import Task
26
+ from api .db .db_models import Task , File
27
27
from api .db .services .file2document_service import File2DocumentService
28
28
from api .db .services .file_service import FileService
29
29
from api .db .services .task_service import TaskService , queue_tasks
33
33
from api .db .services .knowledgebase_service import KnowledgebaseService
34
34
from api .utils .api_utils import server_error_response , get_data_error_result , validate_request
35
35
from api .utils import get_uuid
36
- from api .db import FileType , TaskStatus , ParserType
36
+ from api .db import FileType , TaskStatus , ParserType , FileSource
37
37
from api .db .services .document_service import DocumentService
38
38
from api .settings import RetCode
39
39
from api .utils .api_utils import get_json_result
@@ -59,12 +59,19 @@ def upload():
59
59
return get_json_result (
60
60
data = False , retmsg = 'No file selected!' , retcode = RetCode .ARGUMENT_ERROR )
61
61
62
+ e , kb = KnowledgebaseService .get_by_id (kb_id )
63
+ if not e :
64
+ raise LookupError ("Can't find this knowledgebase!" )
65
+
66
+ root_folder = FileService .get_root_folder (current_user .id )
67
+ pf_id = root_folder ["id" ]
68
+ FileService .init_knowledgebase_docs (pf_id , current_user .id )
69
+ kb_root_folder = FileService .get_kb_folder (current_user .id )
70
+ kb_folder = FileService .new_a_file_from_kb (kb .tenant_id , kb .name , kb_root_folder ["id" ])
71
+
62
72
err = []
63
73
for file in file_objs :
64
74
try :
65
- e , kb = KnowledgebaseService .get_by_id (kb_id )
66
- if not e :
67
- raise LookupError ("Can't find this knowledgebase!" )
68
75
MAX_FILE_NUM_PER_USER = int (os .environ .get ('MAX_FILE_NUM_PER_USER' , 0 ))
69
76
if MAX_FILE_NUM_PER_USER > 0 and DocumentService .get_doc_count (kb .tenant_id ) >= MAX_FILE_NUM_PER_USER :
70
77
raise RuntimeError ("Exceed the maximum file number of a free user!" )
@@ -99,6 +106,8 @@ def upload():
99
106
if re .search (r"\.(ppt|pptx|pages)$" , filename ):
100
107
doc ["parser_id" ] = ParserType .PRESENTATION .value
101
108
DocumentService .insert (doc )
109
+
110
+ FileService .add_file_from_kb (doc , kb_folder ["id" ], kb .tenant_id )
102
111
except Exception as e :
103
112
err .append (file .filename + ": " + str (e ))
104
113
if err :
@@ -228,11 +237,13 @@ def rm():
228
237
req = request .json
229
238
doc_ids = req ["doc_id" ]
230
239
if isinstance (doc_ids , str ): doc_ids = [doc_ids ]
240
+ root_folder = FileService .get_root_folder (current_user .id )
241
+ pf_id = root_folder ["id" ]
242
+ FileService .init_knowledgebase_docs (pf_id , current_user .id )
231
243
errors = ""
232
244
for doc_id in doc_ids :
233
245
try :
234
246
e , doc = DocumentService .get_by_id (doc_id )
235
-
236
247
if not e :
237
248
return get_data_error_result (retmsg = "Document not found!" )
238
249
tenant_id = DocumentService .get_tenant_id (doc_id )
@@ -241,21 +252,25 @@ def rm():
241
252
242
253
ELASTICSEARCH .deleteByQuery (
243
254
Q ("match" , doc_id = doc .id ), idxnm = search .index_name (tenant_id ))
244
- DocumentService .increment_chunk_num (
245
- doc .id , doc .kb_id , doc .token_num * - 1 , doc .chunk_num * - 1 , 0 )
255
+
256
+ DocumentService .clear_chunk_num (doc_id )
257
+ b , n = File2DocumentService .get_minio_address (doc_id = doc_id )
258
+
246
259
if not DocumentService .delete (doc ):
247
260
return get_data_error_result (
248
261
retmsg = "Database error (Document removal)!" )
249
262
250
- informs = File2DocumentService .get_by_document_id (doc_id )
251
- if not informs :
252
- MINIO . rm ( doc . kb_id , doc . location )
253
- else :
254
- File2DocumentService . delete_by_document_id ( doc_id )
263
+ f2d = File2DocumentService .get_by_document_id (doc_id )
264
+ FileService . filter_delete ([ File . source_type == FileSource . KNOWLEDGEBASE , File . id == f2d [ 0 ]. file_id ])
265
+ File2DocumentService . delete_by_document_id ( doc_id )
266
+
267
+ MINIO . rm ( b , n )
255
268
except Exception as e :
256
269
errors += str (e )
257
270
258
- if errors : return server_error_response (e )
271
+ if errors :
272
+ return get_json_result (data = False , retmsg = errors , retcode = RetCode .SERVER_ERROR )
273
+
259
274
return get_json_result (data = True )
260
275
261
276
0 commit comments