diff --git a/seahub/ai/apis.py b/seahub/ai/apis.py index 4054ddf3b1e..2f8d04b4aba 100644 --- a/seahub/ai/apis.py +++ b/seahub/ai/apis.py @@ -1,5 +1,4 @@ # -*- coding: utf-8 -*- -import os import logging from rest_framework.authentication import SessionAuthentication @@ -14,8 +13,9 @@ from seahub.views import check_folder_permission from seahub.utils.repo import parse_repo_perm -from seahub.ai.utils import create_library_sdoc_index, get_sdoc_info_recursively, similarity_search_in_library, \ - update_library_sdoc_index, delete_library_index, query_task_status, query_library_index_state, question_answering_search_in_library +from seahub.ai.utils import create_library_sdoc_index, similarity_search_in_library, update_library_sdoc_index, \ + delete_library_index, query_task_status, query_library_index_state, question_answering_search_in_library,\ + get_file_download_token from seaserv import seafile_api @@ -39,24 +39,14 @@ def post(self, request): return api_error(status.HTTP_404_NOT_FOUND, error_msg) parent_dir = '/' - username = request.user.username # permission check if parse_repo_perm(check_folder_permission(request, repo_id, parent_dir)).can_download is False: error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) - try: - sdoc_info_list = get_sdoc_info_recursively(username, repo_id, parent_dir, []) - except Exception as e: - logger.error(e) - error_msg = 'Internal Server Error' - return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) - params = { 'repo_id': repo_id, - 'last_modify': repo.last_modify, - 'sdoc_info_list': sdoc_info_list } try: @@ -92,21 +82,9 @@ def post(self, request): if not repo_id: return api_error(status.HTTP_400_BAD_REQUEST, 'repo_id invalid') - parent_dir = '/' - username = request.user.username - - try: - sdoc_info_list = get_sdoc_info_recursively(username, repo_id, parent_dir, []) - except Exception as e: - logger.error(e) - error_msg = 'Internal Server Error' - return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) - - sdoc_files_info = {file.get('path'): file for file in sdoc_info_list} params = { 'query': query, - 'associate_id': repo_id, - 'sdoc_files_info': sdoc_files_info, + 'repo_id': repo_id, 'count': count, } @@ -122,8 +100,8 @@ def post(self, request): return Response(resp_json, resp.status_code) -class QuestionAnsweringSearchInLibrary(APIView): +class QuestionAnsweringSearchInLibrary(APIView): authentication_classes = (TokenAuthentication, SessionAuthentication) permission_classes = (IsAuthenticated, ) throttle_classes = (UserRateThrottle, ) @@ -143,21 +121,9 @@ def post(self, request): if not repo_id: return api_error(status.HTTP_400_BAD_REQUEST, 'repo_id invalid') - parent_dir = '/' - username = request.user.username - - try: - sdoc_info_list = get_sdoc_info_recursively(username, repo_id, parent_dir, []) - except Exception as e: - logger.error(e) - error_msg = 'Internal Server Error' - return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) - - sdoc_files_info = {file.get('path'): file for file in sdoc_info_list} params = { 'query': query, - 'associate_id': repo_id, - 'sdoc_files_info': sdoc_files_info, + 'repo_id': repo_id, 'count': count, } @@ -173,6 +139,7 @@ def post(self, request): return Response(resp_json, resp.status_code) + class LibrarySdocIndex(APIView): authentication_classes = (TokenAuthentication, SessionAuthentication) permission_classes = (IsAuthenticated, ) @@ -190,24 +157,14 @@ def put(self, request): return api_error(status.HTTP_404_NOT_FOUND, error_msg) parent_dir = '/' - username = request.user.username # permission check if parse_repo_perm(check_folder_permission(request, repo_id, parent_dir)).can_download is False: error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) - try: - sdoc_info_list = get_sdoc_info_recursively(username, repo_id, parent_dir, []) - except Exception as e: - logger.error(e) - error_msg = 'Internal Server Error' - return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) - params = { - 'associate_id': repo_id, - 'last_modify': repo.last_modify, - 'sdoc_info_list': sdoc_info_list, + 'repo_id': repo_id } try: @@ -286,34 +243,44 @@ def get(self, request): return Response(resp_json, resp.status_code) -class RepoFiles(APIView): +class FileDownloadToken(APIView): authentication_classes = (SeafileAiAuthentication, ) throttle_classes = (UserRateThrottle, ) def get(self, request): repo_id = request.GET.get('repo_id') + path = request.GET.get('path') + if not repo_id: return api_error(status.HTTP_400_BAD_REQUEST, 'repo_id invalid') - repo = seafile_api.get_repo(repo_id) - if not repo: - error_msg = 'Library %s not found.' % repo_id - return api_error(status.HTTP_404_NOT_FOUND, error_msg) + if not path: + return api_error(status.HTTP_400_BAD_REQUEST, 'path invalid') - parent_dir = '/' + file_id = seafile_api.get_file_id_by_path(repo_id, path) username = request.user.username - - try: - sdoc_info_list = get_sdoc_info_recursively(username, repo_id, parent_dir, []) - except Exception as e: - logger.error(e) - error_msg = 'Internal Server Error' - return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) + download_token = get_file_download_token(repo_id, file_id, username) library_files_info = { - 'associate_id': repo_id, - 'last_modify': repo.last_modify, - 'sdoc_info_list': sdoc_info_list, + 'download_token': download_token } return Response(library_files_info, status.HTTP_200_OK) + + +# class RepoCommit(APIView): +# authentication_classes = (SeafileAiAuthentication, ) +# throttle_classes = (UserRateThrottle, ) +# +# def get(self, request): +# repo_id = request.GET.get('repo_id') +# if not repo_id: +# return api_error(status.HTTP_400_BAD_REQUEST, 'repo_id invalid') +# +# commit_id = get_latest_commit_id(repo_id) +# +# repo_info = { +# 'commit_id': commit_id +# } +# +# return Response(repo_info, status.HTTP_200_OK) diff --git a/seahub/ai/utils.py b/seahub/ai/utils.py index 3f97ffa3c41..c46f5792b27 100644 --- a/seahub/ai/utils.py +++ b/seahub/ai/utils.py @@ -1,10 +1,7 @@ -import os import logging import requests import jwt import time -import stat -import posixpath from urllib.parse import urljoin from seahub.settings import SEAFILE_AI_SERVER_URL, SEAFILE_AI_SECRET_KEY @@ -21,40 +18,6 @@ def gen_headers(): return {"Authorization": "Token %s" % token} -def get_sdoc_info_recursively(username, repo_id, path, sdoc_info_list, include_sys_dir=False): - dirs = seafile_api.list_dir_by_path(repo_id, path) - for dirent in dirs: - - if stat.S_ISDIR(dirent.mode): - if not include_sys_dir and path =='/' and dirent.obj_name in ['images', 'Revisions']: - continue - - sub_path = posixpath.join(path, dirent.obj_name) - get_sdoc_info_recursively(username, repo_id, sub_path, sdoc_info_list) - continue - - sdoc_parent_dir = path - filename = dirent.obj_name - file_id = dirent.obj_id - mtime = dirent.mtime - size = dirent.size - if not filename.endswith('.sdoc'): - continue - - sdoc_path = os.path.join(sdoc_parent_dir, filename) - download_token = seafile_api.get_fileserver_access_token(repo_id, file_id, 'download', username, - use_onetime=True) - sdoc_info = { - 'path': sdoc_path, - 'download_token': download_token, - 'mtime': mtime, - 'size': size, - } - sdoc_info_list.append(sdoc_info) - - return sdoc_info_list - - def create_library_sdoc_index(params): headers = gen_headers() url = urljoin(SEAFILE_AI_SERVER_URL, '/api/v1/library-sdoc-indexes/') @@ -84,7 +47,7 @@ def update_library_sdoc_index(params): def delete_library_index(repo_id): headers = gen_headers() url = urljoin(SEAFILE_AI_SERVER_URL, '/api/v1/library-sdoc-index/') - params = {'associate_id': repo_id} + params = {'repo_id': repo_id} resp = requests.delete(url, headers=headers, json=params) return resp @@ -96,8 +59,12 @@ def query_task_status(task_id): return resp -def query_library_index_state(associate_id): +def query_library_index_state(repo_id): headers = gen_headers() url = urljoin(SEAFILE_AI_SERVER_URL, '/api/v1/library-index-state/') - resp = requests.get(url, headers=headers, params={'associate_id': associate_id}) + resp = requests.get(url, headers=headers, params={'repo_id': repo_id}) return resp + + +def get_file_download_token(repo_id, file_id, username): + return seafile_api.get_fileserver_access_token(repo_id, file_id, 'download', username, use_onetime=True) diff --git a/seahub/urls.py b/seahub/urls.py index e0522e6adc8..8a0313af7f6 100644 --- a/seahub/urls.py +++ b/seahub/urls.py @@ -202,7 +202,7 @@ from seahub.ocm.settings import OCM_ENDPOINT -from seahub.ai.apis import LibrarySdocIndexes, SimilaritySearchInLibrary, LibrarySdocIndex, RepoFiles, TaskStatus, \ +from seahub.ai.apis import LibrarySdocIndexes, SimilaritySearchInLibrary, LibrarySdocIndex, TaskStatus, \ LibraryIndexState, QuestionAnsweringSearchInLibrary urlpatterns = [ @@ -968,7 +968,6 @@ re_path(r'^api/v2.1/ai/similarity-search-in-library/$', SimilaritySearchInLibrary.as_view(), name='api-v2.1-ai-similarity-search-in-library'), re_path(r'^api/v2.1/ai/question-answering-search-in-library/$', QuestionAnsweringSearchInLibrary.as_view(), name='api-v2.1-ai-question-answering-search-in-library'), re_path(r'^api/v2.1/ai/library-sdoc-index/$', LibrarySdocIndex.as_view(), name='api-v2.1-ai-library-sdoc-index'), - re_path(r'^api/v2.1/ai/repo/files/$', RepoFiles.as_view(), name='api-v2.1-ai-repo-files'), re_path(r'^api/v2.1/ai/task-status/$', TaskStatus.as_view(), name='api-v2.1-ai-task-status'), re_path(r'^api/v2.1/ai/library-index-state/$', LibraryIndexState.as_view(), name='api-v2.1-ai-library-index-state'), ]