diff --git a/seahub/ai/apis.py b/seahub/ai/apis.py index 8b948ee497a..d5350a00872 100644 --- a/seahub/ai/apis.py +++ b/seahub/ai/apis.py @@ -17,7 +17,7 @@ from seahub.utils.repo import is_valid_repo_id_format, is_repo_admin from seahub.ai.utils import search, get_file_download_token, get_search_repos, \ RELATED_REPOS_PREFIX, RELATED_REPOS_CACHE_TIMEOUT, SEARCH_REPOS_LIMIT, \ - format_repos + format_repos, ocr from seahub.utils import is_org_context, normalize_cache_key, HAS_FILE_SEASEARCH from seahub.views import check_folder_permission @@ -121,3 +121,48 @@ def post(self, request): f['fullpath'] = f['fullpath'].split(origin_path)[-1] return Response(resp_json, resp.status_code) + + +class OCR(APIView): + authentication_classes = (TokenAuthentication, SessionAuthentication) + permission_classes = (IsAuthenticated, ) + throttle_classes = (UserRateThrottle, ) + + def post(self, request): + repo_id = request.data.get('repo_id') + path = request.data.get('path') + + if not repo_id: + return api_error(status.HTTP_400_BAD_REQUEST, 'repo_id invalid') + if not path: + return api_error(status.HTTP_400_BAD_REQUEST, 'path 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) + + file_obj = seafile_api.get_dirent_by_path(repo_id, path) + if not file_obj: + error_msg = 'File %s not found.' % path + return api_error(status.HTTP_404_NOT_FOUND, error_msg) + + # permission check + if not check_folder_permission(request, repo_id, path): + error_msg = 'Permission denied.' + return api_error(status.HTTP_403_FORBIDDEN, error_msg) + + params = { + 'repo_id': repo_id, + 'path': path, + 'obj_id': file_obj.obj_id, + } + + try: + ocr_result = ocr(params) + except Exception as e: + logger.error(e) + error_msg = 'Internal Server Error' + return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) + + return Response({'ocr_result': ocr_result}, status.HTTP_200_OK) diff --git a/seahub/ai/utils.py b/seahub/ai/utils.py index 75ddade0462..99ba2907573 100644 --- a/seahub/ai/utils.py +++ b/seahub/ai/utils.py @@ -4,7 +4,7 @@ import time from urllib.parse import urljoin -from seahub.settings import SECRET_KEY, SEAFEVENTS_SERVER_URL +from seahub.settings import SECRET_KEY, SEAFEVENTS_SERVER_URL, SEAFILE_AI_SECRET_KEY, SEAFILE_AI_SERVER_URL from seahub.utils import get_user_repos from seaserv import seafile_api @@ -18,6 +18,12 @@ RELATED_REPOS_CACHE_TIMEOUT = 2 * 60 * 60 +def gen_headers(): + payload = {'exp': int(time.time()) + 300, } + token = jwt.encode(payload, SEAFILE_AI_SECRET_KEY, algorithm='HS256') + return {"Authorization": "Token %s" % token} + + def search(params): payload = {'exp': int(time.time()) + 300, } token = jwt.encode(payload, SECRET_KEY, algorithm='HS256') @@ -65,3 +71,10 @@ def format_repos(repos): continue repos_map[real_repo_id] = (real_repo_id, origin_path, repo_name) return searched_repos, repos_map + + +def ocr(params): + headers = gen_headers() + url = urljoin(SEAFILE_AI_SERVER_URL, '/api/v1/ocr/') + resp = requests.post(url, json=params, headers=headers, timeout=30) + return resp diff --git a/seahub/settings.py b/seahub/settings.py index 11e600898d2..8596d0f3259 100644 --- a/seahub/settings.py +++ b/seahub/settings.py @@ -777,6 +777,13 @@ def genpassword(): ENABLE_GLOBAL_ADDRESSBOOK = True ENABLE_ADDRESSBOOK_OPT_IN = False +##################### +# Seafile AI # +##################### +SEAFILE_AI_SERVER_URL = '' +SEAFILE_AI_SECRET_KEY = '' +ENABLE_SEAFILE_AI = False + #################### # Guest Invite # #################### diff --git a/seahub/urls.py b/seahub/urls.py index 33040c43dfa..2bbaeb38bac 100644 --- a/seahub/urls.py +++ b/seahub/urls.py @@ -203,7 +203,7 @@ from seahub.seadoc.views import sdoc_revision, sdoc_revisions, sdoc_to_docx from seahub.ocm.settings import OCM_ENDPOINT -from seahub.ai.apis import Search +from seahub.ai.apis import Search, OCR from seahub.wiki2.views import wiki_view from seahub.api2.endpoints.wiki2 import Wikis2View, Wiki2View, Wiki2ConfigView, Wiki2PagesView, Wiki2PageView, \ Wiki2DuplicatePageView, WikiPageTrashView @@ -1044,3 +1044,8 @@ re_path(r'^api/v2.1/repos/(?P[-0-9a-f]{36})/metadata/move-views/$', MetadataViewsMoveView.as_view(), name='api-v2.1-metadata-views-move'), ] + +if settings.ENABLE_SEAFILE_AI: + urlpatterns += [ + re_path(r'^api/v2.1/ai/ocr/$', OCR.as_view(), name='api-v2.1-ai-ocr'), + ]