-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Common: Support import the exist file in object storage. #3677
Open
baifachuan
wants to merge
4
commits into
infiniflow:main
Choose a base branch
from
baifachuan:import-file-from-object-storage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
45b0498
Common: Support the exist file in object storage.
baifachuan 0685bbd
Common: Merge from main.
baifachuan 605ce53
Common: Support the exist file in object storage.
baifachuan db03463
Merge branch 'main' into import-file-from-object-storage
KevinHuSh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import os | ||
|
||
from flask import request | ||
from flask_login import login_required, current_user | ||
|
||
from api.db import FileType | ||
from api.db.services.file_service import FileService | ||
from api.utils import get_uuid | ||
from api.utils.api_utils import get_json_result | ||
from api.utils.api_utils import server_error_response, get_data_error_result | ||
from api.utils.file_utils import filename_type | ||
from rag.utils.storage_factory import STORAGE_IMPL | ||
|
||
|
||
@manager.route('/list', methods=['GET']) | ||
def list_storage_keys(): | ||
dir = request.args.get("dir", "/") | ||
try: | ||
def filter_dir_and_exist_file(key): | ||
if key["size"] == 0: | ||
return False | ||
parent_dir = os.path.dirname(key["name"]) | ||
file_name = os.path.basename(key["name"]) | ||
dirs = list(filter(None, parent_dir.split("/"))) | ||
user_root_folder = FileService.get_root_folder(current_user.id) | ||
pf_id = user_root_folder["id"] | ||
for dir in dirs: | ||
exist_file = FileService.get_by_pf_id_name(id=pf_id, name=dir) | ||
if not exist_file: | ||
return True | ||
pf_id = exist_file.id | ||
exist_file = FileService.get_by_pf_id_name(id=pf_id, name=file_name) | ||
return exist_file == None | ||
|
||
files = STORAGE_IMPL.list(None, dir) | ||
return get_json_result(data=list(filter(filter_dir_and_exist_file, files))) | ||
except Exception as e: | ||
return server_error_response(e) | ||
|
||
|
||
@manager.route('/import', methods=['POST']) | ||
@login_required | ||
def import_storage_keys(): | ||
content = request.json | ||
keys = content['keys'] | ||
is_dir = content['dir'] | ||
try: | ||
all_keys = [] | ||
file_res = [] | ||
if is_dir: | ||
for key in keys: | ||
all_keys = all_keys + STORAGE_IMPL.list(bucket=None, dir=key, recursive=True) | ||
else: | ||
for key in keys: | ||
all_keys.append(key) | ||
|
||
for key in all_keys: | ||
parent_dir = os.path.dirname(key) | ||
file_name = os.path.basename(key) | ||
|
||
dirs = list(filter(None, parent_dir.split("/"))) | ||
user_root_folder = FileService.get_root_folder(current_user.id) | ||
pf_id = user_root_folder["id"] | ||
for dir in dirs: | ||
exist_file = FileService.get_by_pf_id_name(id=pf_id, name=dir) | ||
if exist_file: | ||
pf_id = exist_file.id | ||
continue | ||
file = FileService.insert({ | ||
"id": get_uuid(), | ||
"parent_id": pf_id, | ||
"tenant_id": current_user.id, | ||
"created_by": current_user.id, | ||
"name": dir, | ||
"location": "", | ||
"size": 0, | ||
"type": FileType.FOLDER.value | ||
}) | ||
pf_id = file.id | ||
|
||
e, file = FileService.get_by_id(pf_id) | ||
if not e: | ||
return get_data_error_result( | ||
retmsg="Can't find this folder!") | ||
|
||
if FileService.get_by_pf_id_name(id=pf_id, name=file_name): | ||
continue | ||
|
||
filetype = filename_type(file_name) | ||
location = key | ||
file = { | ||
"id": get_uuid(), | ||
"parent_id": pf_id, | ||
"tenant_id": current_user.id, | ||
"created_by": current_user.id, | ||
"type": filetype, | ||
"name": file_name, | ||
"location": location, | ||
"size": STORAGE_IMPL.get_properties("bucket", key)["size"], | ||
} | ||
file = FileService.insert(file) | ||
file_res.append(file.to_json()) | ||
|
||
return get_json_result(data=content) | ||
except Exception as e: | ||
return server_error_response(e) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { useGetPagination } from '@/hooks/logic-hooks'; | ||
import { ResponseType } from '@/interfaces/database/base'; | ||
import fileManagerService from '@/services/file-manager-service'; | ||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; | ||
import { PaginationProps } from 'antd'; | ||
|
||
export interface IImportListResult { | ||
name: string; | ||
size: string; | ||
etag: string; | ||
owner: string; | ||
} | ||
|
||
export interface IListResult { | ||
data: IImportListResult[]; | ||
pagination: PaginationProps; | ||
setPagination: (pagination: { page: number; pageSize: number }) => void; | ||
loading: boolean; | ||
} | ||
|
||
export const useFetchImportFileList = (): ResponseType<any> & IListResult => { | ||
const { pagination } = useGetPagination(); | ||
const { data, isFetching: loading } = useQuery({ | ||
queryKey: ['getImportFiles', {}], | ||
initialData: {}, | ||
gcTime: 0, | ||
queryFn: async (params: any) => { | ||
console.info(params); | ||
const { data } = await fileManagerService.getImportFiles(); | ||
return data; | ||
}, | ||
}); | ||
|
||
return { | ||
...data, | ||
pagination: { ...pagination, total: data?.data?.total }, | ||
loading, | ||
}; | ||
}; | ||
|
||
export const useImportFiles = () => { | ||
const queryClient = useQueryClient(); | ||
const { | ||
data, | ||
isPending: loading, | ||
mutateAsync, | ||
} = useMutation({ | ||
mutationKey: ['importFiles'], | ||
mutationFn: async (params: { keys: string[]; dir: boolean }) => { | ||
const { data = {} } = await fileManagerService.importFiles(params); | ||
return data; | ||
}, | ||
}); | ||
|
||
return { data, loading, importFiles: mutateAsync }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm getting a message here that
s3_logger
does not exist in the settings.