Skip to content

Commit

Permalink
Add API for moving files (infiniflow#1016)
Browse files Browse the repository at this point in the history
### 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)
  • Loading branch information
aopstudio authored May 31, 2024
1 parent 398ecbe commit 54c1ea3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
22 changes: 22 additions & 0 deletions api/apps/file_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,5 +343,27 @@ def get(file_id):
'application/%s' %
ext.group(1))
return response
except Exception as e:
return server_error_response(e)

@manager.route('/mv', methods=['POST'])
@login_required
@validate_request("src_file_ids", "dest_file_id")
def move():
req = request.json
try:
file_ids = req["src_file_ids"]
parent_id = req["dest_file_id"]
for file_id in file_ids:
e, file = FileService.get_by_id(file_id)
if not e:
return get_data_error_result(retmsg="File or Folder not found!")
if not file.tenant_id:
return get_data_error_result(retmsg="Tenant not found!")
fe, _ = FileService.get_by_id(parent_id)
if not fe:
return get_data_error_result(retmsg="Parent Folder not found!")
FileService.move_file(file_ids, parent_id)
return get_json_result(data=True)
except Exception as e:
return server_error_response(e)
11 changes: 10 additions & 1 deletion api/db/services/file_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,4 +304,13 @@ def add_file_from_kb(cls, doc, kb_folder_id, tenant_id):
"source_type": FileSource.KNOWLEDGEBASE
}
cls.save(**file)
File2DocumentService.save(**{"id": get_uuid(), "file_id": file["id"], "document_id": doc["id"]})
File2DocumentService.save(**{"id": get_uuid(), "file_id": file["id"], "document_id": doc["id"]})

@classmethod
@DB.connection_context()
def move_file(cls, file_ids, folder_id):
try:
cls.filter_update((cls.model.id << file_ids, ), { 'parent_id': folder_id })
except Exception as e:
print(e)
raise RuntimeError("Database error (File move)!")

0 comments on commit 54c1ea3

Please sign in to comment.