1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-19 10:26:17 +00:00

batchUploadImage

This commit is contained in:
skywalker
2023-12-05 16:09:41 +08:00
parent 2ce0f90cb8
commit f047a67855
2 changed files with 28 additions and 21 deletions

View File

@@ -306,15 +306,18 @@ class SeadocUploadImage(APIView):
error_msg = 'Permission denied.' error_msg = 'Permission denied.'
return api_error(status.HTTP_403_FORBIDDEN, error_msg) return api_error(status.HTTP_403_FORBIDDEN, error_msg)
file = request.FILES.get('file') file_list = request.FILES.getlist('file')
if not file: if not file_list or not isinstance(file_list, list):
error_msg = 'Image can not be found.' error_msg = 'Image can not be found.'
return api_error(status.HTTP_400_BAD_REQUEST, error_msg) 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) for file in file_list:
if file_type != IMAGE: file_type, ext = get_file_type_and_ext(file.name)
error_msg = file_type_error_msg(ext, PREVIEW_FILEEXT.get('Image')) if file_type != IMAGE:
return api_error(status.HTTP_400_BAD_REQUEST, error_msg) 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) uuid_map = FileUUIDMap.objects.get_fileuuidmap_by_uuid(file_uuid)
if not uuid_map: if not uuid_map:
@@ -329,22 +332,26 @@ class SeadocUploadImage(APIView):
repo_id = uuid_map.repo_id repo_id = uuid_map.repo_id
username = payload.get('username', '') username = payload.get('username', '')
parent_path = gen_seadoc_image_parent_path(file_uuid, repo_id, 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) upload_link = get_seadoc_asset_upload_link(repo_id, parent_path, username)
files = {
'file': file, relative_path = []
'file_name': file.name, for file in file_list:
'target_file': file_path, file_path = posixpath.join(parent_path, file.name)
} files = {
data = {'parent_dir': parent_path} 'file': file,
resp = requests.post(upload_link, files=files, data=data) 'file_name': file.name,
if not resp.ok: 'target_file': file_path,
logger.error(resp.text) }
error_msg = 'Internal Server Error' data = {'parent_dir': parent_path}
return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) resp = requests.post(upload_link, files=files, data=data)
image_url = '/' + file.name if not resp.ok:
return Response({'relative_path': image_url}) 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): class SeadocDownloadImage(APIView):

View File

@@ -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): def get_seadoc_asset_upload_link(repo_id, parent_path, username):
obj_id = json.dumps({'parent_dir': parent_path}) obj_id = json.dumps({'parent_dir': parent_path})
token = seafile_api.get_fileserver_access_token( 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: if not token:
return None return None
upload_link = gen_file_upload_url(token, 'upload-api') upload_link = gen_file_upload_url(token, 'upload-api')