Add API for moving files (#1016)
Browse files### What problem does this PR solve?
Add backend API support for moving files into other directory
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
- api/apps/file_app.py +22 -0
- api/db/services/file_service.py +10 -1
api/apps/file_app.py
CHANGED
@@ -343,5 +343,27 @@ def get(file_id):
|
|
343 |
'application/%s' %
|
344 |
ext.group(1))
|
345 |
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
346 |
except Exception as e:
|
347 |
return server_error_response(e)
|
|
|
343 |
'application/%s' %
|
344 |
ext.group(1))
|
345 |
return response
|
346 |
+
except Exception as e:
|
347 |
+
return server_error_response(e)
|
348 |
+
|
349 |
+
@manager.route('/mv', methods=['POST'])
|
350 |
+
@login_required
|
351 |
+
@validate_request("src_file_ids", "dest_file_id")
|
352 |
+
def move():
|
353 |
+
req = request.json
|
354 |
+
try:
|
355 |
+
file_ids = req["src_file_ids"]
|
356 |
+
parent_id = req["dest_file_id"]
|
357 |
+
for file_id in file_ids:
|
358 |
+
e, file = FileService.get_by_id(file_id)
|
359 |
+
if not e:
|
360 |
+
return get_data_error_result(retmsg="File or Folder not found!")
|
361 |
+
if not file.tenant_id:
|
362 |
+
return get_data_error_result(retmsg="Tenant not found!")
|
363 |
+
fe, _ = FileService.get_by_id(parent_id)
|
364 |
+
if not fe:
|
365 |
+
return get_data_error_result(retmsg="Parent Folder not found!")
|
366 |
+
FileService.move_file(file_ids, parent_id)
|
367 |
+
return get_json_result(data=True)
|
368 |
except Exception as e:
|
369 |
return server_error_response(e)
|
api/db/services/file_service.py
CHANGED
@@ -304,4 +304,13 @@ class FileService(CommonService):
|
|
304 |
"source_type": FileSource.KNOWLEDGEBASE
|
305 |
}
|
306 |
cls.save(**file)
|
307 |
-
File2DocumentService.save(**{"id": get_uuid(), "file_id": file["id"], "document_id": doc["id"]})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
304 |
"source_type": FileSource.KNOWLEDGEBASE
|
305 |
}
|
306 |
cls.save(**file)
|
307 |
+
File2DocumentService.save(**{"id": get_uuid(), "file_id": file["id"], "document_id": doc["id"]})
|
308 |
+
|
309 |
+
@classmethod
|
310 |
+
@DB.connection_context()
|
311 |
+
def move_file(cls, file_ids, folder_id):
|
312 |
+
try:
|
313 |
+
cls.filter_update((cls.model.id << file_ids, ), { 'parent_id': folder_id })
|
314 |
+
except Exception as e:
|
315 |
+
print(e)
|
316 |
+
raise RuntimeError("Database error (File move)!")
|