From f047a67855a43c992545ff882fae7ad56b6fdd2e Mon Sep 17 00:00:00 2001 From: skywalker Date: Tue, 5 Dec 2023 16:09:41 +0800 Subject: [PATCH] batchUploadImage --- seahub/seadoc/apis.py | 47 ++++++++++++++++++++++++------------------ seahub/seadoc/utils.py | 2 +- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/seahub/seadoc/apis.py b/seahub/seadoc/apis.py index 4afc110c4c..6c61e49607 100644 --- a/seahub/seadoc/apis.py +++ b/seahub/seadoc/apis.py @@ -306,15 +306,18 @@ class SeadocUploadImage(APIView): error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) - file = request.FILES.get('file') - if not file: + file_list = request.FILES.getlist('file') + if not file_list or not isinstance(file_list, list): error_msg = 'Image can not be found.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) + # max 10 images + file_list = file_list[:10] - file_type, ext = get_file_type_and_ext(file.name) - if file_type != IMAGE: - error_msg = file_type_error_msg(ext, PREVIEW_FILEEXT.get('Image')) - return api_error(status.HTTP_400_BAD_REQUEST, error_msg) + for file in file_list: + file_type, ext = get_file_type_and_ext(file.name) + if file_type != IMAGE: + error_msg = file_type_error_msg(ext, PREVIEW_FILEEXT.get('Image')) + return api_error(status.HTTP_400_BAD_REQUEST, error_msg) uuid_map = FileUUIDMap.objects.get_fileuuidmap_by_uuid(file_uuid) if not uuid_map: @@ -329,22 +332,26 @@ class SeadocUploadImage(APIView): repo_id = uuid_map.repo_id username = payload.get('username', '') parent_path = gen_seadoc_image_parent_path(file_uuid, repo_id, username) - file_path = posixpath.join(parent_path, file.name) upload_link = get_seadoc_asset_upload_link(repo_id, parent_path, username) - files = { - 'file': file, - 'file_name': file.name, - 'target_file': file_path, - } - data = {'parent_dir': parent_path} - resp = requests.post(upload_link, files=files, data=data) - if not resp.ok: - logger.error(resp.text) - error_msg = 'Internal Server Error' - return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) - image_url = '/' + file.name - return Response({'relative_path': image_url}) + + relative_path = [] + for file in file_list: + file_path = posixpath.join(parent_path, file.name) + files = { + 'file': file, + 'file_name': file.name, + 'target_file': file_path, + } + data = {'parent_dir': parent_path} + resp = requests.post(upload_link, files=files, data=data) + if not resp.ok: + logger.error(resp.text) + error_msg = 'Internal Server Error' + return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) + image_url = '/' + file.name + relative_path.append(image_url) + return Response({'relative_path': relative_path}) class SeadocDownloadImage(APIView): diff --git a/seahub/seadoc/utils.py b/seahub/seadoc/utils.py index 066843a1b6..578fe98bef 100644 --- a/seahub/seadoc/utils.py +++ b/seahub/seadoc/utils.py @@ -147,7 +147,7 @@ def gen_seadoc_image_parent_path(file_uuid, repo_id, username): def get_seadoc_asset_upload_link(repo_id, parent_path, username): obj_id = json.dumps({'parent_dir': parent_path}) token = seafile_api.get_fileserver_access_token( - repo_id, obj_id, 'upload-link', username, use_onetime=True) + repo_id, obj_id, 'upload-link', username, use_onetime=False) if not token: return None upload_link = gen_file_upload_url(token, 'upload-api')